skip to Main Content

i have a folder inside com.example.myapplication called files inside it there is a file text file called users how to read and write in java android studio

i tried bufferread read = new bufferread(new fileread(filename)))

2

Answers


  1. Here is an example to read a file:

    FileInputStream in = openFileInput("filename.txt");
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
          sb.append(line).append("n");
    }
    inputStreamReader.close();
    

    This to write to a file instead:

    String text;
    FileOutputStream out = openFileOutput("filename.txt", MODE_PRIVATE);
    out.write(text.getBytes());
    out.close();
    
    Login or Signup to reply.
  2. How to read and write to a text file in Android Studio

    Here’s how you can read and write to a text file called "users" inside the "files" folder of your "com.example.myapplication" project in Android Studio:

    Reading:

    1. Get the file path:
    String filename = "users";
    File file = new File(getFilesDir(), filename);
    
    1. Create a BufferedReader:
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null) {
            // Process each line of text
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    Writing:

    1. Get the file path:
    String filename = "users";
    File file = new File(getFilesDir(), filename);
    
    1. Create a BufferedWriter:
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(file));
        writer.write("This is a line of text");
        writer.newLine();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    Note:

    The getFilesDir() method gets the directory where your app can store private files.
    Use try-with-resources block to automatically close the reader and writer.
    You can read and write multiple lines of text using a loop.
    Make sure to handle exceptions properly.
    Additional resources:

    Android developer documentation on file storage: https://developer.android.com/reference/java/io/File
    Stack Overflow question on reading and writing to a file: Read/Write String from/to a File in Android

    Regarding your previous attempt:

    The code you provided, bufferread read = new bufferread(new fileread(filename))), is incorrect. It should be:

    BufferedReader reader = new BufferedReader(new FileReader(filename));
    

    You misspelled BufferedReader and used fileread instead of FileReader. Remember, class names are case-sensitive in Java.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search