Can't read lines from a text file

时间:2023-02-06 12:20:33

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.

我想要完成的是返回此文件的内容,并将整数增加1.但似乎我无法进入while循环,因为LogCat不打印任何内容。是的,我确信路径是正确的。

3 个解决方案

#1


2  

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?

我想FileWriter gpxwriter的构造函数在创建Scanner时已经消隐了文件,因此该文件为空并且hasNextLine返回false。为什么要在阅读时打开文件进行写作?

#2


1  

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

从我可以告诉该文件不存在。尝试添加gpxfile.createNewFile()

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

为了更深入地创建File实例,不会在文件系统上创建文件。

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

那么,这一行 - >文件gpxfile = new File(path,filename);

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

不足以在SD卡上创建文件。你必须遵循它

gpxfile.createNewFile() which quoting the docs says:

引用文档的gpxfile.createNewFile()说:

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.

当且仅当具有此名称的文件尚不存在时,以原子方式创建由此抽象路径名命名的新空文件。检查文件是否存在以及文件的创建(如果不存在)是针对可能影响文件的所有其他文件系统活动的原子操作。

#3


0  

OK I MARK YOUR FILE NAME

好的,我标记你的文件名

Just add a BACKSLASH in extractFolder at END

只需在END的extractFolder中添加一个BACKSLASH即可

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

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

因为File gpxfile = new File(root,“gpxfile.txt”);没有BackSlash /作为日志


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
}

#1


2  

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?

我想FileWriter gpxwriter的构造函数在创建Scanner时已经消隐了文件,因此该文件为空并且hasNextLine返回false。为什么要在阅读时打开文件进行写作?

#2


1  

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

从我可以告诉该文件不存在。尝试添加gpxfile.createNewFile()

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

为了更深入地创建File实例,不会在文件系统上创建文件。

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

那么,这一行 - >文件gpxfile = new File(path,filename);

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

不足以在SD卡上创建文件。你必须遵循它

gpxfile.createNewFile() which quoting the docs says:

引用文档的gpxfile.createNewFile()说:

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.

当且仅当具有此名称的文件尚不存在时,以原子方式创建由此抽象路径名命名的新空文件。检查文件是否存在以及文件的创建(如果不存在)是针对可能影响文件的所有其他文件系统活动的原子操作。

#3


0  

OK I MARK YOUR FILE NAME

好的,我标记你的文件名

Just add a BACKSLASH in extractFolder at END

只需在END的extractFolder中添加一个BACKSLASH即可

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

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

因为File gpxfile = new File(root,“gpxfile.txt”);没有BackSlash /作为日志


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
}