java - Java-读写.txt文件

原文 标签 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();
            }
        }
    }
}





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

最佳答案

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

因此,使用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 - Java-读写.txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29267020/

相关文章:

java - 如果我在conf文件夹中的server.xml中进行更改,则Tomcat服务器无法启动

java - 我可以这样做吗 - token=str.split (""| |",");

java - JUnit参数化测试: how do I run only 1 specific test from IntelliJ/Eclipse?

android - 截图文件需要很长时间才能保存在Android设备上

java - 如何循环用户输入直到输入整数?

ios - iOS上一个/下一个按钮意外关闭键盘

java - arrayListName.sort(null) 有什么作用?

java - File()构造函数中是否需要.txt扩展名?

Linux:删除不包含所有指定单词的文件

c# - 快速将字符串复制到char数组