2017-02-16 10 views
0

こんにちは私は読んだファイルに問題があります。metodでBIG txtファイルを読むJAVA

私はBIG .txtファイル(500メガバイト)

を持っていると私は方法medhor rsaultが最初の行で開始する方法で読ま行をしたいです。 メソッドを2番目に開始して2行目を返します

私はこのコードを持っています。最後の読み取りラインと読み取りライン+ 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; 
    } 

どうすれば問題を解決できますか?しかし、私が50万000を設定すると、それは1秒以上です。ファイルは19 000 000行あります..

答えて

3

私はあなたの考えを持っているかどうかはわかりませんが、X行からY行までのファイルのいくつかの行を処理したい場合は、File.lines()方法:あなたがラインの必要量を取得し、Files.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が)機能を持っている(カウント)ファイルの総行を取得します。

P.S:私が2GBを介してファイルを処理するために、このメソッドを使用し、私は答えは)

+0

役に立つことを願っていますし、どのように文字列にするStartLineを取得しますか? – user7575308

+1

_lines_という変数がStream データ型なので、lambda(Java8)を使用してストリームをループすることができます。 'lines.forEach(l - > { //ここでは各行を処理するロジックに入ります... ); ' ** l **は開始線です。ループ内の文字列です –

+1

@ user7575308答えを更新しました今、それがもっと明確になることを願っています。 –

関連する問題