如何将Java中的输入和输出流从控制台切换到文件,反之亦然?

我创建了Singleton类,用于编写编程竞赛的代码。 在系统上测试解决方案时,我需要读取/写入文件“ input.txt” /“ output.txt”。 为了使用控制台进行交互式调试,我创建了两种方法- 1。reset()-将I / O流重置为控制台和 2。set()-将I / O流设置为文件“ input.txt”和“ output.txt”

班级是-

class SingletonClass
{
    public static BufferedReader input=null;
    public static InputStream console_input=System.in;
    public static printstream console_output=System.out;
    public static FileInputStream file_istream;
    public static printstream file_ostream;
    public static BufferedReader getInstance()throws IOException,FileNotFoundException
    {
        if(input==null)
        {
            if (System.getProperty("ONLINE_JUDGE") == null)
            {
                file_istream = new FileInputStream("input.txt");
                file_ostream = new printstream("output.txt");
                System.setIn(file_istream);
                System.setOut(file_ostream);
            }
            input = new BufferedReader(new InputStreamReader(System.in));
        }
        return input;
    }
    public static BufferedReader reset()throws IOException
    {
        System.setOut(console_output);
        System.setIn(console_input);
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        return input;
    }
    public static BufferedReader set()throws IOException
    {
        System.setIn(file_istream);
        System.setOut(file_ostream);
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        return input;
    }
}

创建的BufferedReader类的第一个实例是使用文件进行读取/写入。 现在,如果我使用reset(),它可以正常工作,并且可以读/写切换到控制台。 但是在reset()之后,现在如果我使用set(),则仅将输出正确地定向到“ output.txt”,并且在接受输入时,程序将引发异常java.lang.NumberFormatException: null

我使用以下代码对其进行了测试。

class tester
{
    public static void main(String[] args)throws IOException
    {
        BufferedReader input = SingletonClass.getInstance();
        int n;
        System.out.println("Using file first time");//............line1
        n=Integer.parseInt(input.readLine());//...................line2
        System.out.println("n from file to file = "+n);//.........line3
        input = SingletonClass.reset();//.........................line4
        System.out.println("Switched to console");//..............line5
        System.out.println("n from file to console = "+n);//......line6
        n=Integer.parseInt(input.readLine());//...................line7
        System.out.println("n from console to console = "+n);//...line8
        input=SingletonClass.set();//.............................line9
        System.out.println("Switched to file");//.................lin10
        System.out.println("n from console to file = "+n);//......lin11
        System.out.println("Again reading from file");//..........line12
        n=Integer.parseInt(input.readLine());//...................line 13 throws Exception
        System.out.println("second n from file to file = "+n);//..line 14
    }
}

“ input.txt”有2行-

10
20

对于第7行,我在控制台中输入25。 我得到了以下2个输出-

1.output.txt

Using file first time
n from file to file = 10
Switched to file
n from console to file = 25
Again reading from file

2。控制台输出

Switched to console
n from file to console = 10
n from console to console = 25
Exception in thread "main" java.lang.NumberFormatException: null
        at java.lang.Integer.parseInt(Unknown Source)
        at java.lang.Integer.parseInt(Unknown Source)
        at abcd.main(tester.java:57)

因此,在第9行切换到文件后,第10、11、12行可以正确打印到文件,但是第13行会抛出java.lang.NumberFormatException: null,并且不会从文件中读取值20。 我假设设置System.in属性的方式出了问题,但是当在input中创建main的第一个实例时调用相同的代码行时,它可以正常工作。

iCMS 回答:如何将Java中的输入和输出流从控制台切换到文件,反之亦然?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2267238.html

大家都在问