Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

Can't read lines from a text file

I have a method like this:

    public int getIncrement() {

    String extractFolder = "/mnt/sdcard/MyFolder";
    boolean newFolder = new File(extractFolder).mkdir();
    int counter = 0; 

    try {
        File root = new File(extractFolder); 
        if (root.canWrite()){
            File gpxfile = new File(root, "gpxfile.txt");
            FileWriter gpxwriter = new FileWriter(gpxfile);
            BufferedWriter out = new BufferedWriter(gpxwriter);

            Scanner scanner = new Scanner(new FileInputStream(gpxfile.getAbsolutePath()), "UTF-8");
            Log.i("PATH: ", extractFolder + "/gpxfile.txt");

           while(scanner.hasNextLine()) {
               String inc = scanner.nextLine(); 
               counter = Integer.parseInt(inc);
               Log.i("INSIDE WHILE: ", Integer.toString(counter)); 
           }
            counter++;
            out.write(Integer.toString(counter));
            out.close();
        }
    } catch (IOException e) {
        Log.e("GEN_PCN: ", "Could not write file " + e.getMessage());
    }
    return counter;  
}

What I am trying to accomplish is returning the content of this file, and increment the integer by 1. But it seems that I can't get in the while loop, because LogCat doesn't print out anything. Yes, am sure that the path is correct.

Upvotes: 5

Views: 1180

Answers (3)

Chintan Raghwani
Chintan Raghwani

Reputation: 3370

OK I MARK YOUR FILE NAME

Just add a BACKSLASH in extractFolder at END

                                            v
                                            v
                                            v
String extractFolder = "/mnt/sdcard/MyFolder/";
                                            ^ // <---   HERE
                                            ^
                                            ^

Because File gpxfile = new File(root, "gpxfile.txt"); doesn't have BackSlash / as Log have


Just try Once Following:

while(scanner.hasNextLine()) {
      String inc = scanner.nextLine(); 
      //       //   counter = Integer.parseInt(inc);
      Log.i("INSIDE WHILE: ", inc);
      System.out.println("Next Line ::"+inc);  //  Also check this
}

Upvotes: 0

Mike
Mike

Reputation: 2409

From what I can tell the file doesn't exist. Try adding gpxfile.createNewFile()

To get a little more in depth, creating a File instance, does not create a file on the file system.

So, this line -> File gpxfile = new File(path, filename);

is not sufficient to create the file on the sd card. You must follow it with

gpxfile.createNewFile() which quoting the docs says:

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

Upvotes: 1

Erich Kitzmueller
Erich Kitzmueller

Reputation: 36977

I guess the constructor of FileWriter gpxwriter has already blanked out the file by the time the Scanner is created, so the file is empty and hasNextLine returns false. Why do you open a file for writing when you want to read it?

Upvotes: 2

Related Questions