Can somebody help me with this scenario?
有人可以帮我解决这个问题吗?
I have a method which when run generates a number of files. The files can take anywhere from a couple of seconds to a minute to be generated so instead of just waiting for 2 minutes everytime I am trying to look at the number of files in the folder then check every second up to 2 minutes for the file count to go up.
我有一个方法,运行时会生成许多文件。这些文件可能需要几秒到一分钟的时间才能生成,所以我每次尝试查看文件夹中的文件数时都要等待2分钟,然后每隔2秒检查一次文件计数上去。
If the file count doesn't go up in that 2 minutes I just want to carry on with the rest of the method instead of throwing an exception and exiting.
如果文件计数在2分钟内没有上升,我只想继续使用方法的其余部分而不是抛出异常并退出。
The problem is the first time it runs when it gets to new File(generatedFilesFolder).listFiles().length;
it throws a null pointer exception because the generatedFilesFolder is empty at this point.
问题是它第一次运行到新文件(generatedFilesFolder).listFiles()。length;它抛出一个空指针异常,因为此时generatedFilesFolder为空。
I could do this check after the generate.next() but then I would have to sleep which would defeat the point of what I am trying to archive.
我可以在generate.next()之后执行此检查,但之后我将不得不睡觉,这将打败我想要存档的点。
Please can anyone tell me a better way? I tried changing the int to an Integer instead but it still threw an exception.
请有人能告诉我一个更好的方法吗?我尝试将int更改为Integer,但它仍然引发异常。
int actualFilesGenerated = new File(generatedFilesFolder).listFiles().length;
generate.next();
// / Now I want to give each file 2 minutes to generate otherwise catch and
// carry on with rest of method.
try {
int currentFilesGenerated = new File(generatedFilesFolder).listFiles().length;
int counter = 0;
while ((currentFilesGenerated < actualFilesGenerated) & counter < 120) {
Thread.sleep(1000);
currentFilesGenerated = new File(generatedFilesFolder).listFiles().length;
counter++;
System.out.println("actualFilesGenerated: " + actualFilesGenerated
+ " currentFilesGenerated: " + currentFilesGenerated + "counter: "
+ counter);
}
} catch (InterruptedException e1) {
e1.printStackTrace();
}
///rest of method here
5 个解决方案
#1
3
You can use File.exists() to check the existence of a File
您可以使用File.exists()来检查文件是否存在
File f = new File(generatedFilesFolder)
if(f.exists()) {
// file exists and do the operation
}
else {
f.createNewFile(); //or f.mkdir() to create directory
}
#2
1
The problem is the first time it runs when it gets to new File(generatedFilesFolder).listFiles().length; it throws a null pointer exception because the generatedFilesFolder is empty at this point.
问题是它第一次运行到新文件(generatedFilesFolder).listFiles()。length;它抛出一个空指针异常,因为此时generatedFilesFolder为空。
This cannot be true. If generatedFilesFolder
is effectively a path to a directory but there are no files in it, this will not throw an NPE but will return an empty array.
这不可能是真的。如果generatedFilesFolder实际上是一个目录的路径但是没有文件,这将不会抛出NPE但会返回一个空数组。
Therefore you need to generate your directory before going any further:
因此,您需要先生成目录,然后再继续:
final File file = new File(generatedFilesFolder);
if (file.exists()) {
if (!file.isDirectory())
throw new IllegalStateException("not a directory");
} else {
if (!files.mkdirs())
throw new IllegalStateException("cannot create directory");
}
// go on
Finally: if you use Java 7, forget about File
. Use Files
. Which has a directory watcher...
最后:如果您使用Java 7,请忘记File。使用文件。哪个有目录观察者......
#3
0
you can use exists() method of File class to check if that file is present, e.g.
您可以使用File类的exists()方法来检查该文件是否存在,例如
File f=new File(path);
if(f.exists()) {
.....
}
#4
0
there is exist method. So check if derectory exist before you get list of files
存在方法。因此,在获得文件列表之前,请检查是否存在derectory
#5
0
make a check before using it like.
在使用它之前做一个检查。
File file = new File(generatedFilesFolder);
if(file.exists() && file.isDirectory()) {
// do something
}
#1
3
You can use File.exists() to check the existence of a File
您可以使用File.exists()来检查文件是否存在
File f = new File(generatedFilesFolder)
if(f.exists()) {
// file exists and do the operation
}
else {
f.createNewFile(); //or f.mkdir() to create directory
}
#2
1
The problem is the first time it runs when it gets to new File(generatedFilesFolder).listFiles().length; it throws a null pointer exception because the generatedFilesFolder is empty at this point.
问题是它第一次运行到新文件(generatedFilesFolder).listFiles()。length;它抛出一个空指针异常,因为此时generatedFilesFolder为空。
This cannot be true. If generatedFilesFolder
is effectively a path to a directory but there are no files in it, this will not throw an NPE but will return an empty array.
这不可能是真的。如果generatedFilesFolder实际上是一个目录的路径但是没有文件,这将不会抛出NPE但会返回一个空数组。
Therefore you need to generate your directory before going any further:
因此,您需要先生成目录,然后再继续:
final File file = new File(generatedFilesFolder);
if (file.exists()) {
if (!file.isDirectory())
throw new IllegalStateException("not a directory");
} else {
if (!files.mkdirs())
throw new IllegalStateException("cannot create directory");
}
// go on
Finally: if you use Java 7, forget about File
. Use Files
. Which has a directory watcher...
最后:如果您使用Java 7,请忘记File。使用文件。哪个有目录观察者......
#3
0
you can use exists() method of File class to check if that file is present, e.g.
您可以使用File类的exists()方法来检查该文件是否存在,例如
File f=new File(path);
if(f.exists()) {
.....
}
#4
0
there is exist method. So check if derectory exist before you get list of files
存在方法。因此,在获得文件列表之前,请检查是否存在derectory
#5
0
make a check before using it like.
在使用它之前做一个检查。
File file = new File(generatedFilesFolder);
if(file.exists() && file.isDirectory()) {
// do something
}