Java 如何创建一个名称来自用户输入的txt文件
我想创建一个txt文件并将其存储在特定位置,如C:\或C:\用户。我还希望用户输入文件名作为输入。我已经用下面的代码尝试过了,但从来都不起作用Java 如何创建一个名称来自用户输入的txt文件,java,file-io,Java,File Io,我想创建一个txt文件并将其存储在特定位置,如C:\或C:\用户。我还希望用户输入文件名作为输入。我已经用下面的代码尝试过了,但从来都不起作用 import java.io.File; import java.util.Scanner; public class cITF implements ICommand{ @Override public void Execute() { // TODO Auto-generated method stub
import java.io.File;
import java.util.Scanner;
public class cITF implements ICommand{
@Override
public void Execute() {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Enter a file name");
String 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");
}
}
}
其中Currentpath
是cShell
类的成员,相当于C:\(保存txt文件的目录)。当它运行时,虽然没有创建任何内容,但会输出“创建的文件”
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("ls", new cLS());
myhashData.put("gd", new cGD());
myhashData.put("md", new cMD());
myhashData.put("rnd", new cRND());
myhashData.put("del", new cDEL());
myhashData.put("hd", new cHD());
myhashData.put("uhd", new cUHD());
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");
}
}
}
import java.util.HashMap;
导入java.util.Scanner;
公共类cShell{
静态字符串Currentpath=“C:\\”;
公共字符串Current=Currentpath;
静态HashMap myhashData=newhashmap();
公共静态void main(字符串[]args)
{
put(“ls”,新的cLS());
put(“gd”,新的cGD());
put(“md”,newcmd());
put(“rnd”,new cRND());
put(“del”,新的cDEL());
put(“hd”,new cHD());
myhashData.put(“uhd”,新的cUHD());
put(“ltf”,new cITF());
myhashData.put(“nbc”,新的cNBC());
put(“gdb”,新的cGDB());
put(“Tedit”,new cTedit());
系统输出打印(Currentpath+“>”);
扫描仪扫描=新扫描仪(System.in);
字符串输入=scan.nextLine();
if(myhashData.containsKey(输入))
{
ICommand myCommand=myhashData.get(输入);
myCommand.Execute();
}
其他的
{
System.out.println(“无效命令”);
}
}
}
我建议您首先尝试使用一些目录,在这些目录中您肯定可以访问并打印文件路径。然后,您可以检查这些文件是否通常由您的代码创建
public static void main(final String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a file name");
String filename = scan.nextLine();
try
{
File file = new File("/tmp", filename);
System.out.println("path=" + file.getAbsolutePath());
file.createNewFile();
System.out.println("File created");
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("Failed to create file");
}
}
编辑:下面的答案不反映原始问题。请忽略,注意评论
public static void main(String[] args) {
// open filechooser at user's current location (this will be in your
// eclipse workspace if running from eclipse (/NetBeans)
JFileChooser jfc = new JFileChooser(System.getProperty("user.dir"));
// just for safety; we're saving files anyhow
jfc.setMultiSelectionEnabled(false);
jfc.setDialogTitle("Enter file name and select location . .");
// null should be the name of the parent JFrame
int returnVal = jfc.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
String path = jfc.getSelectedFile().getAbsolutePath();
writeToCustomFile(path);
}
}
public static void writeToCustomFile(String path) {
File f = new File(path);
// redundancy check, feel free to remove
if(!f.getPath().equals("")) {
// create parent directories if they don't exist
f.getParentFile().mkdirs();
try {
f.createNewFile();
System.out.println("File created");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Failed to create file");
}
}
}
答案很简单
您可能正在使用计算机上运行的Windows Vista或最新操作系统。
Windows不允许您在C:\folder[root]中创建文件。
您可以保留系统相关文件夹以外的其他驱动器[D:\或E:\或C:\ somefolder],它将完美工作。
这是一个操作系统限制,以避免恶意软件/安全相关问题尝试在sysout中打印完整的文件名,看看会发生什么happens@vishram0709为什么?控制台输入有什么问题?@zencv您所说的全名是什么意思。。??我是否应该删除路径并只放入文件名..?抱歉,伙计们,当我运行此程序时,它会显示“未能创建文件”,而不是我上面提到的“已创建文件…”知道为什么文件没有在目录cShell.Currentpath..中创建吗?cShell.Currentpath的值是多少?这个路径存在吗?我想如果我输入C:\而不是cShell.Currentpath…就可以了。比如说,如果我输入C:\\sample.txt而不是提供用户输入,那么它就是成功创建的文件…但这不是我想要的…我想让用户提供文件名..我的情况有点不同。我的程序应该像命令提示符一样工作,用户应该输入一些命令,比如说“lft sample.txt”,程序应该在cShell.CurrentpathAh中给出的目录中创建一个名为sample.txt的文件,对此表示抱歉。我不知道你的cShell类做什么,我也不能在谷歌上找到它。你能提供一个完整的代码工作示例吗?@user3483389,这在Windows 7和最新版本的Windows中是不可能的。刚刚测试过。顺便说一句,创建文件和文件夹是不同的。Reddy,如果是权限问题,他会得到java.io.IOException:Access is denied
/“创建文件失败”。他正在“创建文件”。有什么简单的方法吗。。??我认为这很简单,不知怎么的,我没有解决它…@user3483389,你是否更改了任何其他硬盘分区的路径,如果你有,或者,将其更改为你的用户文件夹(C:\users\your username)