我有一個大的文本文件,我想要閱讀它,當我嘗試做它沒有任何操作,如從該文件中添加一些文本列出它的讀取文件最大爲一分鐘,但是當我嘗試添加一些文本到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