gpt4 book ai didi

Java - 读取和写入 .txt 文件

转载 作者:行者123 更新时间:2023-11-30 08:54:58 26 4
gpt4 key购买 nike

我想从 .txt 文件(在本例中为 input.txt)中读取行,并将它们写入另外两个 .txt 文件,具体取决于每行开头的整数是奇数还是偶数。我必须使用 DataInputStream/DataOutputStream 和 BufferedInputStream/BufferedOutputStream。

输入文件如下所示。

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

当我运行程序时,我在 readUTF 部分收到 EOFException。当我评论这些部分并尝试只写整数时,我在奇数文件中只得到“1”,在偶数文件中得到“2”。它不会复制所有整数。

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();
}
}
}
}

我该怎么做才能将所有行复制到适当的文本文件(以及包含所有内容的每一行)?

最佳答案

您正在操作一个文本文件,因此您应该使用字符流来处理它。

所以使用 BufferedReader 和 BufferedWriter 可以帮助您以简单的方式解决这个问题。这是执行此操作的代码。我已将您的示例数据复制到一个文本文件中,并将其命名为 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();
}
}

关于Java - 读取和写入 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29267020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com