Bel
Bel

Reputation: 312

Working with files: read and write many times

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

That's about audio information.

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?

Upvotes: 1

Views: 1785

Answers (1)

She Smile GM
She Smile GM

Reputation: 1322

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

this is how i save it

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

and this is how i retrieve it

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

Upvotes: 2

Related Questions