Java 如何从文本文件中读取数据,然后用新数据覆盖它?
我是android studio的新手,我有一个文本视图,它显示存储在我的文本文件中的数据。如果我单击按钮,它应该读取文本文件中的数据,添加整数,总和应该替换文本文件中的现有数据。但是,当我返回到显示文本文件中包含新数据的文本视图的活动时,它不会改变 这是我的文本视图的代码Java 如何从文本文件中读取数据,然后用新数据覆盖它?,java,android,android-studio,Java,Android,Android Studio,我是android studio的新手,我有一个文本视图,它显示存储在我的文本文件中的数据。如果我单击按钮,它应该读取文本文件中的数据,添加整数,总和应该替换文本文件中的现有数据。但是,当我返回到显示文本文件中包含新数据的文本视图的活动时,它不会改变 这是我的文本视图的代码 txt_stars = (TextView) findViewById(R.id.txtStars); try { FileInputStream fileInput
txt_stars = (TextView) findViewById(R.id.txtStars);
try {
FileInputStream fileInputStream = openFileInput("Stars.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
String star;
while ((star=bufferedReader.readLine()) != null){
stringBuffer.append(star);
}
txt_stars.setText(stringBuffer.toString());
}
catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
还有按钮的代码
Integer stars, totalStars;
public void onClick(DialogInterface dialog, int whichButton) {
try {
FileInputStream fileInputStream = openFileInput("Stars.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
String star;
while ((star = bufferedReader.readLine()) != null) {
stringBuffer.append(star);
}
stars = Integer.parseInt(stringBuffer.toString());
totalStars = stars + 50;
//is this acceptable?
FileOutputStream fileOutputStream = openFileOutput("Stars.txt", MODE_PRIVATE);
fileOutputStream.write(totalStars.toString().getBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Intent nextForm = new Intent(".MainActivity");
startActivity(nextForm);
}
此外,我在手机中的何处可以找到创建的文本文件,以便确保该文本文件已创建?我正在使用Android studio 1.5.1,并在手机上运行该应用程序
我的清单文件里有这个
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我应该做些什么来定位和创建文本文件
我被困在这里好几天了。请帮帮我。
非常感谢 在开始新活动之前,您是否尝试过更新textview?例如:
txt_start.setText(""+totalStars);
这可能是由于在save方法中读取文件时出现了
FileNotFoundException
。当您尝试更新文件并将其写入时,不会分离try/catch
方法,因此在读取部分可能会出现异常,从而阻止继续脚本和更新文件。可能这是在Logcat中打印的,但您还没有看一眼。因此,我建议您检查该文件是否已经存在,并将读/写部分分开 当您第一次读取文件以在
TextView
中显示它时,只需检查它是否是为避免后台异常而创建的:
File f = new File(getFilesDir().toString() + "/stars.txt");
Log.v("", "Does the file exist? " + f.exists());
if (f.exists()) {
readFile();
}
您可以在这里看到文件应该存储在哪里(在getFilesDir()
中)。当您使用openFileInput(String)
时,默认情况下会选择路径,路径为:
data/data/com.package.name/files/
您必须记住,您实际上并没有在代码中创建/files
文件夹,例如,我必须自己创建它才能使用上述方法。(这只能是我的设备,但您应该知道这会阻止创建文件。)
现在,你的阅读方法没有大的变化,看起来是这样的:
private void readFile() {
try {
FileInputStream file = openFileInput("stars.txt");
InputStreamReader inRead = new InputStreamReader(file);
BufferedReader buffReader = new BufferedReader(inRead);
StringBuffer stringBuffer = new StringBuffer();
String star;
while ((star = buffReader.readLine()) != null) {
stringBuffer.append(star);
}
inRead.close();
file.close();
txt_stars.setText(stringBuffer.toString());
} catch (Exception e) {
Log.e("ReadFile", e.toString());
}
}
显然,只有在前面的检查返回true
时才会调用此方法。单击按钮时,您必须执行相同的操作:检查它是否存在->如果存在,获取内容,完成您的工作(添加、求和等)并写入->如果不存在,只需通过写入来创建它。
以下几点会起作用:
public void writeFile(View v) {
File f = new File(getFilesDir().toString() + "/stars.txt");
Log.v("", "Does it exist? " + f.exists());
String result = "";
if ( f.exists() ) {
try {
FileInputStream file = openFileInput("stars.txt");
InputStreamReader inRead = new InputStreamReader(file);
BufferedReader buffReader = new BufferedReader(inRead);
StringBuffer stringBuffer = new StringBuffer();
String star;
while ((star=buffReader.readLine()) != null) {
stringBuffer.append(star);
}
result = stringBuffer.toString();
inRead.close();
file.close();
} catch (Exception e) {
Log.e("WriteFile", "--- Error on reading file: "+e.toString());
}
} else {
// get the user's star or whatever
result = editRating.getText().toString();
}
Log.v("WriteFile", "--- Read file returns: " + result);
stars = Integer.parseInt(result);
totalStars = stars + 50;
try {
FileOutputStream fileOut = openFileOutput("stars.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter = new OutputStreamWriter(fileOut);
outputWriter.write(String.valueOf(totalStars));
outputWriter.close();
fileOut.close();
// display file saved message
Toast.makeText(getBaseContext(), "File saved", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e(" WriteFile", e.toString());
}
}
此时,当您返回到上一个活动时,您应该能够看到更改
但是,为了查看存储中的文件,不幸的是,您必须拥有根设备,否则。最后,避免重新启动活动。您应该完成编辑,这将返回到上一个,您只需在onResume()
中调用readFile()
,而不是onCreate()
。它会将新内容更新到TextView