私は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個以上の出力ファイルを与えるようにこのコードを動作させます。
おそらくここに右のトラックに。 <201の代わりに[modulus arithmatic](http://stackoverflow.com/questions/90238/whats-the-syntax-for-mod-in-java)を見てください。 –