扫描仪通过从文件中扫描添加空行

Bak*_*915 0 java arraylist java.util.scanner

我试图使用扫描仪从文件读入然后将这些附加到数组.但是当我这样做时,扫描仪似乎正在拾取空行并将这些空白行分配给数组中的索引.

我尝试了几种不同的工作方式无济于事,只是想知道是否有任何问题和新问题为什么会这样做?

该文件的格式如下:

F:\ DATA\SFW3 \文件夹

F:\ DATA\SFW3 \文件夹

F:\ DATA\SFW3 \文件夹

F:\ DATA\SFW3 \文件夹

任何想法都会受到欢迎.

public void scanFiles() throws NoSuchElementException {
    Scanner sc = null;
    System.out.println("Sage 2015 is Installed on this machine");
    int i = 0;

    try {
        String line;
        File companyFile = new File(sageFolders[8] + "\\COMPANY"); 
        sc = new Scanner(new BufferedReader(new FileReader(companyFile)));

        while(sc.hasNextLine()) {
            line = sc.nextLine();
            System.out.println(line);
            System.out.println(i);
            currentFolders.add(i,line);
            System.out.println("At Index" + i + ": " + currentFolders.get(i));
            i++; 
        }

        sc.close();    
    }
    catch(FileNotFoundException e) {
        System.out.println("File not Found: Moving onto next Version"); 
    }
    catch(IOException e) {
        System.out.println("IO Error");
    }
}
Run Code Online (Sandbox Code Playgroud)

Jen*_*ens 6

使用String.trim().length()以找出是否是一个空行,如果不是不加它

System.out.println(i);

if (String.trim().length() > 0) {
    currentFolders.add(i,line);
    System.out.println("At Index" + i + ": " + currentFolders.get(i));
}

i++; 
Run Code Online (Sandbox Code Playgroud)