您好我的读取文件有问题。
我有BIG .txt文件(500MB)
并且想要方法的读取线我开始方法medhor rsault是第1行。 我开始第二个方法并返回第二行
我有这个代码。我保存了最后一条读取行并读取了行+ 1,但程序在每一行中都停止了<最后读行。如果我读了100 000<排它太长了。
public static Boolean Jedno(){
int poradievtahu=0;
int[] tah=new int[7];
String subor= "C:/Users/Paradox/workspace/Citaj_po_Riadku/all6.txt";
Scanner sc;
try {
sc = new Scanner(new File(subor));
int lineIndex = 0;
cit: while(sc.hasNextLine()) {
String line = sc.nextLine();
if(lineIndex++ >= pocetC+1) {
System.out.print("Zvacsujem "+ (pocetC+1) + " " + line);
// do something
poradievtahu=-1;
Scanner scanner=new Scanner(line);
while(scanner.hasNextInt()){
int pom= scanner.nextInt();
tah[++poradievtahu]=pom;
if (poradievtahu==5){
poradievtahu=-1;
pocetC++;
if ((pocetC%(55935)==0)&&(pocetC!=0)){
Calendar cal = Calendar.getInstance();
PrintWriter writer4 = new PrintWriter(new BufferedWriter(new FileWriter("nove.txt", true)));
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
writer4.println("Ďalšia 1/1470 in " + sdf.format(cal.getTime()));
writer4.println(Arrays.toString(tah));
writer4.close();
}
if (pocetC>=13983816){
//berem=false;
PrintWriter writer4 = new PrintWriter(new BufferedWriter(new FileWriter("mozne.txt", true)));
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
writer4.println("End file in " + sdf.format(cal.getTime()));
writer4.close();
return true;
}
Pocty.hladam=tah;
}
}
break cit;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
请你有一些想法如何解决问题?但如果我设置500 000行超过1秒。但文件有19 000 000行..
答案 0 :(得分:4)
我不确定我是否有你的想法,但如果你想处理从第X行到第Y行的文件中的某些行,我建议使用File.lines()方法:
public static void processLinesFromPoint(int startLine, int numberOfLinesToProcess) throws IOException {
//assume startLine = 5000
// numberOfLinesToProcess = 500
Stream<String> lines = Files.lines(Paths.get(pathToYourFile)).skip(startLine).limit(numberOfLinesToProcess);
//lines.forEach method to loop through lines 5000 to 5500 (startLine+numberOfLinesToProcess)
//and printing each line
lines.forEach(currentLine->{
//here goes your logic to process each line...
System.out.println(currentLine)
});
}
Files.lines具有函数,因此您可以获得所需的行数并使用Files.lines()。count()来获取文件中的总行数。
P.S:我使用这种方法处理2Gb以上的文件,希望答案有用)