2017-04-18 156 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; 

    } 

} 
相关问题