好的,这是一个简单的答案,
假设您已输入" /myfile.txt"在名称变量中,
创建路径和文件名(我已经在路径中指定了只想从用户输入添加文件名)。
String name,address,pnum;
Scanner op= new Scanner(System.in);
System.out.println("name:");
name=op.next();
System.out.println("address:");
address=op.next();
System.out.println("Phone Number:");
pnum=op.next();
String p="E:\\EarnestCabs\\Cab\\";
String an=op.next();
String a=".txt";
String po=p+an+a;
try{
FileWriter fw= new FileWriter(po,true);
BufferedWriter pw= new BufferedWriter(fw);
pw.write(name+"#");
pw.write(address+"#");
pw.write(pnum+"#");
pw.newLine();
pw.flush();
pw.close();
}
catch(IOException e)
{
System.out.println("Error"+e);
}