導航:首頁 > 文件教程 > 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獲取文件所在路徑相關的資料

熱點內容
中興光纖貓f460埠映射教程 瀏覽:41
怎麼弄迷你世界編程 瀏覽: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

友情鏈接