1

Why is PrintWriter not writing to the file in the following code?

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

    }
}

As mentioned in the code, the lines of the input files are being read but not written to the output file.

lynxx
  • 544
  • 3
  • 18
  • 1
    https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java – Doc Apr 04 '19 at 02:36
  • thats exactly what I have done in the code above. I still cant find why it is not working. – lynxx Apr 04 '19 at 02:40
  • 2
    This code seems ok, where did you check your output file(file path)? does your output file have write permission? – MD Ruhul Amin Apr 04 '19 at 02:44
  • 1
    what are you feeding to `outputfile`? all fine with write permissions? – Doc Apr 04 '19 at 02:48
  • Its working. I wasn't giving .txt with the file name. SO I was just providing "file" in place of "file.txt". Strange that, scanner can read a file without extensions where as a printwriter can't write to a file without file extensions. Thanks. – lynxx Apr 04 '19 at 02:54
  • 3
    @lynxx - `PrintWriter` can write files without extensions. It is just that it won't automatically *add* an extension. Why? Because it has no idea what the correct extension should be! (File extensions are an OS specific (i.e. non-portable) convention. Java I/O pays them no special attention.) – Stephen C Apr 04 '19 at 02:56
  • @StephenC I tried doing it with the code provided in the question. With just the file name given such as "file", It made a new file as the name given by me but didn't write anything to it. When I added the the extension to the given file, It wrote in the file. – lynxx Apr 04 '19 at 03:00
  • 1
    I agree with @StephenC. Just tested it on Ubuntu 18.04. You would still get a file without extension, which can be opened as normal text file. It may be that you were not expecting it to be there. – Doc Apr 04 '19 at 03:00
  • 1
    I also tested on Ubuntu, and your code works exactly as expected. Modulo a small bug that you have probably already spotted. `print` != `println` :-) – Stephen C Apr 04 '19 at 03:06
  • Most probably file is not in a place where you would expect it to be. IOException would be thrown otherwise. – Antoniossss Apr 04 '19 at 05:51
  • 1
    You can simply check if the file exists after closing the `PrintWriter` using `File.exists` and `File.length`, you will notice it is correct. So you are probably not checking in the correct place. – AxelH Apr 04 '19 at 07:40

0 Answers0