java-向一个文件中写入多个String而不删除浏览器的第一个ANDROID收藏夹
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-向一个文件中写入多个String而不删除浏览器的第一个ANDROID收藏夹,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3829字,纯文字阅读大概需要6分钟。
内容图文
![java-向一个文件中写入多个String而不删除浏览器的第一个ANDROID收藏夹](/upload/InfoBanner/zyjiaocheng/690/e784c7de694947dfafba8b70efba7dd0.jpg)
我正在为学校制作一个简单的浏览器,并且正在尝试添加收藏夹.此处的代码将收藏夹添加到文件中(以便在应用关闭后可以保留它),并将其显示在TextView中.我的问题是它只能保存一个.如果我添加第二个,则替换第一个.我以为我可以将它们添加到array或arrayList(或任何可行的方法中,我乐于接受建议),但我无法成功.谢谢您的帮助.
package com.example.browser3;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Favorite extends Activity {
EditText etName;
EditText etAdress;
Button bAdd;
TextView tvDisplay;
protected void onResume() {
readFile("favorite.txt", tvDisplay);
super.onResume();
}
public void writeFile(String fileName, EditText v, EditText x){
try {
OutputStreamWriter out=new OutputStreamWriter(openFileOutput(fileName,0));
out.write(v.getText().toString()+ x.getText().toString());
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readFile(String fileName, TextView w){
try {
InputStream in=openFileInput(fileName);
if(in!=null){
InputStreamReader reader= new InputStreamReader(in);
BufferedReader buffreader= new BufferedReader(reader);
StringBuilder builder= new StringBuilder();
String str;
while((str=buffreader.readLine())!=null){
builder.append(str+ "\n");
}
in.close();
w.setText(builder.toString());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite);
etName = (EditText) findViewById(R.id.etName);
etAdress = (EditText) findViewById(R.id.etAdress);
bAdd = (Button) findViewById(R.id.bAdd);
tvDisplay = (TextView) findViewById(R.id.tvDisplay);
bAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
writeFile("favorite.txt",etName, etAdress);
readFile("favorite.txt", tvDisplay);
}
});
}
}
解决方法:
在第一个位置写,即前置,然后您需要使用它
private void writeToFile(Context context,String data){
try{
String path=context.getFilesDir().getAbsolutePath();
File file = new File(path + File.separator + fileName);
RandomAccessFile rf = new RandomAccessFile(file,"rws");
file.getParentFile().mkdirs();
Log.d("creating file path",path);
byte[] text = new byte[(int) file.length()];
rf.readFully(text);
rf.seek(0);
rf.writeBytes(data);
rf.write(text);
Log.d("write","writing file...");
rf.close();
}catch(Exception e){e.printStackTrace(); Log.d("caught", "data wititng fail");}
}
如果你想追加使用这个
private void writeToFile(Context context,String data){
try{
String path=context.getFilesDir().getAbsolutePath();
File file = new File(path + File.separator + fileName);
RandomAccessFile rf = new RandomAccessFile(file,"rws");
file.getParentFile().mkdirs();
Log.d("creating file path",path);
byte[] text = new byte[(int) file.length()];
rf.readFully(text);
rf.seek(0);
rf.write(text);
rf.writeBytes(data);
Log.d("write","writing file...");
rf.close();
}catch(Exception e){e.printStackTrace(); Log.d("caught", "data wititng fail");}
}
或者,您可以在MODE_APPEND模式下打开文件..以追加模式打开文件时,请更改为此OutputStreamWriter out = new OutputStreamWriter(openFileOutput(fileName,true));
内容总结
以上是互联网集市为您收集整理的java-向一个文件中写入多个String而不删除浏览器的第一个ANDROID收藏夹全部内容,希望文章能够帮你解决java-向一个文件中写入多个String而不删除浏览器的第一个ANDROID收藏夹所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。