java-向一个文件中写入多个String而不删除浏览器的第一个ANDROID收藏夹

java-向一个文件中写入多个String而不删除浏览器的第一个ANDROID收藏夹,第1张

概述我正在为学校制作一个简单的浏览器,并且正在尝试添加收藏夹.此处的代码将收藏夹添加到文件中(以便在应用关闭后可以保留它),并将其显示在TextView中.我的问题是它只能保存一个.如果我添加第二个,则替换第一个.我以为我可以将它们添加到array或arrayList(或任何可行的方法中,我乐于

我正在为学校制作一个简单的浏览器,并且正在尝试添加收藏夹.此处的代码将收藏夹添加到文件中(以便在应用关闭后可以保留它),并将其显示在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();    }}@OverrIDeprotected 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收藏夹所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1093184.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存