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

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

1. java如何获取class所对应的.java文件的路径

你问这个问题就代抄表没有搞清楚java,java的源文件是.java后缀的文件,源文件经过编译之后是后缀名为.class的类文件,运行这些类文件不需要java源文件,也就是说源文件经过编译之后就跟类文件没有任何关系了,源文件不管放在什么地方甚至是删掉,都没关系,又怎么能通过类文件来获得源文件的地址呢?

2. 如何获得当前Java文件的路径

public class Test { public static void main(String[] args) { String path = "Test.java"; File file = new File(path); System.out.println(file.getAbsoluteFile()); }} —–运行结果:D:\workspaces\studyStruts2\Test.java不加任何路径,就是指当版前路径望采纳权

3. 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\

4. 在java中怎么获得,本文件的路径

File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:

publicclassPathTest{publicstaticvoidmain(String[]args){Filefile=newFile(".\src\");System.out.println(file.getAbsolutePath());try{System.out.println(file.getCanonicalPath());}catch(IOExceptione){e.printStackTrace();}}}

getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。

下面是上面程序在我电脑上的输出:

G:xhuojkonw.srcG:xhuojkonwsrc

5. 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

6. eclipse编写java时如何获得当前文件路径

package com.package2;import java.io.File;public class T5 {public static void main(String[] args) {T test=new T();test.get();}}class T{public void get(){File file = new File(this.getClass().getResource("/").getPath());System.out.println(file.toString()); }}

7. 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); }}

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

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

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

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

10. 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文件路径|在java项目中如何获取某个文件的路径

赞 (0)