【发布时间】:2016-11-25 04:23:53
【问题描述】:
我想将数据存储在一个二进制文件中,如果它不存在它应该生成文件夹,但看起来情况并非如此,我呼吁如果文件不存在它应该生成它。
public Account(int accountid, String name, String lastname, double balance, AccountState state) {
this.name = name;
this.lastname = lastname;
this.accountID = accountid;
this.balance = balance;
this.state = state;
try {
accountfile = new File("./Clients/" + lastname + "/" + name + "/" + "BalanceInfo " + accountid + ".ACC");
if(!accountfile.exists()) {
accountfile.createNewFile();
}
fos = new FileOutputStream(accountfile);
oos = new ObjectOutputStream(fos);
oos.writeObject("balance: " + balance);
oos.writeObject("state: " + state.toString().toLowerCase());
} catch(IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("Account sucessfully Created");
}
但是,它会产生以下错误
The system cannot find the path specified
Account sucessfully Created
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at dinges.Account.<init>(Account.java:44)
at dinges.Main.main(Main.java:10)
我也不生成文件,有点混乱。
【问题讨论】:
-
.的相对路径 - “当前工作目录” - 不推荐,因为它取决于起点(IDE、bat、双击)。使用System.getProperty("user.home") + "/..."之类的。 -
@JoopEggen 我想我会使用它,但如果我将它导出为可运行的 jar,它不会只生成我放置 jar 的文件夹吗?这种方式似乎更容易使用。
-
是的,当他们只需双击 jar 时。也许我是固执己见。我的做法是提示用户等等。
标签: java binaryfiles