① java我想要写某个文件,怎样获取文件的相对路径。文件位置:
URL url=this.getClass.getResource("/test.txt");
应当是少个/吧! LZ试试
② java怎么获取文件的相对路径
一个例子,如果有一个Test文件夹里面有test.java和hello.txt.如果你想用test.java操作hello.txt
只要在test.java中这样写File file=new File("hello.txt");//这样就是相对路径。如果文件结构是
Test文件夹
... |------test.java
... |------hello.txt
... |------source文件夹
................. |---------world.txt
如果想在test.java中操作world.txt。只要这样写File file=new File("source/world.txt");
另外,在web开发中/代表项目文件夹根目录,当然也有可能代替webapps,区分方法是:如果/开头的uri是给浏览器解析则/代表webapps,如果是给服务器后台解析,则代表项目文件
③ 请问在java代码中怎样获取某个磁盘上的某个文件的绝对路径和相对路径有知道的麻烦告诉我!谢谢!
啥子意来思》
String getAbsolutePath()
返回抽象路径名自的绝对路径名字符串。
这个相对路径不好说:因为:
假设:
D盘 a\ b\ c\这里有个1.txt
相对于c\来说是:1.txt
相对于a\来说是:b\c\1.txt
呵呵,你自己看
------------------------
那就全盘搜索吧(楼上的方法)
Linux系统只用搜索一次
④ Java 获取相对路径问题 System.getProperty("user.dir");
不要用user.dir,这是用户的根目录。
也不要在application下测试web下的东西。回
类似于这么写:答
String uploadPath = getContext().getServletContext().getRealPath(
"/")
+ File.separator + "upload" + File.separator;
⑤ java web中读取文件,相对路径怎么写
相对路径的话,可以先获取到当前文件的编译路径,之后在找到想找文件的路径的思路来实现内。容
举例:
XMLS.class.getClass().getResourceAsStream("/test/test.txt");
解释:XMLS.class.getClass()是获取当前的类编译路径,之后通过getResourceAsStream的形式即可找到要读取的文件的路径。
备注:这个方法中后面的路径也可以通过截取的形式来进行路径获取,实现原理都是找到当前类路径,之后通过相对位置找到另外文件路径。
⑥ 如何在java web项目中获得相对路径
第一步: 先获得复classpath路径制
Stringclasspath=this.getClass().getResource("/").getPath().replaceFirst("/","");
这样子可以得到classpath路径,类似于:
F:/projects/JavaStudyParent/study-springmvc-junit-test/target/springmvc-junit-test/WEB-INF/classes/
然后把WEB-INF/classes截取就能获得WebAPP目录啦:
StringwebappRoot=classpath.replaceAll("WEB-INF/classes/","");
得到的结果就是:
F:/projects/JavaStudyParent/study-springmvc-junit-test/target/springmvc-junit-test/
通过这个路径你就能获取该文件夹下的所有文件啦
⑦ java中 读取文件时想用相对路径,代码怎么写
test
|
src
|
t090417
|
test.properties
Read.java
test.properties:
TEST=test
Read.java:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class Read {
public static String TEST ;
private static Properties loadPropertyFile() throws FileNotFoundException,IOException{
Properties properties = new Properties() ;
FileInputStream fs = new FileInputStream("src/t090417/test.properties");
properties.load(fs);
return properties ;
}
public static void loadProperty(){
try{
Properties properties = loadPropertyFile();
TEST = properties.getProperty("TEST");
System.out.println("read from properties: "+TEST);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
loadProperty();
}
}
其中用的就是相对路径!
⑧ 怎样在JAVA文件中获取该项目的相对路径
File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。
⑨ java 怎么把文件的绝对路径转换成相对路径
把文件的绝对路径转换成相对路径
java.io.File提供了两个方法:
File
getAbsoluteFile()返回一个File对象专实例
Returns the absolute form of this abstract pathname.
String
getAbsolutePath() 返回一个字符串
Returns the absolute pathname string of this abstract pathname.
java.nio.file.Path提供的属
Path
toAbsolutePath()
Returns a Path object representing the absolute path of this
path.
⑩ java 配置文件 相对路径
把读取的代码改下
文件的路径可以用 表示
this.getClass().getResource("configFile.ini").getPath()
也可以用
this.getClass().getResource("").getPath()+"configFile.ini"
这样应该就可以了