找不到文本文件
Fail to locate text file
我基本上是想从文本文件中读取一长串数字(双精度)并将它们保存到数组中。我有这些代码行,但当我加载到我的 android 智能手机时它不起作用。当我使用调试模式检查我的代码是否读取 ExamScore 时,readfile() 确实可以完全工作,它确实按预期读取并存储了笔记本电脑中的值。当它加载到智能手机中时,它就不起作用了。我把我的 ExamScore.txt 保存在 android studio 的根目录下,例如 Users->AndroidStudioProjects->Project A。我主要关心的是:
- 我如何知道这个 ExamScore.txt 在我构建应用程序时是否也保存到我的智能手机中?我必须将文本文件单独保存到我的智能手机中吗?我得到的错误是
java.io.FileNotFoundException: ExamScore.txt: 打开失败: ENOENT (没有那个文件或目录)
static double[] readfile() throws FileNotFoundException{
Scanner scorefile = new Scanner(new File("ExamScore.txt"));
int count = -1;
double[] score = new double[8641];
while (scorefile.hasNext()) {
count = count + 1;
score[count] = Double.parseDouble(scorefile.nextLine());
}
scorefile.close();
return score;
}
在我的主要代码中,
double []score=readfile();
I save my ExamScore.txt in the root directory of android studio, for example, Users->AndroidStudioProjects->Project A... How do I know if this ExamScore.txt is saved into my smartphone as well when I build the app?
不是。
您需要创建一个资产文件夹。
参考:Where do I place the 'assets' folder in Android Studio?
并且您将使用 getAssets()
从该文件夹中读取。
public class MainActivity extends Activity {
private double[] readfile() throws FileNotFoundException{
InputStream fileStream = getAssets().open("ExamScore.txt");
// TODO: read an InputStream
}
}
注意:这是您应用的只读位置。
或者您可以使用内置SD卡。
How do I read the file content from the Internal storage - Android App
编辑 在其他答案中使用重构代码
public static List<Double> readScore(Context context, String filename) {
List<Double> scores = new ArrayList<>();
AssetManager mgr = context.getAssets();
try (
BufferedReader reader = new BufferedReader(
new InputStreamReader(mgr.open(fileName)));
) {
String mLine;
while ((mLine = reader.readLine()) != null) {
scores.add(Double.parseDouble(mLine));
}
} catch (NumberFormatException e) {
Log.e("ERROR: readScore", e.getMessage());
}
return scores;
}
然后
List<Double> scores = readScore(MainActivity.this, "score.txt");
对于那些想知道的人,这是我的解决方案!感谢大家的帮助!!!!我遇到的问题是我没有将它写在主 activity 中,而是将代码写在其他 java 文件中。在主 activity 文件中写入此内容并将我的文本文件放入 assets 文件夹后。问题已解决:
public static LinkedList<Double> score=new LinkedList<Double>();
public void readScore() throws java.io.IOException {
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("score.txt")));
String mLine;
while ((mLine = reader.readLine()) != null) {
score.add(Double.parseDouble(mLine));
}
reader.close();
}
我基本上是想从文本文件中读取一长串数字(双精度)并将它们保存到数组中。我有这些代码行,但当我加载到我的 android 智能手机时它不起作用。当我使用调试模式检查我的代码是否读取 ExamScore 时,readfile() 确实可以完全工作,它确实按预期读取并存储了笔记本电脑中的值。当它加载到智能手机中时,它就不起作用了。我把我的 ExamScore.txt 保存在 android studio 的根目录下,例如 Users->AndroidStudioProjects->Project A。我主要关心的是:
- 我如何知道这个 ExamScore.txt 在我构建应用程序时是否也保存到我的智能手机中?我必须将文本文件单独保存到我的智能手机中吗?我得到的错误是
java.io.FileNotFoundException: ExamScore.txt: 打开失败: ENOENT (没有那个文件或目录)
static double[] readfile() throws FileNotFoundException{
Scanner scorefile = new Scanner(new File("ExamScore.txt"));
int count = -1;
double[] score = new double[8641];
while (scorefile.hasNext()) {
count = count + 1;
score[count] = Double.parseDouble(scorefile.nextLine());
}
scorefile.close();
return score;
}
在我的主要代码中,
double []score=readfile();
I save my ExamScore.txt in the root directory of android studio, for example, Users->AndroidStudioProjects->Project A... How do I know if this ExamScore.txt is saved into my smartphone as well when I build the app?
不是。
您需要创建一个资产文件夹。
参考:Where do I place the 'assets' folder in Android Studio?
并且您将使用 getAssets()
从该文件夹中读取。
public class MainActivity extends Activity {
private double[] readfile() throws FileNotFoundException{
InputStream fileStream = getAssets().open("ExamScore.txt");
// TODO: read an InputStream
}
}
注意:这是您应用的只读位置。
或者您可以使用内置SD卡。
How do I read the file content from the Internal storage - Android App
编辑 在其他答案中使用重构代码
public static List<Double> readScore(Context context, String filename) {
List<Double> scores = new ArrayList<>();
AssetManager mgr = context.getAssets();
try (
BufferedReader reader = new BufferedReader(
new InputStreamReader(mgr.open(fileName)));
) {
String mLine;
while ((mLine = reader.readLine()) != null) {
scores.add(Double.parseDouble(mLine));
}
} catch (NumberFormatException e) {
Log.e("ERROR: readScore", e.getMessage());
}
return scores;
}
然后
List<Double> scores = readScore(MainActivity.this, "score.txt");
对于那些想知道的人,这是我的解决方案!感谢大家的帮助!!!!我遇到的问题是我没有将它写在主 activity 中,而是将代码写在其他 java 文件中。在主 activity 文件中写入此内容并将我的文本文件放入 assets 文件夹后。问题已解决:
public static LinkedList<Double> score=new LinkedList<Double>();
public void readScore() throws java.io.IOException {
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("score.txt")));
String mLine;
while ((mLine = reader.readLine()) != null) {
score.add(Double.parseDouble(mLine));
}
reader.close();
}