我有一个大的文本文件,我想要阅读它,当我尝试做它没有任何操作,如从该文件中添加一些文本列出它的读取文件最大为一分钟,但是当我尝试添加一些文本到arrayList和接下来我想做一些操作它太慢了,你知道我怎么读取这些数据并使用它? 这是我的代码:如何阅读大文本文件,并在Java中使用它
public class ReaderTEst {
public static void main(String[] args) throws IOException {
List<String> graphList = new ArrayList<>();
List<String> edgeList = new ArrayList<>();
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream("myText.txt");
sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
String line = sc.nextLine();
line = line.replace("\uFEFF", "");//i use UTF-8 file so I need delete unneeded character
if (Character.isWhitespace(line.charAt(0))) {
edgeList.add(line.trim());
} else {
graphList.add(line);
}
}
if (sc.ioException() != null) {
throw sc.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
}
} 它需要很多的时间,你知道它是如何可能会更快?我有600 MB 文件TXT当我改变:
List<Integer> graphList = new ArrayList<>(1);
int i = 0;
while (sc.hasNextLine()) {`String line = sc.nextLine();`
line = line.replace("\uFEFF", "");//i use UTF-8 file so I need delete unneeded character
graphList.add(i++);
}
我的作品,但是当我想要把字符串时间太长的时间
这是正弦波我使用UTF-8和第一个字符我有奇怪的空字符,所以我尽量只的charAt(0) – grapexs
所以用它它*是一个BOM标记,所以你应该只从第一行删除它。 – EJP