1

I have written simple code to save data into text files internally. But, after running the code, I don't know where I can find the required file. Additionally, I find an error message in the log cast as

"SPAN_EXCLUSIVE_EXCLUSIVE"

package com.example.saving_files;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

     File file;
     FileOutputStream fos;
     String FlieName = "output.text";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            file = new File(FlieName);
            try {
                fos = openFileOutput(FlieName, MODE_PRIVATE);
                fos.write(122);
            } catch (FileNotFoundException e) {
                Log.d("output", file.getAbsolutePath());
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            file = getFilesDir();
            Log.d("output_path", file.getAbsolutePath());
        }
    }
Simon Halsey
  • 5,459
  • 1
  • 21
  • 32
Mohsen Ali
  • 655
  • 1
  • 9
  • 30
  • possible duplicate of [Writing a file to sdcard](http://stackoverflow.com/questions/2455102/writing-a-file-to-sdcard) – jww Feb 18 '15 at 08:28

2 Answers2

0

Check this code and comments;

File sdcard = Environment.getExternalStorageDirectory();
File f = new File(sdcard, "/yourfile");

if (!f.exsist()) {
    f.createNewFile();
    // Use outwriter here, (outputstream) search how to write into a text file in java code 
}
Intrications
  • 16,782
  • 9
  • 50
  • 50
Tugrul
  • 1,760
  • 4
  • 24
  • 39
  • i update the code to be as shown and i put an ext_sdcard to my device .also i added the permissions to the manifest file to be readable and writable. – Mohsen Ali Jul 15 '13 at 15:15
0
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "filename");

FileOutputStream f = new FileOutputStream(file);

This code will create new file in the root directory of your SD. Dont forget to add <uses-permission> to write to SD in your manifest

Semyon Danilov
  • 1,753
  • 1
  • 17
  • 37