简体   繁体   English

从Java文件中读取行

[英]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 来自https://stackoverflow.com/a/326440/2698254http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines%28java.nio.file 。路径,%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. “文件无法解析”-但是没有建议进行有用的导入,与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. readAllLines返回List<String> ,关于此的警告应在正确导入后消失。

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

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