Is there a way to use FileOutputStream in a way that if a file (String filename) does not exist, then it will create it?
有没有一种方法可以使用FileOutputStream,如果一个文件(字符串文件名)不存在,那么它将创建它?
FileOutputStream oFile = new FileOutputStream("score.txt", false);
8 个解决方案
#1
219
It will throw a FileNotFoundException
if the file doesn't exist and cannot be created (doc), but it will create it if it can. To be sure you probably should first test that the file exists before you create the FileOutputStream
(and create with createNewFile()
if it doesn't):
如果文件不存在且无法创建(doc),它将抛出FileNotFoundException,但如果可以,它将创建它。为了确保在创建FileOutputStream(如果没有创建FileOutputStream,则使用createNewFile()创建)之前,您可能应该首先测试该文件是否存在:
File yourFile = new File("score.txt");
yourFile.createNewFile(); // if file already exists will do nothing
FileOutputStream oFile = new FileOutputStream(yourFile, false);
#2
23
Before creating a file, it's needed to create all the parent directories.
在创建文件之前,需要创建所有父目录。
Use yourFile.getParentFile().mkdirs()
使用yourFile.getParentFile().mkdirs()
#3
21
You can create an empty file whether it exists or not ...
您可以创建一个空文件,不管它是否存在……
new FileOutputStream("score.txt", false).close();
if you want to leave the file if it exists ...
如果你想保留文件,如果它存在……
new FileOutputStream("score.txt", true).close();
You will only get a FileNotFoundException if you try to create the file in a directory which doesn't exist.
如果您试图在一个不存在的目录中创建文件,那么您将得到一个FileNotFoundException。
#4
19
File f = new File("Test.txt");
if(!f.exists()){
f.createNewFile();
}else{
System.out.println("File already exists");
}
Pass this f
to your FileOutputStream
constructor.
将这个f传递给FileOutputStream构造函数。
#5
11
FileUtils from apache commons is a pretty good way to achieve this in a single line.
apache commons中的FileUtils是实现这一目标的一种很好的方法。
FileOutputStream s = FileUtils.openOutputStream("/home/nikhil/somedir/file.txt")
This will create parent folders if do not exist and create a file if not exists and throw a exception if file object is a directory or cannot be written to. This is equivalent to:
如果不存在,则创建父文件夹;如果不存在,则创建文件;如果文件对象是目录或无法写入,则抛出异常。这相当于:
File file = new File("/home/nikhil/somedir/file.txt");
file.getParentFile().mkdirs(); // Will create parent directories if not exists
file.createNewFile();
FileOutputStream s = new FileOutputStream(file,false);
All the above operations will throw an exception if the current user is not permitted to do the operation.
如果当前用户不允许执行此操作,则上述所有操作都将抛出异常。
#6
3
Create file if not exist. If file exits, clear its content:
创建文件,如果不存在。如果文件退出,清除其内容:
/**
* Create file if not exist.
*
* @param path For example: "D:\foo.xml"
*/
public static void createFile(String path) {
try {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
} else {
FileOutputStream writer = new FileOutputStream(path);
writer.write(("").getBytes());
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
#7
1
You can potentially get a FileNotFoundException
if the file does not exist.
如果文件不存在,您可以获得FileNotFoundException。
Java documentation says:
Java文档说:
Whether or not a file is available or may be created depends upon the underlying platform http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
文件是否可用或可能创建取决于底层平台http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
If you're using Java 7 you can use the java.nio package:
如果您正在使用Java 7,您可以使用Java。nio包:
The options parameter specifies how the the file is created or opened... it opens the file for writing, creating the file if it doesn't exist...
选项参数指定如何创建或打开文件……它打开文件进行写入,如果文件不存在,则创建该文件……
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
#8
0
Just Giving an alternative way to create the file only if doesn't exists using Path and Files.
只有当使用路径和文件不存在时,才提供另一种创建文件的方法。
Path path = Paths.get("Some/path/filename.txt");
Files.createDirectories(path.getParent());
if( !Files.exists(path))
Files.createFile(path);
Files.write(path, ("").getBytes());
#1
219
It will throw a FileNotFoundException
if the file doesn't exist and cannot be created (doc), but it will create it if it can. To be sure you probably should first test that the file exists before you create the FileOutputStream
(and create with createNewFile()
if it doesn't):
如果文件不存在且无法创建(doc),它将抛出FileNotFoundException,但如果可以,它将创建它。为了确保在创建FileOutputStream(如果没有创建FileOutputStream,则使用createNewFile()创建)之前,您可能应该首先测试该文件是否存在:
File yourFile = new File("score.txt");
yourFile.createNewFile(); // if file already exists will do nothing
FileOutputStream oFile = new FileOutputStream(yourFile, false);
#2
23
Before creating a file, it's needed to create all the parent directories.
在创建文件之前,需要创建所有父目录。
Use yourFile.getParentFile().mkdirs()
使用yourFile.getParentFile().mkdirs()
#3
21
You can create an empty file whether it exists or not ...
您可以创建一个空文件,不管它是否存在……
new FileOutputStream("score.txt", false).close();
if you want to leave the file if it exists ...
如果你想保留文件,如果它存在……
new FileOutputStream("score.txt", true).close();
You will only get a FileNotFoundException if you try to create the file in a directory which doesn't exist.
如果您试图在一个不存在的目录中创建文件,那么您将得到一个FileNotFoundException。
#4
19
File f = new File("Test.txt");
if(!f.exists()){
f.createNewFile();
}else{
System.out.println("File already exists");
}
Pass this f
to your FileOutputStream
constructor.
将这个f传递给FileOutputStream构造函数。
#5
11
FileUtils from apache commons is a pretty good way to achieve this in a single line.
apache commons中的FileUtils是实现这一目标的一种很好的方法。
FileOutputStream s = FileUtils.openOutputStream("/home/nikhil/somedir/file.txt")
This will create parent folders if do not exist and create a file if not exists and throw a exception if file object is a directory or cannot be written to. This is equivalent to:
如果不存在,则创建父文件夹;如果不存在,则创建文件;如果文件对象是目录或无法写入,则抛出异常。这相当于:
File file = new File("/home/nikhil/somedir/file.txt");
file.getParentFile().mkdirs(); // Will create parent directories if not exists
file.createNewFile();
FileOutputStream s = new FileOutputStream(file,false);
All the above operations will throw an exception if the current user is not permitted to do the operation.
如果当前用户不允许执行此操作,则上述所有操作都将抛出异常。
#6
3
Create file if not exist. If file exits, clear its content:
创建文件,如果不存在。如果文件退出,清除其内容:
/**
* Create file if not exist.
*
* @param path For example: "D:\foo.xml"
*/
public static void createFile(String path) {
try {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
} else {
FileOutputStream writer = new FileOutputStream(path);
writer.write(("").getBytes());
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
#7
1
You can potentially get a FileNotFoundException
if the file does not exist.
如果文件不存在,您可以获得FileNotFoundException。
Java documentation says:
Java文档说:
Whether or not a file is available or may be created depends upon the underlying platform http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
文件是否可用或可能创建取决于底层平台http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
If you're using Java 7 you can use the java.nio package:
如果您正在使用Java 7,您可以使用Java。nio包:
The options parameter specifies how the the file is created or opened... it opens the file for writing, creating the file if it doesn't exist...
选项参数指定如何创建或打开文件……它打开文件进行写入,如果文件不存在,则创建该文件……
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
#8
0
Just Giving an alternative way to create the file only if doesn't exists using Path and Files.
只有当使用路径和文件不存在时,才提供另一种创建文件的方法。
Path path = Paths.get("Some/path/filename.txt");
Files.createDirectories(path.getParent());
if( !Files.exists(path))
Files.createFile(path);
Files.write(path, ("").getBytes());