在java中从一个文本文件创建多个文件
我有一个在java中从一个文本文件创建多个文件,java,filestream,file-handling,Java,Filestream,File Handling,我有一个input.txt文件,包含520行。 我必须用java编写一个类似这样的代码 从前200行创建名为file-001.txt的第一个文件。然后从201-400行创建另一个文件-002。然后从其余行中选择file-003.txt 我已经编码了这个,它只写了前200行。为了将其工作更新到上述场景,我需要进行哪些更改 public class DataMaker { public static void main(String args[]) throws IOException{
input.txt
文件,包含520行。
我必须用java编写一个类似这样的代码
从前200行创建名为file-001.txt
的第一个文件。然后从201-400行创建另一个文件-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();
}
}
}
公共类数据生成器{
公共静态void main(字符串args[])引发IOException{
DataMaker dm=新的DataMaker();
String file=“D:\\input.txt”;
int roll=1;
字符串rollnum=“file-00”+滚动;
字符串outputfilename=“D:\\output\\”+rollnum+“.txt”;
字符串乌尔都字词;
字符串路径;
ArrayList,其中=新的ArrayList();
内部温度=0;
try(BufferedReader br=new BufferedReader(new FileReader(file))){
for(字符串行;(line=br.readLine())!=null;){
++温度;
如果(温度可以更改以下位:
if(temp<201){ //may be i need some changes here
dm.filewriter(line+" "+temp+")",outputfilename);
}
这将确保前200行转到第一个文件,后200行转到下一个文件,依此类推
此外,您可能希望将200行批处理在一起,一次性写入,而不是每次都创建编写器并写入文件。您可能有一种方法,可以将编写器
创建到当前文件
,读取到限制
行数,将编写器
关闭到当前文件ile
,如果有足够的数据读取,则返回true
;如果无法读取限制行数,则返回false
(即,中止下一次调用,不要尝试读取更多行或写入下一个文件)
然后在循环中调用它,传递读取器
、新文件名和限制号
以下是一个例子:
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;
}
}
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.OutputStreamWriter;
公共类数据生成器{
公共静态void main(最终字符串args[])引发IOException{
DataMaker dm=新的DataMaker();
String file=“D:\\input.txt”;
int roll=1;
字符串rollnum=null;
字符串outputfilename=null;
布尔值shouldContinue=false;
try(BufferedReader br=new BufferedReader(new FileReader(file))){
做{
rollnum=“file-00”+滚动;
outputfilename=“D:\\output\\”+rollnum+“.txt”;
shouldContinue=dm.fillFile(outputfilename,br,200);
roll++;
}而(应该继续);
}catch(filenotfounde异常){
System.out.println(“未找到文件”);
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私有布尔填充文件(最终字符串输出文件名、最终BufferedReader读取器、最终整数限制)
抛出IOException{
布尔结果=假;
字符串行=null;
BufferedWriter fbw=null;
内部温度=0;
试一试{
OutputStreamWriter writer=新的OutputStreamWriter(
新文件OutputStream(outputfilename,true),“UTF-8”);
fbw=新的缓冲写入程序(写入程序);
而(temp
您刚刚更改了要写入文件的数据。这与输出文件名无关。请再看一看。让我告诉您这是如何工作的。我给了1083行input.txt文件。代码创建了200个文本文件,每个文件包含5行。因此.200x5=1000和83行它们刚刚消失了。kay,将%
更改为/
,现在就试试?可能在这里的正确轨道上。而不是<201,请看。
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;
}
}