James Adams
James Adams

Reputation: 748

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?

Upvotes: 1

Views: 1414

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44854

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.

Upvotes: 1

Related Questions