Reputation:
As the titles says, i want to read into a binary file, "mac_address_name", that is already created. The project is runned on a physical device connected to the android studio IDE.
Here's my project tree, you can see the classes and the binary file :
Here's the code in the ProcessorManager class, you can see in comments every other ways i tried to do it...
private String readFromFile() {
String ret = "";
try {
/*InputStream inputStream = main_activity.getApplicationContext().openFileInput("mac_address_name");*/
/*InputStream inputStream = new FileInputStream(new File("../mac_address_name"));*/
/*InputStream inputStream = main_activity.openFileInput("mac_address_name");*/
InputStream inputStream = new FileInputStream(new File("mac_address_name"));
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append("\n").append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}
And the output :
E/login activity: File not found: java.io.FileNotFoundException: mac_address_name: open failed: ENOENT (No such file or directory)
Upvotes: 1
Views: 391
Reputation: 641
For this purpose, you should use assets. Move the file to the src/main/assets folder inside your project. Then use AssetsManager to get file content:
AssetManager am = context.getAssets();
InputStream is = am.open("mac_address_name");
More info at: https://nbasercode.com/net/android-read-text-file-from-assets-folder-in-android-studio/ https://developer.android.com/reference/android/content/res/AssetManager
Upvotes: 1