获取类路径上文件的绝对路径 安装程序 “粘贴”是一个文件的修改版本 程序运行时,如果您现在在路径目录中创建一个新的文件或目录,您将看到显示的路径不正确 例如,当我运行它时,当文件的实际路径为/home/hoagy/test/new_file时,它将路径目录中新创建的文件的绝对路径显示为/home/hoagy/test/path/new_file 问题: 如何获得正确的绝对路径 代码 package path.question; 导入java.nio.file.*; 导入静态java.nio.file.StandardWatchEventTypes.*; 导入静态java.nio.file.LinkOption.*; 导入java.nio.file.attribute.*; 导入java.io.*; 导入java.util.*; 公共类WatchDir{ 私人最终观察者服务观察者; 私有最终映射密钥; @抑制警告(“未选中”) 静态WatchEvent强制转换(WatchEvent事件){ 返回(WatchEvent)事件; } 私有无效寄存器(路径目录)引发IOException{ WatchKey key=dir.register(观察者、条目创建、条目删除、条目修改); keys.put(key,dir); } WatchDir(Path dir)抛出IOException{ this.watcher=FileSystems.getDefault().newWatchService(); this.keys=new HashMap(); 注册主任; } void processEvents(){ 对于(;;){ 监视键; 试试{key=watcher.take();} catch(InterruptedException x){return;} Path dir=keys.get(key); if(dir==null){ 继续; } for(WatchEvent事件:key.pollEvents()){ WatchEvent.Kind-Kind=event.Kind(); 如果(种类==溢出){continue;} WatchEvent ev=铸造(事件); 路径名=ev.context(); 路径子项=目录解析(名称); System.out.println(“完整路径:+name.toabsolutionPath().toString()); } 键。重置(); } } 公共静态void main(字符串[]args)引发IOException{ Path dir=Path.get(“./Path”); 新的WatchDir(dir.processEvents(); } }

获取类路径上文件的绝对路径 安装程序 “粘贴”是一个文件的修改版本 程序运行时,如果您现在在路径目录中创建一个新的文件或目录,您将看到显示的路径不正确 例如,当我运行它时,当文件的实际路径为/home/hoagy/test/new_file时,它将路径目录中新创建的文件的绝对路径显示为/home/hoagy/test/path/new_file 问题: 如何获得正确的绝对路径 代码 package path.question; 导入java.nio.file.*; 导入静态java.nio.file.StandardWatchEventTypes.*; 导入静态java.nio.file.LinkOption.*; 导入java.nio.file.attribute.*; 导入java.io.*; 导入java.util.*; 公共类WatchDir{ 私人最终观察者服务观察者; 私有最终映射密钥; @抑制警告(“未选中”) 静态WatchEvent强制转换(WatchEvent事件){ 返回(WatchEvent)事件; } 私有无效寄存器(路径目录)引发IOException{ WatchKey key=dir.register(观察者、条目创建、条目删除、条目修改); keys.put(key,dir); } WatchDir(Path dir)抛出IOException{ this.watcher=FileSystems.getDefault().newWatchService(); this.keys=new HashMap(); 注册主任; } void processEvents(){ 对于(;;){ 监视键; 试试{key=watcher.take();} catch(InterruptedException x){return;} Path dir=keys.get(key); if(dir==null){ 继续; } for(WatchEvent事件:key.pollEvents()){ WatchEvent.Kind-Kind=event.Kind(); 如果(种类==溢出){continue;} WatchEvent ev=铸造(事件); 路径名=ev.context(); 路径子项=目录解析(名称); System.out.println(“完整路径:+name.toabsolutionPath().toString()); } 键。重置(); } } 公共静态void main(字符串[]args)引发IOException{ Path dir=Path.get(“./Path”); 新的WatchDir(dir.processEvents(); } },java,path,classpath,nio,watchservice,Java,Path,Classpath,Nio,Watchservice,更换: //prints the "absolute path" of the name of the file System.out.println("Full path: " + name.toAbsolutePath().toString()); 与 A将比这个长类更好,它有许多问题不需要的代码行。让帮助变得更容易。打得好。按建议编辑。@Hoagy Carmichael感谢您的编辑。帖子回答了你的问题吗? //prints the "absolute path" of the n

更换:

 //prints the "absolute path" of the name of the file 
 System.out.println("Full path: " + name.toAbsolutePath().toString());


A将比这个长类更好,它有许多问题不需要的代码行。让帮助变得更容易。打得好。按建议编辑。@Hoagy Carmichael感谢您的编辑。帖子回答了你的问题吗?
 //prints the "absolute path" of the name of the file 
 System.out.println("Full path: " + name.toAbsolutePath().toString());
 //prints the "absolute path" of the "child" 
 System.out.println("child " + child.toAbsolutePath().toString());