我正在尝试为我正在制作的游戏制作保存文件,这些文件中将包含多个 txt 文件。我的问题是文件夹和 txt 文件都不会在我指定的目录中创建自己,这是文件夹的代码:
File folde = new File("c:/Users/Mike/Desktop/Saves/bob/" + save);
try{
if (!folde.exists()) {
if (folde.mkdirs()) {
System.out.println("Created new save file");
} else {
System.out.println("Did not create new save file");
}
}
}finally{
System.out.println("Folder found.");
}
这是该文件的代码:
try{
PrintWriter writer = new PrintWriter("c:/Users/Mike/Desktop/javafiles/Saves/" + save + "/Stats.txt", "UTF-8");
writer.println(Stats.Health);
writer.println(Stats.Strength);
writer.println(Stats.Constitution);
writer.println(Stats.Dexterity);
writer.println(Stats.Inteligence);
writer.println(Stats.Wisdom);
writer.println(Stats.Charisma);
writer.close();
} catch (IOException x) {
System.err.println("Could not create save file.");
}
这是整个班级:
package files.maintain;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import vars.all.*;
public class SaveFile {
MainStart Start = new MainStart();
Stats Stats = new Stats();
public void save(){
Scanner in = new Scanner(System.in);
System.out.println("What should I save as?");
String save = in.nextLine();
File folde = new File("c:/Users/Dave/Desktop/Saves/bob/" + save);
try{
if (!folde.exists()) {
if (folde.mkdirs()) {
System.out.println("Created new save file");
} else {
System.out.println("Did not create new save file");
}
}
}finally{
System.out.println("Folder found.");
}
try{
PrintWriter writer = new PrintWriter("c:/Users/Dave/Desktop/javafiles/Saves/" + save + "/Stats.txt", "UTF-8");
writer.println(Stats.Health);
writer.println(Stats.Strength);
writer.println(Stats.Constitution);
writer.println(Stats.Dexterity);
writer.println(Stats.Inteligence);
writer.println(Stats.Wisdom);
writer.println(Stats.Charisma);
writer.close();
} catch (IOException x) {
System.err.println("Could not create save file.");
}
}
}
我从控制台得到的响应是:
- 我应该保存为什么?
- 鲍勃
- 找到文件夹。
- 无法创建保存文件。*
我的输入是粗体的,我检查了这个网站:http ://www.mkyong.com/java/how-to-create-directory-in-java/ 和 java 教程:http ://docs.oracle.com/ javase/tutorial/essential/io/file.html 但这不起作用。
谢谢!