1

I tried to make a console app that creates a file and writes text to it. What did I do wrong here?

package Writer;

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


public class main {

    public static void main(String args[]) {

    System.out.println("What would you like your SimpleText file name to be?");
    Scanner uInputName = new Scanner(System.in);
    String fileName = uInputName.nextLine();

    File file = new File(fileName);

    FileWriter fw = FileWriter;
    BufferedWriter Text = new BufferedWriter(fw);
    {

        System.out.println("What would you like to write in: " + fileName);

        Scanner uInputText = new Scanner(System.in);
        String fileText = uInputText.nextLine();

        fw.write(fileText);
        Text.newLine();
    }

    System.out.println("Okay. File saved.");

}

3 Answers3

0
  1. This is totally illegal

    FileWriter fw = FileWriter;
    
  2. You probably want this

    BufferedWriter Text = new BufferedWriter(new FileWriter(file));
    
  3. You have an unnecessary code block

    {
    
    }
    
  4. Your code is going to throw an IOExcpeption, so you have two options. Either

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

    Or

    try {
        BufferedWriter Text = new BufferedWriter(new FileWriter(file));
       ....
       Text.write(inputText);
       Text.close();
    } catch (IOException ex) {
       ex.printStackTrace();
    }
    
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

First, let us begin with FileWriter fw. fw should be a FileWriter object and so, you need to create a FileWriter object.

FileWriter fw = new FileWriter(file);

Now, notice that you may want to be more specific so it's worth checking the different constructors in the FileWriter API

Also, the writing won't THROW and exception, it MIGHT THROW an exception (saying that it will throw an exception is saying that your code will always fail and in that case, exception handling is "patching" it. So I would recommend adding a catch clause. However, remember that YOU must ALWAYS close the writers yourself (Java does not do that unless specified otherwise) and so, you need to either try with resource

    try (FileWriter fw = new FileWriter(file);
         BufferedWriter bw = new BufferedWriter(fw)){
          ....
          writer.write(inputText);
} catch (IOException ex) {
   ex.printStackTrace();
}

or, add a finally clause

FileWriter fw = new FileWriter(file);
BufferedWriter writer = new BufferedWriter(fw);
  try {
   ....
   writer.write(inputText);
} catch (IOException ex) {
   ex.printStackTrace();
} finally{
writer.close();
fw.close();
}

Because the garbage collector does not close streams and if the exception is thrown inside the try then it will not reach the close() command but instead go to catch (and then finally) without closing the stream.

Elayeek
  • 72
  • 4
0

As others have said. it is difficult to know all the things that are wrong without knowing what exactly you are trying to accomplish.

And again, as PopoFibo mentioned, the thing that probably stands out most and seems to be syntactically incorrect is:

FileWriter fw = FileWriter;

So instead of listing what might be wrong, here is an example that might help you.

In first line you you enter in console you specify the file name - like you seem to want to do.

Each subsequent line you input into console is written to that file. Entering empty line will terminate the input.

public class Program {

    public static void main(String[] args) {

        System.out.println("Enter file name:");
        Scanner input = new Scanner(System.in);
        String fileName = null;
        while (fileName == null) {
            if (input.hasNextLine()) {
                fileName = input.nextLine();
            }
        }

        try {
            FileWriter fw = new FileWriter(fileName);
            String newLine = System.getProperty("line.separator");

            System.out.println("Enter lines (enter empty line to terminate):");

            String line = null;
            while (true) {
                if (input.hasNextLine()) {
                    line = input.nextLine();
                    if (isTerminatingEntry(line)) {
                        break;
                    }
                    fw.write(line + newLine);
                }
            }

            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        input.close();
    }

    private static boolean isTerminatingEntry(String line) {
        // add some condition that will cause you to stop reading input
        // in this example, empty line will terminate the input 
        return line.length() == 0;
    }
}
mrzli
  • 16,799
  • 4
  • 38
  • 45
  • I don't know how to answer to an edit, but an edit made by Elayeek was incorrect, since `fw.close()` in `finally` without its own try/catch will not compile. I put `fw.close()` at the end of try to simplify things, and worked with assumption there would be no exceptions after `FileWriter fw = new FileWriter(fileName);` until the end of try block. If I needed somthing more robust I would probably do it like [this](http://stackoverflow.com/a/2699270/520229) or like [this](http://stackoverflow.com/a/2699273/520229). – mrzli Jan 28 '14 at 18:32