简体   繁体   English

扫描仪无法从文件中读取文本

[英]Scanner not reading text from file

I am having an issue where I am trying to use Scanner to add elements to an arraylist from a text file. 我在尝试使用扫描仪将元素从文本文件添加到arraylist时遇到问题。 I have looked over my code what feels like a thousand times and I can't seem to find the issue. 我已经看了我的代码一千遍了,但似乎找不到问题。

Scanner scan = new Scanner(new File(fileNameSub)); //scanner object that locates the file
    scan.useDelimiter("\\n"); //delimiter for scanner

        ArrayList<String> fileList = new ArrayList<>(); //create arraylist to hold txt file contents                

        while(scan.hasNextLine()) {
            fileList.add(scan.nextLine());
        } // while loop that adds the contents so the arraylist

Please let me know if you see any errors that could be causing the issue. 如果您发现任何可能导致此问题的错误,请告诉我。

Thanks in advance. 提前致谢。

Remove scan.useDelimiter("\\\\n" ); 删除scan.useDelimiter("\\\\n" ); and try again, or better use NIO2 然后重试,或者更好地使用NIO2

List<String>lines = Files.readLines(Paths.get(file), StandardCharsets.UTF_8);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM