java.io.FileNotFoundException 当我试图读入我的 Android Studio 项目中的二进制文件时
java.io.FileNotFoundException when I'm trying to read into a binary file in my Android Studio project
正如标题所说,我想读入一个已经创建的二进制文件“mac_address_name”。
该项目在连接到 android 工作室 IDE.
的物理设备上运行
这是我的项目树,你可以看到 classes 和二进制文件:
PROJECT TREE.PNG
这是 ProcessorManager class 中的代码,您可以在评论中看到我尝试过的其他方法...
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;
}
输出:
E/login activity: File not found: java.io.FileNotFoundException:
mac_address_name: open failed: ENOENT (No such file or directory)
为此,您应该使用资产。
将文件移动到项目中的 src/main/assets 文件夹。
然后使用AssetsManager获取文件内容:
AssetManager am = context.getAssets();
InputStream is = am.open("mac_address_name");
更多信息请访问:
https://nbasercode.com/net/android-read-text-file-from-assets-folder-in-android-studio/
https://developer.android.com/reference/android/content/res/AssetManager
正如标题所说,我想读入一个已经创建的二进制文件“mac_address_name”。 该项目在连接到 android 工作室 IDE.
的物理设备上运行这是我的项目树,你可以看到 classes 和二进制文件:
PROJECT TREE.PNG
这是 ProcessorManager class 中的代码,您可以在评论中看到我尝试过的其他方法...
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;
}
输出:
E/login activity: File not found: java.io.FileNotFoundException: mac_address_name: open failed: ENOENT (No such file or directory)
为此,您应该使用资产。 将文件移动到项目中的 src/main/assets 文件夹。 然后使用AssetsManager获取文件内容:
AssetManager am = context.getAssets();
InputStream is = am.open("mac_address_name");
更多信息请访问: https://nbasercode.com/net/android-read-text-file-from-assets-folder-in-android-studio/ https://developer.android.com/reference/android/content/res/AssetManager