How to check whether a file exists or not? If it does, read it, otherwise create it and write it ...

Tried to create a file like this:

File myF = new File(Environment.getDataDirectory(),"file.txt"); try { myF.createNewFile(); text.setText(myF.getAbsolutePath()); } catch (IOException e1) { text.setText(e1.toString()); } 

As a result, writes java.io.IOException: Permission denied

And in any case, when using the createNewFile() method, it createNewFile() out ...

After a long walk on an Internet, I found a solution to my problem ...

Initially, the following line should be added to the application's manifest file.

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 

Then everything happens like this:

 File myF = new File(Environment.getExternalStorageDirectory() + "/data/", "file.txt"); if (!myF.exists()) { try { String str = "0"; myF.createNewFile(); try { FileOutputStream fOut = openFileOutput(myF.getAbsolutePath(), 0); OutputStreamWriter outStr = new OutputStreamWriter(fOut); outStr.write(str); outStr.flush(); outStr.close(); } catch (Throwable t) { Toast.makeText(getApplicationContext(), "Exception: " + t.toString(), Toast.LENGTH_LONG).show(); } } catch (IOException e1) { text.setText(e1.toString()); } } else { text.setText("Файл есть!"); } 

But now it comes out:

java.lang.IllegalArgumentException: File mnt // sdcard // data // file.txt contains a path separator

Polaziv found that " openFileOutput can not accept the path, but only the file name .."

But then how will the file be created?

    2 answers 2

     File file = new File(path); if (!file.exists()) { file.createNewFile() // и писать FileOutputStream fOut = openFileOutput(file.getAbsolutePath(), MODE_WORLD_READABLE); OutputStreamWriter osw = new OutputStreamWriter(fOut); osw.write("сюда писать инфу"); osw.flush(); osw.close(); } else { // читать FileInputStream fIn = openFileInput(file.getAbsolutePath()); InputStreamReader isr = new InputStreamReader(fIn); char[] inputBuffer = new char[bufSize]; isr.read(inputBuffer); String read = new String(inputBuffer); } 

    The MODE_WORLD_READABLE flag allows other applications to access the file with read permissions. There is also MODE_PRIVATE - only the creator has access, and MODE_WORLD_WRITEABLE - all applications can write to this file. Flags are defined in the Context class.

    How to read and write to file

    • Thank! And where the file will be created if you specify the following way File File = new File (path); file.createNewFile (); otherwise I can't find it on the smartphone ... - Leshij_2005
    • /data/data/package/files/file.txt I think - kENNAAAAA
    • I don’t have such a path at all, only the folder / data The file is not created at all ... Does not write and does not read .. does nothing ... - Leshij_2005
    • you most likely do not have rights to write this file. See logs. - kENNAAAAA
    • Where to see? What to watch? How to look? .. - Leshij_2005

    As a result, there was such a way out:

     File myF = new File(Environment.getExternalStorageDirectory()+"/data/","file.txt"); if(!myF.exists()) { try { String str = "500"; myF.createNewFile(); try { FileWriter fWr = new FileWriter(myF); fWr.write(str); fWr.flush(); fWr.close(); } catch (Throwable t) { Toast.makeText(getApplicationContext(), "Exception: " + t.toString(), Toast.LENGTH_LONG).show(); } text.setText("Бюджет - "+str+" р."); } catch (Exception e1) { text.setText(e1.toString()); } } else { try { FileReader fRd = new FileReader(myF); BufferedReader reader = new BufferedReader(fRd); String str; StringBuffer buffer = new StringBuffer(); while ((str = reader.readLine()) != null) { buffer.append(str + "\n"); } fRd.close(); text.setText("Бюджет - "+buffer.toString()+"р."); } catch (Throwable t) { Toast.makeText(getApplicationContext(),"Exception: " + t.toString(),Toast.LENGTH_LONG).show(); } } 

    Thank you all for your attention!

    • By the way, if you have an exception while recording, the stream will not close at all. This is not true. - angry