나는 하나의 input.txt
파일을 가지고 있습니다.이 파일은 520 행이라고 가정합니다. 자바에서 코드를 만들어야합니다.자바의 한 텍스트 파일에서 여러 파일 만들기
처음 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 개 이상의 출력 파일을 제공하는 것처럼이 코드를 작동시키고 싶습니다. 이것에
if(temp<201){ //may be i need some changes here
dm.filewriter(line+" "+temp+")",outputfilename);
}
:
: 여기
은 예입니다. <201, 대신에 [modulus arithmatic]을보십시오 (http://stackoverflow.com/questions/90238/whats-the-syntax-for-mod-in-java). –