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");