[英]how to create a .dat file in java
我想在java中创建一个不存在的.dat文件。 我不知道如何手动创建它。 文件f =新文件(文件); 是用于文件的代码,但究竟是不存在的文件的代码是什么。 换句话说,创建一个新文件。
像File f = new File(file);
这样的语句File f = new File(file);
不会在磁盘上创建文件。 类java.io.File
仅表示文件路径,而不是磁盘上的实际文件。
要创建新文件,请为其打开FileOutputStream
,然后可以使用该文件将数据写入文件。
OutputStream out = new FileOutputStream("C:\\Temp\\filename.dat");
try {
// Write data to 'out'
} finally {
// Make sure to close the file when done
out.close();
}
很简单:
File myFile = new File("your_file.bat");
myFile.createNewFile();
问题有点不清楚,但这将创建一个新的空文件:
new File("x.dat").createNewFile();
如果你想在其中包含一些数据,请使用文件的FileOutputStream(如果丢失也会创建它,如果已经存在则会覆盖)。
在创建文件之前,还应检查是否确实不存在。
File file = new File("abc.dat");
if(!file.exists())
{
boolean created = file.createNewFile();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.