如何从文件中一次读取一行并写入不同的文件?
想知道如何一次读取一行并写入不同的文件。谷歌搜索时找不到好的例子:
例如,阅读包含以下内容的文本文件: sent1 sent2 sent3 ....
写 sent1到1.txt, sent2到2.txt, sent3到3.txt
这似乎是一个简单的操作,我的编码有问题,不知道如何修复它。
import java.util.*;
import java.io.*;
public class MyFunction {
public static void main(String args[])
throws IOException, ClassNotFoundException
{
//variable initialization
String strLine;
int i = 0;
//Read in a file and process one line at a time
FileReader singleFile = new FileReader("C:\\data);
BufferedReader reader = new BufferedReader(singleFile);
for (int j=0; j < 12; j++) {
FileWriter outFile = new FileWriter("C:\\doc" + i + ".txt");
BufferedWriter writer = new BufferedWriter(outFile);
//Read one line at a time from the input file
while ((strLine = reader.readLine()) !=null) {
//process a string which is not empty
if (strLine.length() > 0) {
System.out.println("This is strLine: " + strLine);
writer.write (strLine);
i=i+1;
System.out.println("This is i: " + i);
} //end of if
}
singleFile.close();
writer.close();
} }}
答案 0 :(得分:0)
最简单的方法可能是在一个数组中打开n个FileWriters,每个输出文件一个(我称之为数组“writers”)。设置计数器i = 0.当您迭代输入行时,将当前行写入writers [i],然后递增i并设置i = i%n。这将使我在0..n-1范围内循环,这意味着你将写入序列0,1,...... n-1,0,1 ...中的文件......我认为这就是你想要的。
答案 1 :(得分:0)
在文件完成之前,您一直处于困境中。取出while循环并将其替换为if,如果突然出现for循环。这将只为你提供前12行。之后,将for循环更改为while循环并手动增加j,如果j> = 12,则将j设置为0.
下面的代码循环超过12个文件,这是你想要的吗?
import java.util.*;
import java.io.*;
public class MyFunction {
public static void main(String args[])
throws IOException, ClassNotFoundException
{
//variable initialization
String strLine;
int i = 0;
//Read in a file and process one line at a time
FileReader singleFile = new FileReader("C:\\data);
BufferedReader reader = new BufferedReader(singleFile);
j = 0;
while (true) { //loop forever
FileWriter outFile = new FileWriter("C:\\doc" + j + ".txt");
BufferedWriter writer = new BufferedWriter(outFile);
//Read one line at a time from the input file
if ((strLine = reader.readLine()) !=null) { break; } // exit loop
//process a string which is not empty
if (strLine.length() > 0) {
System.out.println("This is strLine: " + strLine);
writer.write (strLine);
i=i+1;
j=j+1;
if (j >= 12) {j=0};
System.out.println("This is i: " + i);
} //end of if
singleFile.close();
writer.close();
} }}
答案 2 :(得分:0)
它适用于以下代码。因为我知道文档中只有12行 - 想知道如何找出输入文件中的总行数。
import java.util.*;
import java.io.*;
public class MyFunction {
public static void main(String args[])
throws IOException, ClassNotFoundException
{
//variable initialization
String strLine;
//Read in a file and process one line at a time
FileReader singleFile = new FileReader("C:\\data\\data.txt");
BufferedReader reader = new BufferedReader(singleFile);
for (int i=0; i<12; i++) {
FileWriter outFile = new FileWriter("C:\\sample\\doc" + i + ".txt");
BufferedWriter writer = new BufferedWriter(outFile);
//Read one line at a time from the input file
if ((strLine = reader.readLine()) !=null)
//process a string which is not empty
if (strLine.length() > 0) {
System.out.println("This is strLine: " + strLine);
writer.write (strLine);
System.out.println("This is i: " + i);
} //end of if
singleFile.close();
writer.close();
} }}