简体   繁体   中英

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. 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" ); and try again, or better use NIO2

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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