问题
I want to read lines from a .txt file (in this case input.txt) and write them in two other .txt files depending on whether the integer number at the start of each line is odd or even. I have to use DataInputStream / DataOutputStream and BufferedInputStream / BufferedOutputStream.
The input file looks like this.
1 String1 1.1
2 String2 2.2
3 String3 3.3
4 String4 4.4
5 String5 5.5
6 String6 6.6
7 String7 7.7
8 String8 8.8
9 String9 9.9
10 String10 10.1
When I run the program I get an EOFException, at the readUTF parts. When I comment those parts and try to write only the integers, I get only "1" in the odd file and "2" in the even file. It doesn't copy all the integers.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.EOFException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
public class datastream {
public static void main(String[] args) throws IOException {
DataInputStream inputStream = null;
DataOutputStream outputStreamEven = null;
DataOutputStream outputStreamOdd = null;
try {
inputStream = new DataInputStream (new BufferedInputStream ( new FileInputStream("src/input.txt")));
outputStreamEven = new DataOutputStream (new BufferedOutputStream ( new FileOutputStream("src/even.txt")));
outputStreamOdd = new DataOutputStream (new BufferedOutputStream ( new FileOutputStream("src/odd.txt")));
int number;
String textinput;
double doublenumber;
number = inputStream.readInt();
textinput = inputStream.readUTF();
doublenumber = inputStream.readDouble();
while ((number = inputStream.read()) % 2 != 0) {
outputStreamOdd.writeInt(number);
outputStreamOdd.writeUTF(textinput);
outputStreamOdd.writeDouble(doublenumber);
}
while ((number = inputStream.read()) % 2 == 0) {
outputStreamEven.writeInt(number);
outputStreamEven.writeUTF(textinput);
outputStreamEven.writeDouble(doublenumber);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStreamEven != null) {
outputStreamEven.close();
}
if (outputStreamOdd != null) {
outputStreamOdd.close();
}
}
}
}
What can I do to copy all the lines to the appropriate text file (along with each line having all its contents)?
回答1:
You are manipulating a text file, therefore you should rather use characters streams to handle this.
So using BufferedReader and BufferedWriter would help you solve this issue in a simple way. Here is a code that does that. I have copied your sample datas in a text file and called it sourceData.txt
public class OddEvenFileManager {
public static final String DATA_SOURCE_PATH = "sourceDatas.txt";
public static final String ODD_TARGET_PATH = "oddDatas.txt";
public static final String EVEN_TARGET_PATH = "evenDatas.txt";
public static void doOddEvenSplitting() throws IOException, NumberFormatException {
BufferedReader sourceReader = new BufferedReader(new FileReader(new File(DATA_SOURCE_PATH)));
BufferedWriter oddWriter = new BufferedWriter(new FileWriter(new File(ODD_TARGET_PATH)));
BufferedWriter evenWriter = new BufferedWriter(new FileWriter(new File(EVEN_TARGET_PATH)));
String sourceLine = null;
while ((sourceLine = sourceReader.readLine()) != null) {
int lineNumber = Integer.parseInt(sourceLine.split(" ")[0]);
if (lineNumber % 2 == 0) {
evenWriter.write(sourceLine);
evenWriter.newLine();
} else {
oddWriter.write(sourceLine);
oddWriter.newLine();
}
}
sourceReader.close();
oddWriter.close();
evenWriter.close();
}
public static void main(String[] args) throws IOException, NumberFormatException {
doOddEvenSplitting();
}
}
来源:https://stackoverflow.com/questions/29267020/java-read-and-write-a-txt-file