jawncopper
jawncopper

Reputation: 9

How to create a new text file based on the user input in Java?

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?

Upvotes: 0

Views: 9835

Answers (5)

GauravRai1512
GauravRai1512

Reputation: 844

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

Upvotes: 2

Sid
Sid

Reputation: 4997

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.

Upvotes: 3

Tran Ho
Tran Ho

Reputation: 1500

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.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347194

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.

Upvotes: 1

Kartik
Kartik

Reputation: 7917

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.");
}

Upvotes: 3

Related Questions