Java - 读取和写入.txt文件

时间:2015-03-25 21:37:46

标签: java file input output

我想读取.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();
            }
        }
    }
}

如何将所有行复制到相应的文本文件(以及每行包含其所有内容)?

1 个答案:

答案 0 :(得分:2)

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

因此,使用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();
    }
}