[英]How to read and write text to a file
I want to code a program which gets 3 textbox values and 1 global variable saved in a text document, however i want to append to the text document, instead of rewriting it each time! 我想编写一个程序,该程序将在文本文档中保存3个文本框值和1个全局变量,但是我想附加到文本文档中,而不是每次都重写它! This is what i have so far;
这是我到目前为止所拥有的;
public class Proceed extends Activity {
// Defining edittext variables and where they will be used
EditText edittext;
EditText edittext1;
EditText edittext2;
// Getting global variable to get price data from past activity
String PriceResult = Main2Activity.GSTFinal;
// Defining textview to show data
TextView tvResult2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_proceed);
// assigning variables from their respective EditText and textview box
edittext = (EditText) findViewById(R.id.Name);
edittext1 = (EditText) findViewById(R.id.Phone);
edittext2 = (EditText) findViewById(R.id.Address);
tvResult2 = (TextView) findViewById(R.id.tvResult2);
// Assigning tvresult what string to show
tvResult2.setText(PriceResult + "");
//
}
public void save(View view) {
String n1 = edittext.getText().toString();
String n2 = edittext1.getText().toString();
String n3 = edittext2.getText().toString();
String n4 = tvResult2.getText().toString();
String Writetotxtfile = n1 + "," + n2 + "," + n3 + "," + n4;
String filename = "myfile";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(Writetotxtfile.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
For Read file 对于读取文件
public String readFromFile() {
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,".YOURAPP/myFile.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
return text.toString();
}
For Write file 对于写文件
public void writeFile(String data){
try {
File logFile = new File(Environment.getExternalStorageDirectory().toString(), ".YOURAPP/myFile.txt");
File dirFile = new File(Environment.getExternalStorageDirectory().toString(), ".YOURAPP");
if(!logFile.exists()) {
dirFile.mkdir();
logFile.createNewFile();
}
BufferedWriter output = new BufferedWriter(new FileWriter(logFile));
output.write(data);
output.close();
}catch (Exception e){
System.out.print("Exception e"+e);
}
}
For Delete file 对于删除文件
public void deleteFile(){
File logFile = new File(Environment.getExternalStorageDirectory().toString(), ".YOURAPP/myFile.txt");
File dirFile = new File(Environment.getExternalStorageDirectory().toString(), ".YOURAPP");
if(logFile.exists()) {
logFile.delete();
dirFile.delete();
}
}
Add permission to menifest 向清单添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Instead of 代替
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
use 采用
outputStream = new FileOutputStream(filename, true);
This will ensure you append to file, not truncate it. 这将确保您追加到文件,而不是截断它。
What you are trying to do is called 你想做的事叫做
append
附加
FileOutputStream
has a Constructor
for appending. FileOutputStream
具有用于附加的Constructor
器。
Creates a file output stream to write to the file represented by the specified File object.
创建文件输出流以写入由指定File对象表示的文件。
FileOutputStream(File file, boolean append)
So use that constructor when instantiating your FileOutputStream. 因此,在实例化FileOutputStream时使用该构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.