导航:首页 > 文件教程 > 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获取文件所在路径相关的资料

热点内容
怎么弄迷你世界编程 浏览:702
qq发手机里的文件找不到 浏览:832
百度云文件有密码忘记了怎么办 浏览:469
扫描文件扫到哪里了 浏览:85
为什么淘宝app是黑色的 浏览:17
如何在cad中把图形输出为pdf文件 浏览:535
文件夹横签 浏览:988
extjs5mvc 浏览:614
win7如何安装数据库 浏览:647
informix数据库倒数卸数 浏览:983
华硕p7h55mplus升级 浏览:240
servlet调用jsp 浏览:481
文件的命名原则有哪些 浏览:352
苹果的文件管理是哪个 浏览:387
智能黑板如何给pdf文件做批注 浏览:788
哈弗智联app如何绑定二手车 浏览:728
cad文件不多可是异常增大 浏览:872
苹果手机怎样将音频文件导入剪映 浏览:432
2016秋季飞歌导航升级 浏览:151
电脑字符串怎么编程 浏览:381

友情链接