简体   繁体   中英

Reading lines from a file in Java

G'day:

I'm trying to use this:

List<String> lines = Files.readAllLines(Paths.get(path), encoding);

from https://stackoverflow.com/a/326440/2698254 and http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file.Path,%20java.nio.charset.Charset%29

but I'm having a bit of trouble implementing it.

My goal is to read all the lines from a file, then do some stuff with each line using that list (I imagine it works just like a vector...)

A few questions, because I'm getting a bunch of errors:

List<String> lines = Files.readAllLines(Paths.get("assets/unitsloc.txt"), Charset.defaultCharset());

This is what I've got so far, but the error markers are:

"Files cannot be resolved" - but there's no useful suggested import to make, same with Paths.

"The type List is not generic: it cannot be parametrized with arguments " -Do I need to initialise lines first?

You need to import the class before using it.

At the top of you class (after package), add

import java.nio.file.Files;
import java.nio.file.Paths;

As readAllLines returns List<String> the warning about this should go away after correct importation.

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