0

I'm trying to create a new text file in java by having the user input their desired file name. However, when I look in the directory for the file after I run the code once, it doesn't show up.

import java.util.Scanner;
import java.io.File;

public class TestFile {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the desired name of your file: ");
        String fileName = input.nextLine();
        fileName = fileName + ".txt";

        File file = new File(fileName);
    }
}

Although, when I don't have the user input a file name and just have the code written with the name in quotation marks, the file ends up being created when I look back in the directory.

File file = new File("TestFile.txt")

Why won't it create a file when I try to use the String input from the user?

7
  • 1
    What filename are you entering as input? Commented Nov 8, 2018 at 5:04
  • Should work the exact same. User input or not Commented Nov 8, 2018 at 5:05
  • I tried everything, but I tested it using TestFile123 and or even TestFileName, in theory the concatenation should change it to TestFile123.txt or TestFileName.txt, correct? Commented Nov 8, 2018 at 5:06
  • Could it be related to flushing/closing the File object? Just throwing an idea.. Commented Nov 8, 2018 at 5:07
  • 1
    Try printing fileName before adding in File. Commented Nov 8, 2018 at 5:08

5 Answers 5

3

You must be mistaken because just calling new File(String) won't create a file. It will just create an instance of File class.

You need to call file.createNewFile().

Adding this at the end creates the file:-

if (file.createNewFile()) {
    System.out.println("File created.");
} else {
    System.out.println("File already exists.");
}
Sign up to request clarification or add additional context in comments.

Comments

3

The following code worked for me:

    Scanner input = new Scanner(System.in);
    System.out.print("Enter the desired name of your file: ");
    String fileName = input.nextLine();
    fileName = fileName + ".txt";

    File file = new File(fileName);
    boolean isFileCreated = file.createNewFile();  // New change
    System.out.print("Was the file created? -- ");
    System.out.println(isFileCreated);

The only change made to your code is to call createNewFile method. This worked fine in all cases. Hope this helps.

From the API:

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file. Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.

1 Comment

You "should" be checking the result to ensure that the file was created successfully, just saying ;)
2

Please use below code to solve your issue. You just have to call createNewFile() method it will create file in your project location. You can also provide the location where you want to create file otherwise it will create file at your project location to create file at specified location you have to provide location of your system like below

String fileLocation="fileLocation"+fileName;
File file = new File(fileLocation);


import java.util.Scanner;
import java.io.File;

public class TestFile {
public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the desired name of your file: ");
        String fileName = input.nextLine();
        fileName = fileName + ".txt";

        File file = new File(fileName);
        file.createNewFile();
    }
}

1 Comment

It would be better if you provided some explanation.
1

When faced with issues like this, it's really, really, really important to go hit the JavaDocs, because 90% of the time, it's just a misunderstanding of how the APIs work.

File is described as:

An abstract representation of file and directory pathnames.

This means that creating an instance of File does not create a file nor does the file have to exist, it's just away of describing a virtual concept of a file.

Further reading of the docs would have lead you to File#createNewFile which is described as doing:

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

Comments

1

When you initialize your File file = new File("TestFile.txt"), it is not created yet. You should write something to your file using FileWriter or others.

File f = new File(fileName);
FileWriter w = new FileWriter(f);
w.write("aaa");
w.flush();

or using f.createNewFile() as suggested in other answer. Then you can see your file and its content.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.