Android: writing text to txt

With the following code, I am trying to write to my SD card:

public void writedata(String data) { //BufferedWriter out = null; System.out.println(data); try{ FileOutputStream out = new FileOutputStream(new File("/sdcard/tsxt.txt")); out.write(data.getBytes()); out.close(); } catch (Exception e) { //fehlende Permission oder sd an pc gemountet} System.out.println("CCCCCCCCCCCCCCCCCCCCCCCALSKDJLAK"); } } 

Resolution in manifest:

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

But now, when I open the file, there is nothing. Where is the problem? I am sure that data has some meaning.

EDIT:

I get this message in LogCat:

 02-06 01:59:51.676: W/System.err(1197): java.io.FileNotFoundException: /storage/sdcard0/sdcard/tsxt.txt: open failed: ENOENT (No such file or directory) 

I tried to create the file on the SD card, but still the same error. Is there any code created by the file if it does not exist?

+4
source share
6 answers

Try using this code:

 File sdCard = Environment.getExternalStorageDirectory(); File dir = new File (sdCard.getAbsolutePath() + "/dir"); File file = new File(dir, "tsxt.txt"); FileOutputStream f = new FileOutputStream(file); 

Therefore, the file path is incorrect. You must remove the directory name:

 File dir = new File (sdCard.getAbsolutePath() + "/"); 
+5
source

Try the following:

 BufferedWriter out; try { FileWriter fileWriter= new FileWriter(Environment.getExternalStorageDirectory().getPath()+"/tsxt.txt") out = new BufferedWriter(fileWriter); out.write("Your text to write"); out.close(); }catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } 
+2
source
  try{ File myFile = new File("/sdcard/tsxt.txt"); myFile.createNewFile(); FileOutputStream fOut = new FileOutputStream(myFile); OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); myOutWriter.append("your data here"); myOutWriter.close(); fOut.close(); }catch(Exception e){} 
+1
source

try it

  FileOutputStream fOut =openFileOutput(Environment.getExternalStorageDirectory().getPath()+"/tsxt.txt",MODE_WORLD_READABLE); OutputStreamWriter osw = new OutputStreamWriter(fOut); //---write the string to the file--- osw.write(str); osw.flush(); osw.close(); 
0
source

Before writing any file to the SD card, you need to check that sdcard is installed or not, if not, just mount it and then write the file to it using an external storage path.

you can use the following code to check sdcard mount or not

 static public boolean hasStorage(boolean requireWriteAccess) { //TODO: After fix the bug, add "if (VERBOSE)" before logging errors. String state = Environment.getExternalStorageState(); Log.v(TAG, "storage state is " + state); if (Environment.MEDIA_MOUNTED.equals(state)) { if (requireWriteAccess) { boolean writable = checkFsWritable(); Log.v(TAG, "storage writable is " + writable); return writable; } else { return true; } } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { return true; } return false; } if(hasStorage(true)){ //call here your writedata() function } 
0
source

This code works just fine.

 public File getAlbumStorageDir(String albumName) { // Get the directory for the user public pictures directory. File file = new File(Environment.getExternalStorageDirectory() + "/files", albumName); Log.d("File", "Bug file Created" + file.getAbsolutePath()); return file; }` 

To write a text file inside the sd card (/storage/sdcard0/files/bugReport.txt)

  try{ outputStream = new FileOutputStream(this.getAlbumStorageDir(bugReport.txt)); outputStream.write(report.toString().getBytes()); Log.d("File","Report Generated"); outputStream.close(); } catch(Exception e){ e.printStackTrace(); } 
0
source

Source: https://habr.com/ru/post/1485027/


All Articles