2017-04-18 189 views
3

我有一個input.txt文件,它包含假設520行。 我必須在java中製作一個代碼,它的行爲就像這樣。從java中的一個文本文件創建多個文件

從頭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,看看[模數算術](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); 
} 

這樣:

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

這將確保第200行到第一個文件,下一個200行到下一個文件等等。

此外,您可能需要一起批處理200行並一次寫入,而不是每次創建writer並寫入文件。

+0

你剛剛改變了要寫入文件的數據。它與輸出文件名無關。請再看看。 –

+0

@AdnanAli感謝您的輸入,更新了答案。 –

+0

讓我告訴你這是如何工作的。 我給了1083行input.txt文件。和代碼創建了200個文本文件,每個文件包含5行。所以。 200x5 = 1000和83行他們剛剛消失 –

0

您可能有創建Writer當前的File的方法,讀取多達limit號線,關閉Writer當前的File,然後返回true,如果它有足夠的閱讀,false如果不能讀取限制行數(即中止下一次調用,不要嘗試讀取更多行或寫入下一個文件)。

然後你會在循環中調用它,傳遞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; 

    } 

} 
相關問題