如何用java获取桌面路径

Python022

如何用java获取桌面路径,第1张

FileSystemView fsv = FileSystemView.getFileSystemView()

File com=fsv.getHomeDirectory() //这便是读取桌面路径方法

System.out.println(com.getPath())

import java.io.File

import javax.swing.filechooser.FileSystemView

public class Test {

 public static void main(String[] args) {

  FileSystemView fsv=FileSystemView.getFileSystemView()

  //将桌面的那个文件目录赋值给file

  File file=fsv.getHomeDirectory()

  //输出桌面那个目录的路径

  System.out.println(file.getPath())

 }

}

写了一个读取本地文件的方法, File file = new File(htmlFile)FileReader fr = new FileReader(file)BufferedReader br = new BufferedReader(fr)while((s=br.readLine())!=null){ al.add(s)} 在当前类写了main方法测试了一下是可行的, 但是页面某方法想调用该方法,不能实现。 总结问题是:只有放在static方法中可行,在其他地方调用都显示找不到指定文件。 文件结构: 把本地文件放在了web-inf的classes下了,相对路径写的(“/file.txt”)求教为啥static方法可以,其他地方调用不行,这个函数本身不是静态的啊。