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();
try
{
File file = new File(cShell.Currentpath, filename);
file.createNewFile();
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();
if(myhashData.containsKey(Input))
{
ICommand myCommand=myhashData.get(Input);
myCommand.Execute();
}
else
{
System.out.println("Invalid Command");
}
}
}
*ICommand is an interface.