Is there any simple way to create a txt file with its name come from the user input. Also the file will be stored in a specific location such as C:\Users for example. Here is what I do but doesn't work.

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

public class cITF implements ICommand{

    public void Execute() {
    // TODO Auto-generated method stub

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a file name");
        filename = scan.nextLine();

            File file = new File(cShell.Currentpath, filename);

            System.out.println("File created");

        catch(Exception e)
            System.out.println("Failed to create file");


I also have another class cShell which consist of the Currentpath (C:).

import java.util.HashMap;
import java.util.Scanner;

    public class cShell {

        static String Currentpath="C:\\";
        public String Current = Currentpath;

        static HashMap<String, ICommand> myhashData=new HashMap<String, ICommand>();

        public static void main(String[] args)

            myhashData.put("ltf", new cITF());
            myhashData.put("nbc", new cNBC());
            myhashData.put("gdb", new cGDB());
            myhashData.put("Tedit", new cTedit());

            System.out.print(Currentpath+"> ");

            Scanner scan = new Scanner(System.in);
            String Input = scan.nextLine();

                ICommand myCommand=myhashData.get(Input);
                System.out.println("Invalid Command");

*ICommand is an interface.

2 Years
Discussion Span
Last Post by JamesCherrill

You declared a new File but you also have to declare an ObjectOutputStream (TextOutputStream) in order to save it on your desktop.

Try that?

Edited by SynThiicQ


"It doesn't work" tells us nothing. Did you get an exception? Was the file created but with the wrong location and/or name? Etc?
The exception on line 22 (catch) contains details of any errors. It is a bad mistake not to use that. Add an e.printStackTrace(); inside the catch clause to see the full error message.

Declaring an output stream will not affect the creation of a new file when you use createNewFile(), so that's not the problem.

Print the values of cShell.Currentpath and filename just after line 16 to confirm that they are what you expected.


First of all, thank you for you replies...I have just recently solved it and it's something wrong with how I define the path to save the file. I tried File file = new File(cShell.Currentpath+"\"+filename); and it works. I also found out that it cannot works in the C: itself but I have to run it inside another directory for example C:\Users


OK! Thanks for sharing the solution with us. Please mark this "solved" for our knowledge base.


Edited by JamesCherrill

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.