chRyNaN
chRyNaN

Reputation: 3692

Adding file contents to an ArrayList; Android

What I am trying to accomplish is to read a file line by line and store each line into an ArrayList. This should be such a simple task but I keep running into numerous problems. At first, it was repeating the lines when it was saved back into a file. Another error which seems to occur quite often is that it skips the try but doesn't catch the exception? I have tried several techniques but no luck. If you have any advice or could provide help in anyway it would be greatly appreciated. Thank you

Current code:

try{
    // command line parameter
    FileInputStream fstream = new FileInputStream(file);
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;

    while ((strLine = br.readLine()) != null)   {
        fileList.add(strLine);
    }
    //Close the input stream
    in.close();
} catch (Exception e){//Catch exception if any
    Toast.makeText(this, "Could Not Open File", Toast.LENGTH_SHORT).show();
}
fileList.add(theContent);

//now to save back to the file
try {
    FileWriter writer = new FileWriter(file); 
    for(String str: fileList) { 
        writer.write(str);
        writer.write("\r\n");
    }
    writer.close();
} catch (java.io.IOException error) {
    //do something if an IOException occurs.
    Toast.makeText(this, "Cannot Save Back To A File", Toast.LENGTH_LONG).show();
}

Upvotes: 0

Views: 2969

Answers (2)

Matt Wolfe
Matt Wolfe

Reputation: 9284

Why do you have fileList.add(theContent) after the try/catch? I don't see what the point of that is. Remove that line and see if it helps.

Example, I just tested this code on my local machine (not android but should be the same)

import java.io.*;
import java.util.ArrayList;
class FileRead 
{
 public static void main(String args[])
  {
  ArrayList<String> fileList = new ArrayList<String>();
  final String file = "textfile.txt";
  final String outFile = "textFile1.txt";
  try{
      FileInputStream fstream = new FileInputStream(file);
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;

      //Read File Line By Line
      while ((strLine = br.readLine()) != null)   {
      // Print the content on the console
        fileList.add(strLine);
      }
      //Close the input stream
      in.close();
    } catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }

   try {
        FileWriter writer = new FileWriter(outFile); 
        for(String str: fileList) { 
          writer.write(str);
          writer.write("\r\n");
        }
        writer.close();
    } catch (java.io.IOException error) {
        System.err.println("Error: " + error.getMessage());
    }
  }
}

After I ran this the 2 files had no differences. So my guess is that line may have something to do with it.

Upvotes: 0

Erol
Erol

Reputation: 6526

There is a very simple alternative to what you are doing with Scanner class:

Scanner s = new Scanner(new File("filepath"));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
    list.add(s.next());
}
s.close();

Upvotes: 2

Related Questions