Java-读写.txt文件 [英] Java - Read and Write a .txt file

查看:53
本文介绍了Java-读写.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

输入文件如下所示.

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".它不会复制所有整数.

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)?

推荐答案

您正在处理文本文件,因此,您应该使用字符流来处理它.

You are manipulating a text file, therefore you should rather use characters streams to handle this.

因此,使用BufferedReader和BufferedWriter将帮助您以简单的方式解决此问题.这是执行此操作的代码.我已将您的示例数据复制到一个文本文件中,并将其命名为sourceData.txt

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

这篇关于Java-读写.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆