我想看一个文件更改目录.我在java.nio中使用了WatchService.我可以成功监听文件创建的事件.但我不能听文件修改事件.我查了官方的java教程,但还在苦苦挣扎.
这是源代码.
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class MainWatch {
public static void watchDirectoryPath(Path path) {
// Sanity check - Check if path is a folder
try {
Boolean isFolder = (Boolean) Files.getAttribute(path,
"basic:isDirectory", NOFOLLOW_LINKS);
if (!isFolder) {
throw new IllegalArgumentException("Path: " + path
+ " is not a …Run Code Online (Sandbox Code Playgroud) 我的Java程序想要读取一个文件,该文件可以被写入其中的另一个程序锁定.我需要检查文件是否被锁定,如果是,请等待它是免费的.我该如何实现这一目标?
Java程序在Windows 2000服务器上运行.
在尝试以编程方式删除文件之前,如何检查文件是否已打开?
这样的事情
if (file is open){
// close it first
}
// delete file
Run Code Online (Sandbox Code Playgroud) 我正在为新文件轮询文件系统,这是由某人从Web界面上传的.现在我必须处理每个新文件,但在此之前我想确保我正在处理的文件是完整的(我的意思是说它完全通过Web界面传输).
如何在处理之前验证文件是否已完整下载?
我需要帮助的只是测试文件是否已打开。
这是我所拥有的:
public static void main(String[] args) {
//Prompt user to input file name
SimpleIO.prompt("Enter File name: ");
String fileName = SimpleIO.readLine();
//Create file object
File file = new File (fileName);
//Check to see if file is opened
if (!file.exists()){
System.out.println("The file you entered either do not exist or the name is spelled wrong.\nProgram is now being terminated.\nGoodbye!");}
}
Run Code Online (Sandbox Code Playgroud) 如何检查可以删除Java中的文件?
例如,如果test.txt在另一个程序中打开了文件,则无法将其删除。而且我必须在实际删除之前知道它,所以我不能这样做:
if (!file.delete()) { ... }
Run Code Online (Sandbox Code Playgroud)
而且srcFile.canWrite()也不起作用。