gpt4 book ai didi

java - 如何在Java中更快地读取大文本文件?

转载 作者:行者123 更新时间:2023-12-01 22:10:40 25 4
gpt4 key购买 nike

我刚刚制作了一个简单的顺序中值滤波器算法,并且碰巧使用了 2 个扫描仪来处理命令行输入和一个扫描仪来读取文件。现在的问题是,我有一个文本文件 2,000,000 2 列文本行,格式为 <integer> <float>光是读取文件就需要很长时间(超过2分钟)。

基本上,程序只是获取输入,使用中值过滤算法并写入输出文件。

下面是我的代码:ma​​in.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class main {
public static void main(String args[]) throws NumberFormatException, IOException{
String inFile; //Input file name.
int filterSize; //Filter size (odd integer >= 3).
String outFile; //Output file name.
int arraySize;
List<Float> elements = new ArrayList<Float>();
int index = 0;

//Scanner to take input file name, filter size and output file name.
Scanner keyboardInput = new Scanner(System.in);
System.out.println("Enter your keyboard input as follows: <data file name> <filter size(odd int >= 3> <output file name>");

//Assigning values to variables.
inFile = keyboardInput.next();
filterSize = keyboardInput.nextInt();
outFile = keyboardInput.next();


// //Reading file
// Scanner readFile = new Scanner(new File(inFile));
// readFile.nextInt(); //Get Array Size
//
// //Add elements into ArrayList
// while(readFile.hasNext()){
// readFile.nextInt();
// elements.add(Float.parseFloat(readFile.next()));
// }

//Reading file
BufferedReader br = new BufferedReader(new FileReader(inFile));
br.readLine(); //Get Array Size

String line;
while((line = br.readLine())!= null){
String[] nums = line.split(" ");
int val = Integer.valueOf(nums[0]);
elements.add(Float.valueOf(nums[1]));
}
br.close();

new Serial(elements, filterSize, outFile);

}
}

Serial.java

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Serial {
int filterSize; //Filter size (odd integer >= 3).
String outFile; //Output file name.
int arraySize;
List<Float> elements = new ArrayList<Float>();
int index = 0;


public Serial(List<Float> elements, int filterSize, String outFile) throws FileNotFoundException, UnsupportedEncodingException {
this.elements = elements;
this.filterSize= filterSize;
this.outFile = outFile;


List<Float> tempElements = new ArrayList<Float>();
List<Float> outputElements = new ArrayList<Float>();

//Add first boundary element to ouput ArrayList
outputElements.add(this.elements.get(0));

while(elements.size() >= filterSize){
for(int i = 0; i<filterSize; i++){
tempElements.add(this.elements.get(i));
}

Collections.sort(tempElements);
outputElements.add(tempElements.get((filterSize-1)/2));

elements.remove(0);
tempElements.clear();
}

//Add last boundary element to output ArrayList
if (elements != null && !elements.isEmpty()) {
outputElements.add(elements.get(elements.size()-1));
}

/*Trace. Checking if output is correct
for(int i=0; i<outputElements.size(); i++){
System.out.println(outputElements.get(i));
}*/

//Write elements to output file
PrintWriter writeOutput = new PrintWriter(this.outFile, "UTF-8");
writeOutput.println(outputElements.size());//Number of lines
for(int i=0; i<outputElements.size();i++){
writeOutput.println(i+1 + " " + outputElements.get(i)); //Each line is written
}

writeOutput.close(); //Close when output finished writing.
}
}

有没有办法更快地读取文件(以秒为单位)?

谢谢

编辑:输入示例

5
1 2
2 80
3 6
4 3
5 1

最佳答案

使用Buffered Reader应该提高读取文件的速度,因为它的缓冲区大小比扫描仪大得多。

从您之前的一个问题中我看出您最初使用的是 Buffered Reader。您可以逐行读取并在空格上分割字符串,如下所示:

    //Reading file
BufferedReader br = new BufferedReader(new FileReader(inFile));
br.readLine(); //Get Array Size

String line;
while((line = br.readLine())!= null){
String[] nums = line.split(" ");
int val = Integer.valueOf(nums[0]);
elements.add(Float.valueOf(nums[1]));
}
br.close();

关于java - 如何在Java中更快地读取大文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31907567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com