我有一个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多个输出文件。
这里大概在正确的轨道上。而不是<201,看看[模数算术](http://stackoverflow.com/questions/90238/whats-the-syntax-for-mod-in-java)。 –