简体   繁体   中英

Reading a txt file, and displaying it in a textView on a different fragment/activity

I am trying to get the text from a file and apply it to a textView . However, I am being returned with the file path as shown below.

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
    tv = (TextView) getActivity().findViewById(R.id.clockText);
    // Displaying the user details on the screen
    try {
        getFileText();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


public void getFileText() throws IOException {
    File path = getActivity().getExternalFilesDir(null); //sd card
    File file = new File(path, "alarmString.txt"); //saves in Android/
    FileInputStream stream = new FileInputStream(file);
    try{
        stream.read();
        tv.setText(file.toString());
    } finally {
        stream.close();
    }
}

The result is being "Android/data/foldername/example/files/alarmString.txt" instead of the time declared by the user in a different activity for example: 18:05

public String getFileContent(File file) throws IOException {
    String str = "";
    BufferedReader bf = null;
    try {
        bf = new BufferedReader(new FileReader(file));
        while(bf.ready())
            str += bf.readLine();
    } catch (FileNotFoundException e){
        Log.d("FileNotFound", "Couldn't find the File");
    }  finally {
        bf.close();
    }
    return str;
}

Use a BufferedReader and FileReader instead of reading the bytes. You used

stream.read();

what gives you one byte of your file.

tv.setText(file.toString());

will set your TextView to the output of the file.toString() methods output and not to the files content.

You are setting file.toString which returns file path. If you want to set the data which is present in the file you need to read stream and append to stringbuffer in while loop until text gets over in file and finally set stringbuffer.toString to textview.

do it as follow

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
    tv = (TextView) getActivity().findViewById(R.id.clockText);
    // Displaying the user details on the screen
    try {
       tv.setText(getFileText());
    } catch (IOException e) {
        e.printStackTrace();
    }
}


public String getFileText() throws IOException {
    File path = getActivity().getExternalFilesDir(null); //sd card
    File file = new File(path, "alarmString.txt"); //saves in Android/
    BufferedReader br = new BufferedReader(new FileReader(file));
try {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }

} finally {
    br.close();
}
return sb.toString()
}

you have to return a string from getFileText function and then set that string to the text view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM