请问读写文件能不能用相对路径? 当然可以用相对路径,估计你的路径错了你先看看当前路径 new File("").getAbsolutePath() 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 getAbsolutePath() 得到包括文件名在内的整个路径。File f= new File("test.ini") 这样的结果是JVM去和文件夹com同级的目录下去找test.ini文件,而你的test.ini文件实际上是在..\com\jv01\这个路径下,所以会提示找不到文件 /** @(#)ReadFile.java */package com.quickpoint.ReadFile;import java.io.*;/** ReadFile 是一个读写文件的类 */public class ReadFile { public static void main(String[] args) { String fileName = "test.txt"; if( args.length > 1 ) { fileName = args[0]; } System.out.println( ReadFile.readStringFromFile(fileName)); } public static String readStringFromFile(String fileName) { BufferedReader br = null; try { br = new BufferedReader( new FileReader( new File(fileName))); StringBuffer buf = new StringBuffer(); String tmpStr = null; while( (tmpStr = br.readLine()) != null ) { buf.append( tmpStr ); } br.close(); br = null; return buf.toString(); } catch (FileNotFoundException ex) { ex.printStackTrace(); return null; } catch (IOException ex) { ex.printStackTrace(); return null; } finally { if (null != br) { try { br.close(); } catch(IOException ex) { // do nothing } } } } }目录结构:test.txtcom | quickpoint | ReadFile | ReadFile.java这个是采用相对路径的。你把要读的文件放到包的最外边,不是class的当前目录中就可以了。 test.ini放在你执行程序的路径下,就能找到了. 可以的,都放在同一个目录里边就可以了或者相对于project的相对路径../conf/test.ini 随机存取1000个0到9的数,从中找出12345第一次连续出现的位置 网页游戏java服务端开发工程师主要负责做什么事情? 有2个简单问题,请大家帮忙~~~ 关于velocity中断求救 输出有非法字符 为什么在linux(FC6系统)下面eclipse不能运行debug 编译声称的class文件或者是jar文件是否可以与其他语言交互的? 关于抓取网页内容的一个问题 如何使用java实现下面问题 java 如何屏蔽掉 系统的热键(如ALT+TAB) 两个JList如何传递元素. 问个问题,关于在应用程序上获取绝对路径的一点点疑惑
File f= new File("test.ini") 这样的结果是JVM去和文件夹com同级的目录下去找test.ini文件,而你的test.ini文件实际上是在..\com\jv01\这个路径下,所以会提示找不到文件
@(#)ReadFile.java
*/
package com.quickpoint.ReadFile;
import java.io.*;/**
ReadFile 是一个读写文件的类
*/
public class ReadFile {
public static void main(String[] args) {
String fileName = "test.txt";
if( args.length > 1 ) {
fileName = args[0];
}
System.out.println( ReadFile.readStringFromFile(fileName));
}
public static String readStringFromFile(String fileName) {
BufferedReader br = null;
try {
br = new BufferedReader(
new FileReader(
new File(fileName)));
StringBuffer buf = new StringBuffer();
String tmpStr = null;
while( (tmpStr = br.readLine()) != null ) {
buf.append( tmpStr );
}
br.close();
br = null;
return buf.toString();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
return null;
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
finally {
if (null != br) {
try {
br.close();
} catch(IOException ex) {
// do nothing
}
}
}
}
}目录结构:
test.txt
com
| quickpoint
| ReadFile
| ReadFile.java
这个是采用相对路径的。
你把要读的文件放到包的最外边,不是class的当前目录中就可以了。
或者相对于project的相对路径../conf/test.ini