2016-11-17 2 views
0

私のタイトルは具体的です:txtファイルを同じサイズの複数のファイルに分割しようとしています。 私は、この機能を使用することを行うために管理:テキストファイルをJAVAの単語を壊さずに同じサイズのファイルに分割する

public static int fileSplitting(String fichier, String dossSortie, int nbMachines) throws FileNotFoundException, IOException{ 
     int i=1; 

     File f = new File(fichier); 
     //FileReader fr = new FileReader(f); 
     //BufferedReader br = new BufferedReader(fr); 
     int sizeOfFiles = (int) (f.length()/(nbMachines)); 

     System.out.print(sizeOfFiles); 
     byte[] buffer = new byte[sizeOfFiles]; 

     try (BufferedInputStream bis = new BufferedInputStream(
       new FileInputStream(f))){ 
      int tmp = 0; 
      while ((tmp = bis.read(buffer)) > 0) { 
       //write each chunk of data into separate file with different number in name 
       File newFile = new File(dossSortie+"S"+i); 
       try (FileOutputStream out = new FileOutputStream(newFile)) { 
        out.write(buffer, 0, tmp);//tmp is chunk size 
        } 
       i++; 
      } 
     } 

     return i; 
} 

事は、私はすべての単語を維持する必要がある一方で、この関数は言葉を切っていることです。 たとえば、「私はアムステルダムに住んでいます」というtxtファイルがある場合、関数は「私はAmsに住んでいます」、「terdam」のように分割します。 私は「私は住んでいます」、「アムステルダム」のようなものを望みます。

ありがとうございます!

+0

ファイルのサイズがまったく同じで、最大公約数を見つけるなどの問題がある場合:https://en.wikipedia.org/wiki/Greatest_common_divisorしかし、すべての長さの単語について検索する必要があります –

+0

'バイト 'のものは厳しくなります。ファイルを 'String'オブジェクトで読み込みます。 'String'は' byte'より簡単に演奏できます。 –

+0

代わりにローマに住んでいたら? "私はローマに住んでいますか"が有効でしょうか? – walen

答えて

0

私は仕事をすることができませんでしたが、ファイルを単語の配列に分割して、単語の数が等しいファイルに分割しました... それは私がやりたいことではなく、それをする "美しい方法"が、それはそれほど悪くはありません。

関連する問題