1

Possible Duplicate:
android reading from a text file

So I need to load text, but I don't know how :( To save text I'm doing this

File logFile = new File("sdcard/data/agenda.file");
if (!logFile.exists())
{
    try
    {
        logFile.createNewFile();
    } 
    catch (IOException e)
    {

        e.printStackTrace();
    }
}
try
{
    //BufferedWriter for performance, true to set append to file flag
    BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
    buf.append(editText1.getText());
    buf.newLine();
    buf.close();
}
catch (IOException e)
{
    e.printStackTrace();
}

So, how to load it back by button tap?

Community
  • 1
  • 1
yota9
  • 37
  • 1
  • 3
  • 8
  • 2
    The tiniest amount of searching this forum itself will provide you with your answer. – Raghav Sood Sep 30 '12 at 19:19
  • I searched it for 2 or 3 hours, really. I found how to write in file, but did not find how to load-all what I found was not working :( – yota9 Sep 30 '12 at 19:32

3 Answers3

2

To read content of file, for example *.txt - do this...

private String GetPhoneAddress() {
    File file = new File(Environment.getExternalStorageDirectory() + "/reklama/tck.txt");
    if (!file.exists()){
        String line = "Need to add smth";
        return line;
    }
    String line = null;
    //Read text from file
    //StringBuilder text = new StringBuilder();
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        line = br.readLine(); 
        }
    catch (IOException e) {
        //You'll need to add proper error handling here
    }
    return line;
}

Then, from activite to set to textview - just do smthing like

final TextView tvphone = (TextView) findViewById(R.id.saved_phone);
    String saved_phone = GetPhoneAddress();
    if (saved_phone.length()>0){
       tvphone.setText(saved_phone);
    }
timonvlad
  • 1,046
  • 3
  • 13
  • 31
  • It works for me with 100%!!!!!!!)))) – timonvlad Sep 30 '12 at 20:05
  • so, please look what I have made-It doesn't work :(( http://pastebin.com/9RYhZh4f – yota9 Sep 30 '12 at 21:18
  • Ok))) is there any error in Log when you press Button??? – timonvlad Oct 01 '12 at 06:10
  • In your code you determine private String readf() in Activity @onCreate.... Put it out after determing of buttons and etc... In your code String read = readf(); editText1.setText(read); } }); – timonvlad Oct 01 '12 at 06:12
  • after it there is case ")" - after it you can determine any methods you use in onClickk... – timonvlad Oct 01 '12 at 06:13
  • OR just use Debug.... in your code - place where you want to stop step by step to see what's wrong - place android.os.Debug.WaitforDebug(); – timonvlad Oct 01 '12 at 06:14
  • Then by F5 and F6 look what is happening... – timonvlad Oct 01 '12 at 06:14
  • Sorry for delay. String readf() is not in onCreate, i just not copied } sorry. So, please look at my code http://pastebin.com/YFq9ysgF File is not empty. If I wrote it wile program is runnig text in button is just disappearing – yota9 Oct 03 '12 at 07:45
  • USE DEBUG to find your problem!!! something is wrong with your FULL CODE!!!!!!!!!!!! – timonvlad Oct 03 '12 at 13:42
1

This function will read your whole file, and set it to the parameter TextView as text, if this is what you want. Your code is trying to write the TextViews content to a file, it's not reading it.

public void loadToTextView(TextView textView) throws Exception
{
    String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    File file = new File(path, "filename.file");
    textView.setText(new Scanner(file).useDelimiter("\\Z").next());
}

Be careful, you will need to handle the Exception, that this function might throw.

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
1

This method will read each line into a StringBuffer.

Then just call setText(contentsOfFile) on your TextView.

BufferedReader fileReader = new BufferedReader(new FileReader("/mnt/sdcard/agenda.file"));

StringBuilder strBuilder = new StringBuilder();

String line;
while((line = fileReader.readLine()) != null)
{
    strBuilder.append(line);
}

fileReader.close();

strBuilder.trimToSize();

String contentsOfFile = strBuilder.toString();
mrres1
  • 1,147
  • 6
  • 10
  • Code here http://pastebin.com/crBc1VbW (This code made eclipse with mistakes). – yota9 Sep 30 '12 at 19:50
  • You should not hard code the sdcard path, and /sdcard/ is definitely wrong. Use Environment.getExternalStorageDirectory(); – Simon Sep 30 '12 at 19:55