2

How do I name a file using Scanner? For some reason, Java's FileWriter won't accept a pre-defined String as an input. The code below is simplified to what I mean : `

import java.io.*;

public class Example{
    public static void main(String[] arg) {
        String title;
        Scanner input = new Scanner(System.in);

        title = input.next();
        try {
            FileWriter fw = new Filewriter(title);
        } catch (IOException ex) {}
        input.close()
    }
}

I don't know why it doesn't work. I've checked other questions online and none of them seem to help.

EDIT: Okay, I edited the IOException and here's what I got from the stack trace;

java.io.FileNotFoundException: entries\awdwad
.txt (The filename, directory name, or volume label syntax is incorrect)
    at java.base/java.io.FileOutputStream.open0(Native Method)
    at java.base/java.io.FileOutputStream.open(FileOutputStream.java:292)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:235)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:156)
    at java.base/java.io.FileWriter.<init>(FileWriter.java:82)
    at main.JournalInput.main(JournalInput.java:26)

I'm sorry, I'm still confused about this.

EDIT EDIT: I found out the problem. There was a Delimiter I forgot and removing it apparently fixed it. I didn't put it in the code above either. Sorry for wasting everyone's time on this.

10
  • 3
    You should include more information than just "won't accept" - is it an error (if so, show us the full error message and stack trace), does it give wrong results (if so, what is the expected vs. actual result), or something else? Also, Filewriter is misspelled; it should be FileWriter with a capital W. Commented Dec 19, 2019 at 11:08
  • 2
    Java's FileWriter won't accept ... what does that mean? don't silently ignore IOException, it probably have answer to your question Commented Dec 19, 2019 at 11:08
  • And what do you mean "name a file"? Commented Dec 19, 2019 at 11:09
  • Does this answer your question? Rename a file using Java Commented Dec 19, 2019 at 11:09
  • the code does not compile because of the spelling error pointed out by kaya3. You could add a System.out.println(title); to see if it is the expected file name and also print the stack trace of the exception you receive Commented Dec 19, 2019 at 11:11

2 Answers 2

1

Scanner parses text into tokens, x.txt will easily become 3 tokens. Instead of next() use nextLine(). Still better would be to not use Scanner.

A FileNotFoundException under Windows also happens a lot, when the file extension is hidden (.txt, .docx, ...).

Sign up to request clarification or add additional context in comments.

Comments

1

Mostly depends of your input to scanner for example if you will pass filename along with / then it won't work. Posting same code tested on three different inputs.
1. example
2. example.txt
3. /example.txt

public class Example {


     public static void main(String[] arg) {
            String title;
            Scanner input = new Scanner(System.in);

            title = input.next();
            try {
                FileWriter fw = new FileWriter(title);
                System.out.print("Working with "+title);

            } catch (IOException ex) {
                System.out.print("Error: with "+ex);
                ex.printStackTrace(System.out);
            }
            input.close();
        }

}

will work on first two input but will throw exception in case of last input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.