java 从字符串检查文件是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17677796/
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
Check if file exists from string
提问by Alberto Rossi
This is the code I use when I try to read some specific text in a *.txt file:
这是我在尝试读取 *.txt 文件中的某些特定文本时使用的代码:
public void readFromFile(String filename, JTable table) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(filename));
String a,b,c,d;
for(int i=0; i<3; i++)
{
a = bufferedReader.readLine();
b = bufferedReader.readLine();
c = bufferedReader.readLine();
d = bufferedReader.readLine();
table.setValueAt(a, i, 0);
table.setValueAt(b, i, 1);
table.setValueAt(c, i, 2);
table.setValueAt(d, i, 3);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the reader
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
And it is called in this way:
它是这样调用的:
readFromFile("C:/data/datafile.txt", table1)
The problem is the following: the 1st time I open the program the *.txt file I'm going to read does not exist, so I thought I could use the function exists()
. I have no idea about what to do, but I tried this:
问题如下:我第一次打开程序时,我要读取的 *.txt 文件不存在,所以我想我可以使用该函数exists()
。我不知道该怎么做,但我试过这个:
if(("C:/data/datafile.txt").exists()) {
readFromFile("C:/data/datafile.txt", table1)
}
It is not working because NetBeans gives me a lot of errors. How could I fix this?
它不起作用,因为 NetBeans 给了我很多错误。我怎么能解决这个问题?
回答by hmjd
String
has no method named exists()
(and even if it did it would not do what you require), which will be the cause of the errors reported by the IDE.
String
没有命名的方法exists()
(即使它做了它也不会做你需要的),这将是 IDE 报告错误的原因。
Create an instance of File
and invoke exists()
on the File
instance:
创建一个实例File
并exists()
在该File
实例上调用:
if (new File("C:/data/datafile.txt").exists())
{
}
回答by Marc-Andre
Note: This answer use classes that aren't available on a version less than Java 7.
注意:此答案使用在低于 Java 7 的版本上不可用的类。
The method exists()
for the object Stringdoesn't exist. See the String documentationfor more information. If you want to check if a file exist base on a path you should use Path
with Files
to verify the existence of the file.
exists()
对象String的方法不存在。有关更多信息,请参阅字符串文档。如果要根据路径检查文件是否存在,则应使用Path
withFiles
来验证文件是否存在。
Path file = Paths.get("C:/data/datafile.txt");
if(Files.exists(file)){
//your code here
}
Some tutorial about the Path
class : Oracle tutorial
And a blog post about How to manipulate files in Java 7
关于该Path
课程的一些教程: Oracle 教程
和关于如何在 Java 7 中操作文件的博客文章
Suggestion for your code:
I'll point to you the tutorial about try-with-resourcesas it could be useful to you. I also want to bring your attention on Files#readAllLinesas it could help you reduce the code for the reading operation. Based on this method you could use a for-each loop to add all the lines of the file on your JTable
.
对您的代码的建议:
我会向您提供有关try-with-resources的教程,因为它可能对您有用。我还想提请您注意Files#readAllLines,因为它可以帮助您减少读取操作的代码。基于此方法,您可以使用 for-each 循环将文件的所有行添加到JTable
.
回答by No Idea For Name
you can use this code to check if the file exist
您可以使用此代码检查文件是否存在
Using java.io.File
使用java.io.File
File f = new File(filePathString);
if(f.exists()) { /* do something */ }
回答by Sunspar
You need to give it an actual File object. You're on the right track, but NetBeans (and java, for that matter) has no idea what '("C:/data/datafile.txt")' is.
你需要给它一个实际的 File 对象。您走在正确的轨道上,但 NetBeans(和 java,就此而言)不知道 '("C:/data/datafile.txt")' 是什么。
What you probably wanted to do there was create a java.io.File
object using that string as the argument, like so:
您可能想要做的是java.io.File
使用该字符串作为参数创建一个对象,如下所示:
File file = new File ("C:/data/datafile.txt");
if (file.exists()) {
readFromFile("C:/data/datafile.txt", table1);
}
Also, you were missing a semicolon at the end of the readFromFile
call. Im not sure if that is just a typo, but you'll want to check on that as well.
此外,您在readFromFile
通话结束时遗漏了一个分号。我不确定这是否只是一个错字,但您也需要检查一下。
If you know you're only ever using this File
object just to check existence, you could also do:
如果你知道你File
只是为了检查存在而使用这个对象,你也可以这样做:
if (new File("C:/data/datafile.txt").exists()) {
readFromFile("C:/data/datafile.txt", table1);
}
回答by Alderath
If you want to ensure that you can read from the file, it might even be appropriate to use:
如果你想确保你可以从文件中读取,它甚至可能适合使用:
if(new File("C:/data/datafile.txt").canRead()){
...
}
as a condition, in order to verify that the file exists andyou have sufficient permissions to read from the file.
作为条件,为了验证文件是否存在并且您有足够的权限从文件中读取。