带有Printwriter对象的println()未写入文件

时间:2019-04-04 02:32:59

标签: java printstream

为什么PrintWriter不在以下代码中写入文件?

import java.io.*;
import java.util.*;

class test{
    public static void main(String[] args) throws IOException{

        Scanner in = new Scanner(System.in);
        System.out.println("Enter input file name");
        String inputfile = in.nextLine();
        System.out.println("Enter output file name");
        String outputfile = in.nextLine();
        in.close();

        File f = new File(inputfile);
        Scanner input = new Scanner(f);
        PrintWriter output = new PrintWriter(outputfile);

        while( input.hasNextLine()){
            String s = input.nextLine(); ////// reading the file lines perfectlly
            output.print(s);    // but not writing 
        }    
        output.close();
        input.close();

    }
}

如代码中所述,正在读取输入文件的行,但未将它们写入输出文件。

0 个答案:

没有答案