程序将一个文件中的偶数和奇数排序为单独的文件

问题描述 投票:0回答:0

我一直在尝试将数字从一个文件排序到两个单独的文件,但我似乎无法让程序按设计工作。代码的步骤是这样的,用命令行参数打开文件,阅读第一个文件,并将数字排序为偶数和奇数文件。

所以我在尝试使用命令行参数打开文件时遇到了一个完整的问题,我认为我成功地做到了,但请告诉我是否没有。下一个问题是在我解析输入文件后在屏幕上打印 odd.txt 和 even.txt 文件。这是代码:

这里是空输出文件、输入文件和 .java 文件,如果有帮助的话:

.java 文件:https://drive.google.com/file/d/15dQaUZ28y0brgOWA7RE4CKPBgcLTSsWF/view?usp=sharing

in_2.txt:https://docs.google.com/document/d/1hsC-1kqYbu_ODJI6WwA4OyhdNw_drdgrOYmk4Bn4DWo/edit?usp=share_link

odd.txt(空文件):https://docs.google.com/document/d/11QNgsbo6O5t28c0Ef0V2XTd1y45DJibHsscqJIrnocM/edit?usp=share_link

even.txt(空文件):https://docs.google.com/document/d/1bsIMuG6sNTasJ8yJTJcEL-ljxdBe3nO4W09M0ui4wHM/edit?usp=share_link

PS我确实向我的老师求助过,他基本上告诉我要支付家教费用,所以我用谷歌搜索了一下,找不到任何东西,请帮忙?或者告诉我应该怎么做?

命令行参数:in_txt odd.txt even.txt

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class even_odd_sorting {

    public static void main(String[] args) {

        BufferedReader br = null;
        BufferedReader buf = null;
        BufferedReader buff = null;

        String inputFile;
        String outputReader1;
        String outputReader2;

        File input   = new File(args[0]);
        File output1 = new File(args[1]);
        File output2 = new File(args[2]);


        //input reader
        try {
            br = new BufferedReader(new FileReader(input));
            buf = new BufferedReader(new FileReader(output1));
            buff = new BufferedReader(new FileReader(output2));

        } catch (FileNotFoundException fnfex) {
            System.out.println(fnfex.getMessage() + "The file was not found");
            System.exit(0);
        }

        //reading the file here
        try {
            while ((inputFile = br.readLine()) != null) {
                int num = Integer.parseInt(inputFile);
                System.out.println(inputFile);
                int even_odd = num%2;

                // Attempting to write here
                // This is where I think that I need help
                if (even_odd == 0) {
                    FileWriter outputWrite1 = new FileWriter(output1);
                    outputWrite1.write (even_odd);
                    outputWrite1.close () ;

                }else {
                    FileWriter outputWrite2 = new FileWriter(output2);
                    outputWrite2.write (even_odd);
                    outputWrite2.close () ;
                }

                // Reading output file 1
                while ((outputReader1 = buf.readLine()) != null) {
                    System.out.println(outputReader1);
                }

                // Reading output file 2
                while ((outputReader2 = buff.readLine()) != null) {
                    System.out.println(outputReader2);
                }
            }
        }catch (IOException ioex) {
            System.out.println(ioex.getMessage() + "Error Reading file");
        } finally {
            System.exit(0);
        }

    }

}```
java file-io command-line-arguments
© www.soinside.com 2019 - 2024. All rights reserved.