写多个字符串的文件,而不删除第一个ANDROID的收藏夹浏览器第一个、多个、而不、收藏夹

由网友(噯、伱卟蓜)分享简介:我想提出一个简单的浏览器上学,我试图做的最爱。这code在这里增加了一个最喜欢的一个文件(这样我就可以把它之后,应用程序被关闭),并在TextView中显示出来。我的问题是,它只能救一个。如果我添加第二个,第一个被替换。我以为我可以将其添加到一个数组或数组列表(或任何工作,我愿意接受建议),但我不能成功。感谢您的帮助。...

我想提出一个简单的浏览器上学,我试图做的最爱。这code在这里增加了一个最喜欢的一个文件(这样我就可以把它之后,应用程序被关闭),并在TextView中显示出来。我的问题是,它只能救一个。如果我添加第二个,第一个被替换。我以为我可以将其添加到一个数组或数组列表(或任何工作,我愿意接受建议),但我不能成功。感谢您的帮助。

 包com.example.browser3;

进口java.io.BufferedReader中;
进口java.io.BufferedWriter中;
进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.FileWriter;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口java.io.OutputStreamWriter中;
进口的java.util.ArrayList;
进口的java.util.List;

进口android.app.Activity;
进口android.content.Context;
进口android.os.Bundle;
进口android.view.View;
进口android.view.inputmethod.InputMethodManager;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;

公共类最喜欢的扩展活动{
的EditText etName;
的EditText etAdress;
按钮BADD;
TextView的tvDisplay;




保护无效onResume(){
    READFILE(favorite.txt,tvDisplay);
    super.onResume();
}

公共无效WRITEFILE(字符串文件名,EditText上V,EditText上X){

    尝试 {
        OutputStreamWriter OUT =新OutputStreamWriter(openFileOutput(文件名,0));
        out.write(。v.getText()的toString()+ x.getText()的toString());

        out.close();
    }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
}

公共无效READFILE(字符串文件名,TextView的W){
    尝试 {
        在的InputStream = openFileInput(文件名);
        如果(在!= NULL){
            InputStreamReader的读者=新的InputStreamReader(中);
            的BufferedReader buffreader =新的BufferedReader(读卡器);

            StringBuilder的建设者=新的StringBuilder();
            字符串str;
            而((海峡= buffreader.readLine())!= NULL){
                builder.append(STR + N);
            }
            附寄();
            w.setText(builder.toString());
        }
    }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
}

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.favorite);


    etName =(EditText上)findViewById(R.id.etName);
    etAdress =(EditText上)findViewById(R.id.etAdress);
    BADD =(按钮)findViewById(R.id.bAdd);
    tvDisplay =(TextView中)findViewById(R.id.tvDisplay);

    bAdd.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            WRITEFILE(favorite.txt,etName,etAdress);
            READFILE(favorite.txt,tvDisplay);
        }

    });

}
}
 

解决方案

要在第一的位置,即prePEND写那么在需要使用这个

 私人无效将writeToFile(上下文的背景下,字符串数据){
         尝试{
             字符串路径= context.getFilesDir()getAbsolutePath()。

             档案文件=新的文件(路径+文件分割符+文件名);
             RandomAccessFile的RF =新RandomAccessFile的(文件,RWS);
             file.getParentFile()mkdirs()。
                Log.d(创建文件路径,路径);

                byte []的文字=新的字节[(INT)file.length()];
                rf.readFully(文本);
                rf.seek(0);
                rf.writeBytes(数据);
                rf.write(文本);
                Log.d(写,写文件......);
                rf.close();




         }赶上(例外五){e.printStackTrace(); Log.d(中招,数据wititng失败);}
     }
 
android怎么安装apk文件,apk文件怎么安装 如何安装apk文件

和如果u要追加使用这种

 私人无效将writeToFile(上下文的背景下,字符串数据){
         尝试{
             字符串路径= context.getFilesDir()getAbsolutePath()。

             档案文件=新的文件(路径+文件分割符+文件名);
             RandomAccessFile的RF =新RandomAccessFile的(文件,RWS);
             file.getParentFile()mkdirs()。
                Log.d(创建文件路径,路径);

                byte []的文字=新的字节[(INT)file.length()];
                rf.readFully(文本);
                rf.seek(0);
                 rf.write(文本);
                rf.writeBytes(数据);

                Log.d(写,写文件......);
                rf.close();


         }赶上(例外五){e.printStackTrace(); Log.d(中招,数据wititng失败);}
     }
 

或U可以在MODE_APPEND模式打开文件。要在附加模式改变这种 OutputStreamWriter OUT =新OutputStreamWriter(openFileOutput(文件名,真))打开文件;

I am making a simple browser for school and I am trying to make the favorites. This code here adds a favorite to a file(so I can keep it after the app is closed) and displays it in the TextView. My problem is that it can only save one. If i add the second one, the first one is replaced. I thought i could add them in an array or arrayList(or anything that works, i am open to suggestions), but i can't succeed. Thanks for the help.

 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);
        }

    });

}
}

解决方案

to write at first position i.e. prepend then u need to use this

     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");} 
     }

and if u want to append use this

     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");} 
     }

or u can open file in MODE_APPEND mode.. to open file in append mode change to this OutputStreamWriter out=new OutputStreamWriter(openFileOutput(fileName,true));

阅读全文

相关推荐

最新文章