gpt4 book ai didi

java - 将多个字符串写入文件而不删除浏览器的第一个 ANDROID Collection 夹

转载 作者:太空宇宙 更新时间:2023-11-04 14:40:02 25 4
gpt4 key购买 nike

我正在为学校制作一个简单的浏览器,我正在尝试制作 Collection 夹。此处的代码将 Collection 夹添加到文件中(这样我可以在应用程序关闭后保留它)并将其显示在 TextView 中。我的问题是它只能保存一个。如果我添加第二个,第一个将被替换。我想我可以将它们添加到数组或数组列表中(或任何有效的东西,我愿意接受建议),但我无法成功。感谢您的帮助。

 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 - 将多个字符串写入文件而不删除浏览器的第一个 ANDROID Collection 夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25058187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com