Read text file from phone memory in android

I just want to create a text file in the phone’s memory and read its contents for display. Now I have created a text file. But it is not in the data / data / package-name / file name.txt path, and it did not display the content on the emulator.

My code ...

public class PhonememAct extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv=(TextView)findViewById(R.id.tv);

        FileOutputStream fos = null;
        try {
            fos = openFileOutput("Test.txt", Context.MODE_PRIVATE);
        } catch (FileNotFoundException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        try {
            fos.write("Hai..".getBytes());
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        FileInputStream fis = null;
        try {
            fis = openFileInput("Test.txt");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int c;

        try {
            while((c=fis.read())!=-1)
                    {
                        tv.setText(c);
                        setContentView(tv);

                        //k += (char)c;
                    }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }
}

Thanks in adv.

+2
source share
5 answers

You do not need to use input / output streams if you are just trying to write / read text.

Use FileWriter to write text to a file and BufferedReader to read text from a file - it's a lot easier. This works great ...

try {
    File myDir = new File(getFilesDir().getAbsolutePath());
    String s = "";

    FileWriter fw = new FileWriter(myDir + "/Test.txt");
    fw.write("Hello World");
    fw.close();

    BufferedReader br = new BufferedReader(new FileReader(myDir + "/Test.txt"));
    s = br.readLine();

    // Set TextView text here using tv.setText(s);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();

}

+6
source
    //Find the directory for the SD Card using the API
    //*Don't* hardcode "/sdcard"
    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard,"file.txt");

    //Read text from file
    StringBuilder text = new StringBuilder();

    try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
    text.append(line);
    text.append('\n');
    }
    }
    catch (IOException e) {
    //You'll need to add proper error handling here
    }

    //Find the view by its id
    TextView tv = (TextView)findViewById(R.id.text_view);

    //Set the text
    tv.setText(text);
+1
source
//To read file from internal phone memory
//get your application context:
Context context = getApplicationContext(); 

filePath = context.getFilesDir().getAbsolutePath();
File file = new File(filePath, fileName);
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));   
    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {

}
return text.toString(); //the output text from file.
+1

.
, try-catch. , openFileInput() , fos.write() fos.close() .

fis.read() fis.close().

openFileInput(), fos.write() fos.close() try-catch. "fis".

!

0

.

    public static void persistAll(Context ctx, List<myObject> myObjects) {

    // save data to file
    FileOutputStream out = null;

    try {
           out = ctx.openFileOutput("file.obj",

           Context.MODE_PRIVATE);
           ObjectOutputStream objOut = new ObjectOutputStream(out);
           objOut.writeObject(myObjects);
           } catch (Exception e) {
        e.printStackTrace();
           } finally {
           try {
               out.close();
           } catch (IOException e) {
               e.printStackTrace();
           } 
       }
   }

. , Java IDE . , !

0

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


All Articles