Reputation: 81
Hello I have a problem with read file.
I have BIG .txt file (500MB)
And I want read line with method I start method medhor rsault is first line. I start method second and return second line
I have this code. I save last read line and read line+1 but program stoped in each line < last read line. and if I read 100 000< line it's too long.
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;
}
Please have you some ides how resolve problem? BUT if I set line 500 000 it's over 1 s. but file have 19 000 000 lines..
Upvotes: 0
Views: 282
Reputation: 54
I am not sure whether I got your idea, but if you want to process some lines in file starting from line X to line Y, I would recommend using File.lines() method:
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 has functions so you can get required amount of lines and use Files.lines().count() to get total lines in file.
P.S: I use this method to process files over 2Gb, I hope the answer will be useful)
Upvotes: 4