目前我正在使用扫描仪/文件读取器并使用whilenextline.我认为这种方法效率不高.有没有其他方法来读取具有类似功能的文件?
public void Read(String file) {
Scanner sc = null;
try {
sc = new Scanner(new FileReader(file));
while (sc.hasNextLine()) {
String text = sc.nextLine();
String[] file_Array = text.split(" ", 3);
if (file_Array[0].equalsIgnoreCase("case")) {
//do something
} else if (file_Array[0].equalsIgnoreCase("object")) {
//do something
} else if (file_Array[0].equalsIgnoreCase("classes")) {
//do something
} else if (file_Array[0].equalsIgnoreCase("function")) {
//do something
}
else if (file_Array[0].equalsIgnoreCase("ignore")) {
//do something
}
else if (file_Array[0].equalsIgnoreCase("display")) {
//do something
}
}
} catch (FileNotFoundException e) {
System.out.println("Input file " + file + " not found");
System.exit(1);
} finally {
sc.close();
}
}
Run Code Online (Sandbox Code Playgroud)
use*_*421 37
您会发现它BufferedReader.readLine()的速度与您的需求一样快:您可以每秒读取数百万行.您的字符串拆分和处理更可能导致您遇到的任何性能问题.
YAM*_*AMM 21
我做了一个比较不同方法的要点:
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
String path = "resources/testfile.txt";
measureTime("BufferedReader.readLine() into LinkedList", Main::bufferReaderToLinkedList, path);
measureTime("BufferedReader.readLine() into ArrayList", Main::bufferReaderToArrayList, path);
measureTime("Files.readAllLines()", Main::readAllLines, path);
measureTime("Scanner.nextLine() into ArrayList", Main::scannerArrayList, path);
measureTime("Scanner.nextLine() into LinkedList", Main::scannerLinkedList, path);
measureTime("RandomAccessFile.readLine() into ArrayList", Main::randomAccessFileArrayList, path);
measureTime("RandomAccessFile.readLine() into LinkedList", Main::randomAccessFileLinkedList, path);
System.out.println("-----------------------------------------------------------");
}
private static void measureTime(String name, Function<String, List<String>> fn, String path) {
System.out.println("-----------------------------------------------------------");
System.out.println("run: " + name);
long startTime = System.nanoTime();
List<String> l = fn.apply(path);
long estimatedTime = System.nanoTime() - startTime;
System.out.println("lines: " + l.size());
System.out.println("estimatedTime: " + estimatedTime / 1_000_000_000.);
}
private static List<String> bufferReaderToLinkedList(String path) {
return bufferReaderToList(path, new LinkedList<>());
}
private static List<String> bufferReaderToArrayList(String path) {
return bufferReaderToList(path, new ArrayList<>());
}
private static List<String> bufferReaderToList(String path, List<String> list) {
try {
final BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null) {
list.add(line);
}
in.close();
} catch (final IOException e) {
e.printStackTrace();
}
return list;
}
private static List<String> readAllLines(String path) {
try {
return Files.readAllLines(Paths.get(path));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static List<String> randomAccessFileLinkedList(String path) {
return randomAccessFile(path, new LinkedList<>());
}
private static List<String> randomAccessFileArrayList(String path) {
return randomAccessFile(path, new ArrayList<>());
}
private static List<String> randomAccessFile(String path, List<String> list) {
try {
RandomAccessFile file = new RandomAccessFile(path, "r");
String str;
while ((str = file.readLine()) != null) {
list.add(str);
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
private static List<String> scannerLinkedList(String path) {
return scanner(path, new LinkedList<>());
}
private static List<String> scannerArrayList(String path) {
return scanner(path, new ArrayList<>());
}
private static List<String> scanner(String path, List<String> list) {
try {
Scanner scanner = new Scanner(new File(path));
while (scanner.hasNextLine()) {
list.add(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return list;
}
}
Run Code Online (Sandbox Code Playgroud)
运行:BufferedReader.readLine() 进入 LinkedList,行数:1000000,估计时间:0.105118655
运行:BufferedReader.readLine() 进入 ArrayList,行数:1000000,估计时间:0.072696934
运行:Files.readAllLines(),行数:1000000,估计时间:0.087753316
运行:Scanner.nextLine() 进入 ArrayList,行数:1000000,估计时间:0.743121734
运行:Scanner.nextLine() 进入 LinkedList,行数:1000000,估计时间:0.867049885
运行:RandomAccessFile.readLine() 进入 ArrayList,行数:1000000,估计时间:11.413323046
运行:RandomAccessFile.readLine() 进入 LinkedList,行数:1000000,估计时间:11.423862897
BufferedReader是最快的,Files.readAllLines()也是可以接受的,Scanner由于正则表达式很慢,RandomAccessFile是不可接受的
小智 6
Scanner不能BufferedReader像它一样快,因为它使用正则表达式来读取文本文件,这使得它比较慢BufferedReader.通过使用,BufferedReader您可以从文本文件中读取块.
BufferedReader bf = new BufferedReader(new FileReader("FileName"));
Run Code Online (Sandbox Code Playgroud)
你可以接下来使用readLine()来读取bf.
希望它符合您的目的.
您可以使用JAVA NIO 中的FileChannel和ByteBuffer。据我观察,字节缓冲区大小是更快地读取数据的最关键部分。下面的代码将读取文件的内容。
static public void main( String args[] ) throws Exception
{
FileInputStream fileInputStream = new FileInputStream(
new File("sample4.txt"));
FileChannel fileChannel = fileInputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
fileChannel.read(byteBuffer);
byteBuffer.flip();
int limit = byteBuffer.limit();
while(limit>0)
{
System.out.print((char)byteBuffer.get());
limit--;
}
fileChannel.close();
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处检查新行的“\n”。谢谢。
即使您可以使用 scatter 和 getter 方式来更快地读取文件,即
fileChannel.get(buffers);
Run Code Online (Sandbox Code Playgroud)
在哪里
ByteBuffer b1 = ByteBuffer.allocate(B1);
ByteBuffer b2 = ByteBuffer.allocate(B2);
ByteBuffer b3 = ByteBuffer.allocate(B3);
ByteBuffer[] buffers = {b1, b2, b3};
Run Code Online (Sandbox Code Playgroud)
这可以使用户进程免于进行多次系统调用(这可能会很昂贵),并允许内核优化数据处理,因为它具有有关总传输的信息,如果有多个 CPU 可用,甚至可以填充和耗尽多个缓冲区同时地。
来自这本书。