2017-04-18 6 views
3

私は1つのinput.txtファイルを用意しています。 私はこのように動作するjavaでコードを作成する必要があります。1つのテキストファイルから複数のファイルを作成する

最初の200行から最初のファイルfile-001.txtを作成します。 201〜400行の別のfile-002を作成します。残りの行からはfile-003.txtになります。

私はこれをコーディングしました。最初の200行しか書いていません。上記のシナリオにその作業を更新するために必要な変更。

public class DataMaker { 
public static void main(String args[]) throws IOException{ 
    DataMaker dm=new DataMaker(); 
    String file= "D:\\input.txt"; 
    int roll=1; 
    String rollnum ="file-00"+roll; 
    String outputfilename="D:\\output\\"+rollnum+".txt"; 
    String urduwords; 
    String path; 
    ArrayList<String> where = new ArrayList<String>(); 
    int temp=0; 
    try(BufferedReader br = new BufferedReader(new FileReader(file))) { 
      for(String line; (line = br.readLine()) != null;) { 
       ++temp; 
       if(temp<201){ //may be i need some changes here 
       dm.filewriter(line+" "+temp+")",outputfilename); 
       } 
      } 
     } catch (FileNotFoundException e) { 
      System.out.println("File not found"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }  
} 
void filewriter(String linetoline,String filename) throws IOException{ 
    BufferedWriter fbw =null; 
    try{ 

     OutputStreamWriter writer = new OutputStreamWriter(
       new FileOutputStream(filename, true), "UTF-8"); 
      fbw = new BufferedWriter(writer); 
     fbw.write(linetoline); 
     fbw.newLine(); 

    }catch (Exception e) { 
     System.out.println("Error: " + e.getMessage()); 
    } 
    finally { 
     fbw.close(); 
     } 
} 

} 

一つの方法は、if elseの使用することができますが、私の実際のファイルは6000+行ですので、私はちょうどそれを使用傾けます。

コードを実行して30個以上の出力ファイルを与えるようにこのコードを動作させます。

+0

おそらくここに右のトラックに。 <201の代わりに[modulus arithmatic](http://stackoverflow.com/questions/90238/whats-the-syntax-for-mod-in-java)を見てください。 –

答えて

1

あなたは、次のビットを変更することができます。

if(temp<201){ //may be i need some changes here 
    dm.filewriter(line+" "+temp+")",outputfilename); 
} 

これに:これは確か最初の200行が最初のファイルに行くようになります

dm.filewriter(line, "D:\\output\\file-00" + ((temp/200)+1) + ".txt"); 

、次の200行は、その次のファイルに移動し、 。

また、毎回writerを作成してファイルに書き込むのではなく、200行をまとめて一度に書き込むこともできます。

あなたが現在 Fileから Writerを作成する方法を有することができる
+0

ファイルに書き込まれるデータを変更しました。出力ファイル名とは関係ありません。親切にもう一度見てみましょう。 –

+0

@AdnanAliは入力を感謝し、答えを更新しました。 –

+0

これがどのように機能しているか教えてください。 1083行のinput.txtファイルを作成しました。 5行のテキストファイルをそれぞれ200個作成した。そう。 200x5 = 1000と83行が消えた –

0

false、その後、現在のFileからWriterを閉じ、ラインのlimit数まで読み込み、それを読むことが十分にあった場合trueを返した場合、それができませんでした行の制限数を読んでください(次の呼び出しを中断し、より多くの行を読んだり、次のファイルを書き込もうとしないでください)。

これをループで呼び出し、Reader、新しいファイル名、および制限番号を渡します。ここで

は一例です:

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 

public class DataMaker { 
    public static void main(final String args[]) throws IOException { 
     DataMaker dm = new DataMaker(); 
     String file = "D:\\input.txt"; 
     int roll = 1; 
     String rollnum = null; 
     String outputfilename = null; 

     boolean shouldContinue = false; 

     try (BufferedReader br = new BufferedReader(new FileReader(file))) { 

      do { 

       rollnum = "file-00" + roll; 
       outputfilename = "D:\\output\\" + rollnum + ".txt"; 
       shouldContinue = dm.fillFile(outputfilename, br, 200); 
       roll++; 
      } while (shouldContinue); 

     } catch (FileNotFoundException e) { 
      System.out.println("File not found"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    private boolean fillFile(final String outputfilename, final BufferedReader reader, final int limit) 
      throws IOException { 

     boolean result = false; 
     String line = null; 
     BufferedWriter fbw = null; 
     int temp = 0; 
     try { 
      OutputStreamWriter writer = new OutputStreamWriter(
        new FileOutputStream(outputfilename, true), "UTF-8"); 
      fbw = new BufferedWriter(writer); 

      while (temp < limit && ((line = reader.readLine()) != null)) { 

       temp++; 
       fbw.write(line); 
       fbw.newLine(); 

      } 

      // abort if we didn't manage to read the "limit" number of lines 
      result = (temp == limit); 

     } catch (Exception e) { 
      System.out.println("Error: " + e.getMessage()); 
     } finally { 
      fbw.close(); 
     } 

     return result; 

    } 

} 
関連する問題