导航:首页 > 文件教程 > java获取文件所在路径

java获取文件所在路径

发布时间:2023-02-26 19:37:52

A. java怎么获取resources下的文件路径

1.读取本地的xml文件,需要注意对应的路径
//读取xml文件,xmlFile为读取文件的路径DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance;DocumentBuilderbuilder=factory.newDocumentBuilder;Documentdocument=builder.parse(xmlFile);NodeListnodeList=document.getElementsByTagName(thisTag);//指定标签()的节点集合for(itni=0;i<nodeList.getLength;i++){//循环获取每个节点信息Nodenode=nodeList.item(i);NamedNodeMapattributes=node.getAttributes;for(intj=0;j<attributes.getLength;j++){Nodeattribute=attributes.item(j);System.out.println(attribute.getNodeName+":"+attribute.getNodeValue);}}
注意:getElementsByTagName方法只是属于document与Element的方法

所以,当针对某个Node查找对应的节点时,需要先强制转换为Element
ElementnodeToElement=(Element)node;NodeListosNodeList=nodeToElement.getElementsByTagName(thisTag);//thisTag为指定标签
2.读取txt文件

一般的数据存储都是键值对的方式在文件中记录,开发人员多是根据已知的键,从文件中取得对应的值。

例如Config.txt中内容为:

name=jack

sex=boy

要从java程序中读取该文件的内容
Fileconfig_file=newFile("./Config");//此处使用相对路径Stringconfig_file_fullpath=config_file.getAbsoluteFile.toString;readConfigconfig=newreadConfig(config_file_fullpath);Stringname=config.get("name");//name为jack//对获取的数据进行处理//...
3.读取.csv文件

csv文件一般为表格,是多行多列的数据,列对应相应不同的属性,java实现逐行读取每列单元格的值。

B. 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_test
F:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar

C. java读取文件路径

你的头是e://tttt11.PNG不是e://tttt11//1.PNG???
如果头是e://tttt11//1.PNG,filepath没有用,去掉。
这段这么改:
for (i=0; i <= str.length(); i += 2) {
if (i == str.length() - 1 || i == str.length() - 2)
break;
else
fileName = str.substring(i);
}

// System.out.println(filePath);
if(ii!=100)
fileName = str.substring(i);
...........后面不改.

如果头是e://tttt11.PNG,那这个和下面的循环规律不一样,大概写下:
if(ii==1)filePath=".PNG";
把我上面修改后的代码加到else里就行了(用我上面修改后的代码,不然你的尾还是显不出来).

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

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

E. java中获取文件路径的几种方式

获取当前类的所在工程路径;
如果不加“/”
File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);结果:C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test
获取当前类的绝对路径;第二种:File directory = new File("");//参数为空
String courseFile = directory.getCanonicalPath() ;
System.out.println(courseFile);结果:C:\Documents and Settings\Administrator\workspace\projectName
获取当前类的所在工程路径;第三种:URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);结果:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt
获取当前工程src目录下selected.txt文件的路径第四种:System.out.println(System.getProperty("user.dir"));结果:C:\Documents and Settings\Administrator\workspace\projectName
获取当前工程路径第五种:System.out.println( System.getProperty("java.class.path"));结果:C:\Documents and Settings\Administrator\workspace\projectName\bin获取当前工程路径

F. 如何获得当前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
不加任何路径,就是指当版前路径
望采纳权

G. java怎么获取本地文件路径

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

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

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

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

阅读全文

与java获取文件所在路径相关的资料

热点内容
u盘文件给删了 浏览:737
vuejsoauth2 浏览:78
2017微信支付日 浏览:81
机械臂编程如何开发 浏览:21
标书U盘PDF文件要不要签字 浏览:222
ps软件文件复制到d盘 浏览:148
一般工业固废招标文件内容 浏览:583
网站建设报价怎么算 浏览:66
三星a7000升级安卓502 浏览:486
word2010清除分隔符 浏览:781
乐视怎么切换网络 浏览:425
cad打印pdf文件名称与cad不一致 浏览:815
电气与可编程控制是什么 浏览:67
文件名中允许使用英文竖线吗 浏览:531
编程猫在哪里 浏览:775
win8共享文件夹访问权限 浏览:380
cad文件显示为只读不能保存怎么办 浏览:703
如何在系统里忘记网络 浏览:24
中小企业出口数据在哪里找 浏览:715
win8和xp传文件 浏览:75

友情链接