使用文件:读取和写入多次 [英] Working with files: read and write many times

查看:142
本文介绍了使用文件:读取和写入多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android开发新手,我无法知道与文件去。

I am newbie in Android development and I cant know that is going with files.

这是关于音频信息。

所以,如果我创建新的文件,并在其上​​写后,我从这个文件中读取。
而新的迭代:我不创建新的文件(因为我已经获得了该文件),并在此写入文件。现在我该怎么从文件(第二次写操作之后),我可以从文件中读取获得?

So, if I create new file and write in it, after it i read from this file. And new iteration: I don't create new file (cuz i already got this file), and write in this file. Now I gonna read from file (after second write) that I can get from file?

我需要得到第二次书面资料,我可以得到它在这条路上?

I need get second written information, can I get it on this way?

推荐答案

是的,您已经阅读再次文件
让说你用文本文件来保存数据。

yes, you have read again the file let say you use a textfile to save your data. Ex.

这是我如何保存

    File temf=new File(getCacheDir()+"/data/mytext.txt");
    Writer out=null;
    if(temf.exists()){
        temf.delete();
    }
    try {
        out = new BufferedWriter(new FileWriter(temf));
        out.write("sample text") + "\r\n");
        }
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

和这是我如何找回它

    FileInputStream fs;
    try {
        File temf=new File(getCacheDir()+"/data/mytext.txt");
        if (temf.exists()) {
            fs = new FileInputStream(temf);
            DataInputStream dis = new DataInputStream(fs);
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    dis));
            String str=br.readLine();
            while(str!=null)
            {
            if(str=="text you want to compare"){
              break;
             }else{
              str=br.readLine();
             }
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

这篇关于使用文件:读取和写入多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆