写入文件并检索文件的内容

时间:2012-01-23 10:59:32

标签: android file

Hii我一直在尝试检索文件的内容。我的目的是将一些内容写入文件,并在单击按钮时将其作为文本视图返回。我写了一些代码,但不幸的是,它没有用。任何人都可以帮助我。谢谢你的推荐

public class Writing_to_fileActivity extends Activity {
    /** Called when the activity is first created. */
    public EditText edit;
    public Button btn, read;
    public TextView tv;
    public String str;
    public FileOutputStream fOut;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        edit = (EditText) findViewById(R.id.edit);
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                str = edit.getText().toString();
                try {
                    File sdcard = Environment.getExternalStorageDirectory();
                     fOut= openFileOutput("Bharath.txt",
                            MODE_WORLD_WRITEABLE);                      
                    OutputStreamWriter osw = new OutputStreamWriter(fOut);                      
                    osw.write(str);                     
                    osw.flush();                        
                    osw.close();                                            
                } catch (IOException e) {
                    e.printStackTrace();
                }   
            }       
        }); 
        read =(Button)findViewById(R.id.read);
        read.setOnClickListener(new OnClickListener() {     
            public void onClick(View v) {
                StringBuilder text = new StringBuilder();
                try {
                    BufferedReader br = new BufferedReader(new FileReader(fOut));
                    String line;
                    while ((line = br.readLine()) != null) {
                        text.append(line);
                        text.append('\n');
                    }
                } catch (IOException e) {
                }
                TextView tv = (TextView) findViewById(R.id.tet);
                tv.setText(text);
            }
        }); 
    }
}

3 个答案:

答案 0 :(得分:1)

再次将文件定义为

FileInputStream fIn;

并将文件输入流设置为您的文件

fIn=openFileInput("Bharath.txt");

然后将缓冲区读取器设置为从流中读取:

BufferedReader br = new BufferedReader(new FileReader(fIn));   

答案 1 :(得分:0)

这是我的应用程序中的代码希望此帮助

        File path = new File(Environment.getExternalStorageDirectory(),"/text");
        if (!path.exists()){path.mkdirs();} 

        try{
        File text = new File(path,"text.txt");
        FileWriter writer = new FileWriter(text);
        writer.append(edit.getText());
        writer.flush();
        writer.close();
                   }
               catch////

            File txt = new File(Environment.getExternalStorageDirectory(),"/text");
    File file = new File(txt,"text.txt");
    StringBuilder text = new StringBuilder();
        if(file.exists())    {   
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
                            tv.setText(Text);
            }
             //catch

答案 2 :(得分:0)

替换以下行......

BufferedReader br = new BufferedReader(new FileReader(fOut));

BufferedReader br = new BufferedReader(new FileReader("Bharath.txt"));

然后尝试......