Java 关于文件文件=新文件(路径)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19702659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
About File file = new File(path)
提问by Leem.fin
The Java.iO.Filedocument says the following words about its constructor which takes the pathname
:
该java.io.File的文件说,有关它的构造函数下面的话更是把pathname
:
public File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.
public File(String pathname)
通过将给定的路径名字符串转换为抽象路径名来创建一个新的 File 实例。如果给定的字符串是空字符串,则结果是空的抽象路径名。
But what if the pathname
points to a file which is already existing?
但是如果pathname
指向一个已经存在的文件呢?
File file = new File(PATH_TO_AN_EXISTING_FILE);
Does the above file
instance represent a fresh new file (with the existing one be deleted?) Ordoes it represent the existing file ?
上面的file
实例是代表一个新文件(删除现有文件?)还是代表现有文件?
采纳答案by Francis
What the documentation says is that it will create a new File
instance. This mean it will create a new instance in memory of the File
class.
文档说的是它将创建一个新File
实例。这意味着它将在File
类的内存中创建一个新实例。
This object will point to a file on you file system. However, if the file exists, it will not create a new file.
该对象将指向您文件系统上的一个文件。但是,如果文件存在,则不会创建新文件。
回答by afk5min
The java.io.File
class represents a path on some file system. It is not directly bound to a file. You are notopening a file when you create a File
instance.
在java.io.File
类表示一些文件系统的路径。它不直接绑定到文件。你是不是当你创建一个打开文件File
的实例。
A File
object is merely an object on the heap. Yes, it does have fields and methods that imply that this object represents a real file (or a directory: see the ambiguity?).
You can create File
objects for files/directories that do not exist: nothing will happen to the file system; the File
instances will be created. After all, a File
is just a descriptor.
一个File
对象只是堆上的一个对象。是的,它确实有字段和方法暗示这个对象代表一个真实的文件(或目录:看到歧义?)。您可以File
为不存在的文件/目录创建对象:文件系统不会发生任何事情;该File
实例将被创建。毕竟,aFile
只是一个描述符。
Furthermore, you can create several File
objects with different paths (esp. when one is absolute and others are relative from different parent paths), but they will all point to the same file/directory when they are actually evaluated (by opening a file with In/OutputStream
, Reader/Writer
; when checking with exists()
or creating: createFile()
, createDirectory()
.
此外,您可以创建多个File
具有不同路径的对象(尤其是当一个对象是绝对路径而其他对象是来自不同父路径的相对对象时),但它们在实际评估时都将指向同一个文件/目录(通过打开带有In/OutputStream
, Reader/Writer
; 检查exists()
或创建时:createFile()
, createDirectory()
.
回答by dasblinkenlight
I think the documentation is a little confusing: creating a new File
object in Java does not mean creation of a new file in your file system. The File
object is merely an abstract representation of file and directory pathname, it may or may not represent a real file on disk or on a network storage.
我认为文档有点混乱:File
在 Java 中创建一个新对象并不意味着在您的文件系统中创建一个新文件。该File
对象仅仅是文件和目录路径名的抽象表示,它可能代表也可能不代表磁盘或网络存储上的真实文件。
It is more or less equivalent to a String
representing an address of something: when you write
它或多或少相当于String
表示某物的地址:当你写
String str = "1600 Pennsylvania Ave NW, Washington, DC 20500";
you create a string with an address of an existing building. There is no other connection between the string str
that you created and The White House that happens to be located at that address.
您使用现有建筑物的地址创建一个字符串。str
您创建的字符串与恰好位于该地址的白宫之间没有其他联系。
The only difference between a File
created with an existing path name and a file created with a non-existent path name is that the call of exists()
on the former will return true
, while the same call on the later would return false
.
File
使用现有路径名创建的文件和使用不存在的路径名创建的文件之间的唯一区别是,exists()
对前者的调用将返回true
,而对后者的相同调用将返回false
。
回答by WilQu
A File
is not directly linked to an actual file on the file system. If the file exists, it will point to that file. If the file doesn't exist, it will not create it. exist()
will return false.
AFile
不直接链接到文件系统上的实际文件。如果文件存在,它将指向该文件。如果文件不存在,它不会创建它。exist()
将返回假。
回答by ???v?т?
This is a very confusingly named class.
这是一个非常容易混淆的类。
A File
object represents a file path, not an actual file. So when you create a File
object you do not change anything on the filing system. Conceptually, it's no different to a String
.
一个File
对象代表一个文件路径,而不是一个实际的文件。因此,当您创建File
对象时,您不会更改文件系统上的任何内容。从概念上讲,它与String
.
In java.nio, the class has been renamed to (the much more intuitive) Path
.
在 java.nio 中,该类已重命名为(更直观)Path
。
回答by Raj
File f=new File("C://Existing_file")
File f=new File("C://Existing_file")
above line indicates already existed file not the new one to be created file. File class instance always refers to IO operations and also it always refers to already consisted file
上面一行表示已经存在的文件不是要创建的新文件。文件类实例总是指IO操作,也总是指已经组成的文件
回答by Sam
By creating new instance
通过创建新实例
File f= new File("ABC.txt");
This new object of file will point to a file named ABC.txtin your system, if present. If the ABC.txtfile is not there, then the file object simply does not point to any file.
这个新的文件对象将指向系统中名为ABC.txt的文件(如果存在)。如果ABC.txt文件不存在,则文件对象根本不指向任何文件。