java获取文件的路径|在java项目中如何获取某个文件的路径

java获取文件的路径|在java项目中如何获取某个文件的路径的第1张示图

① 如何查看java读取文件的路径

File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:123456789101112131415public class PathTest{public static void main(String[] args){File file = new File(".\\src\\");System.out.println(file.getAbsolutePath());try{System.out.println(file.getCanonicalPath());} catch (IOException e){e.printStackTrace();}}}getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。下面是上面程序在我电脑上的输出:G:\xhuoj\konw\.\src\G:\xhuoj\konw\src\

② JAVA如何得到文件路径

用import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.filechooser.FileFilter;public class FileChooserDemo extends JPanel { static final long serialVersionUID = 5854418136127725290L; public class ExtensionFilter extends FileFilter { private String extensions[]; private String description; public ExtensionFilter(String description, String extension) { this(description, new String[] { extension }); } public ExtensionFilter(String description, String extensions[]) { this.description = description; this.extensions = (String[]) extensions.clone(); } public boolean accept(File file) { if (file.isDirectory()) { return true; } int count = extensions.length; String path = file.getAbsolutePath(); for (int i = 0; i < count; i++) { String ext = extensions[i]; if (path.endsWith(ext) && (path.charAt(path.length() – ext.length()) == '.')) { return true; } } return false; } public String getDescription() { return (description == null ? extensions[0] : description); } } public FileChooserDemo() { JButton jb = new JButton("Open File Viewer"); add(jb); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser("."); // chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); FileFilter type1 = new ExtensionFilter("Java source", ".java"); FileFilter type2 = new ExtensionFilter("Image files", new String[] { ".jpg", ".gif", "jpeg", "xbm" }); FileFilter type3 = new ExtensionFilter("HTML files", new String[] { ".htm", ".html" }); chooser.addChoosableFileFilter(type1); chooser.addChoosableFileFilter(type2); chooser.addChoosableFileFilter(type3); chooser.setAcceptAllFileFilterUsed(true); chooser.setFileFilter(type2); // Initial filter setting int status = chooser.showOpenDialog(FileChooserDemo.this); if (status == JFileChooser.APPROVE_OPTION) { File f = chooser.getSelectedFile(); System.out.println(f); } } }; jb.addActionListener(listener); } public static void main(String args[]) { JFrame f = new JFrame("Enhanced File Example"); JPanel j = new FileChooserDemo(); f.getContentPane().add(j, BorderLayout.CENTER); f.setSize(300, 200); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

③ JAVA中如何得到文件路径

java文件中获得路径Thread.currentThread().getContextClassLoader().getResource("") //获得资源文件(.class文件)所在路径ClassLoader.getSystemResource("")Class_Name.class.getClassLoader().getResource("")Class_Name.class .getResource("/") Class_Name.class .getResource("") // 获得当前类所在路径System.getProperty("user.dir") // 获得项目根目录的绝对路径System.getProperty("java.class.path") //得到类路径和包路径打印输出依次如下:file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/com/xml/imp/F:\work_litao\uri_testF:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar

④ java 获取本地文件夹路径怎么写

构造File对象,使用File对象取上级目录,再版取绝对路权径 File f = new File("c:\\temp\\01\\1.txt"); if(f.exists()){ System.out.println(f.getParentFile().getAbsolutePath()); }

⑤ java获取某个文件夹的路径怎么写

File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:public class PathTest{ public static void main(String[] args) { File file = new File(".\\src\\"); System.out.println(file.getAbsolutePath()); try { System.out.println(file.getCanonicalPath()); } catch (IOException e) { e.printStackTrace(); } }}getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。下面是上面程序在我电脑上的输出:G:\xhuoj\konw\.\src\G:\xhuoj\konw\src\

⑥ 如何用java代码得到文件夹的路径(即一个方法,输入文件夹名称,返回路径)

最原始的方法可以遍历所有盘符文件public class Path{ private final List<File> list=new ArrayList<File>(); private String directory; public Path(String s) { this.directory=s; } private void genPath() { File[] roots=File.listRoots(); for(File root:roots) searchExists(root);} private void searchExists(File file) { String tempPath=file.getAbsolutePath(); if(tempPath.contains(directory) &&(tempPath.substring(tempPath.lastIndexOf(directory)).equals(directory))) list.add(file); if(file.isDirectory()&&file.list()!=null) { File[] files=file.listFiles(); for(File f:files) { searchExists(f); } } } public void listPath() { genPath(); for(File file:list) System.out.println(file.getAbsolutePath()); } public static void main(String[] args) throws UnsupportedEncodingException { Path p=new Path("CS1.6"); p.listPath(); }}测试正确,但性能太差,考虑用好的文件查找算法和多线程来作

⑦ java 根据文件获取文件名及路径的方法

通过File类获取文件,然后通过以下两种方法获取绝对路径和名称。返回类型为String获取绝对路径:file.getAbsolutePath()获取名称: file.getName()

⑧ java怎么获取本地文件路径

Java中获取用户本地路径的方法:用request对象来获取:request.getRequestURL();或者用:request.getRequestURI();

⑨ 在java项目中如何获取某个文件的路径

如果是在tomcat等服务器中运行的话,用ServletContext中的getRealPath()方法可以获取指定文件的绝对路径,如:getRealPath("/WEB-INF/db.xml");

⑩ java 怎么获取指定路径下的文件

//根据你的要求修改了一下代码,现在已经能将某文件夹下的所有指定类型文件复制到//指定文件夹下了import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;public class ReadFiles {public static final String FILTER = "xml";public static final String SRC_DIR = "E:\\StudyData";// 待扫描的文件夹public static final String DES_DIR = "E:\\testdata";// 复制后的目标文件夹public static void main(String[] args) {long a = System.currentTimeMillis();scanDir(SRC_DIR, DES_DIR);System.out.println("共花费时间:"+(System.currentTimeMillis() – a)/1000+"秒");}public static void scanDir(String srcPath, String desPath) {File dir = new File(srcPath);File[] files = dir.listFiles();if (files == null)return;for (int i = 0; i < files.length; i++) {if (files[i].isDirectory()) {scanDir(files[i].getAbsolutePath(), desPath);} else {String strFileName = files[i].getAbsolutePath().toLowerCase();File(strFileName, desPath + files[i].getName());}}}public static void File(String srcName, String destName) {if (srcName.endsWith(FILTER)) {System.out.println("正在复制文件 "+srcName+" 至 "+destName);try {BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcName));BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destName));int i = 0;byte[] buffer = new byte[2048];while ((i = in.read(buffer)) != -1) {out.write(buffer, 0, i);}out.close();in.close();} catch (Exception ex) {ex.printStackTrace();}}}}

未经允许不得转载:山九号 » java获取文件的路径|在java项目中如何获取某个文件的路径

赞 (0)