我做了一个FileTest.java,编译后的类文件FileTest.class是在D:\Documents and Settings\Administrator\workspace\lx\bin下,现在我要读一个文本文件1.txt,它和FileTest.class在同一个目录下。
我用
String test=FileTest.class.getResource("").toString();
String testtext=test+1.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(testtext)));
结果不行,请问如何读这个文本文件,注意不要用
String testtext="D:\\Documents and Settings\\Administrator\\workspace\\lx\\bin\\1.txt";
这种方法。
谢谢
我用
String test=FileTest.class.getResource("").toString();
String testtext=test+1.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(testtext)));
结果不行,请问如何读这个文本文件,注意不要用
String testtext="D:\\Documents and Settings\\Administrator\\workspace\\lx\\bin\\1.txt";
这种方法。
谢谢
解决方案 »
- 各种web打印控件的试用评测
- 对日开发中收邮件,字符集如何转换?
- JSP的问题,希望得到大家的帮助!
- FCKeditor换工程后在IE8下不显示
- 如何查看浏览器的请求信息??
- 50分 求 哪位有tomcat与IIS整合的包
- 哪位朋友给我解释下这句sql?
- java.lang.IllegalStateException
- 怎么样来判断手机号码是否合法?
- 报错after end of result set?在执行回复贴子的时候,那位高手知道什么意思呀?
- 会jsp的同学,我刚学,遇到一些很简单的问题想请教一下。能不能留下工作QQ?
- 连接池连接数据库报Communications link failure错误
貌似少个引号--
file:D:\Documents and Settings\Administrator\workspace\lx\bin\
而在FileInputStream中不识别这样的路径。
如果一定要使用这种方法,推荐将file:删除掉。
可以使用
String testtext=test+"1.txt";
testtext = testtext.substring(testtext.indexOf(":") + 1);
试一试.
PS:
LZ代码的第二行貌似缺了个"
我用
String test=FileTest.class.getResource("").toString();
String testtext=test+"1.txt";
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(testtext)));
结果不行,请问如何读这个文本文件,注意不要用
String testtext="D:\\Documents and Settings\\Administrator\\workspace\\lx\\bin\\1.txt";
这种方法。
谢谢
Exception in thread "main" java.io.FileNotFoundException: D:\Documents%20and%20Settings\Administrator\workspace\lx\bin\1.txt (系统找不到指定的路径。)
如何表示1.txt的路径呢,谢谢
String test=FileTest.class.getResource("").getPath();
String testtext=test+"1.txt";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(testtext)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
我代码中没有少引号,是我在帖子里输入时短下了。
所以:
String test=FileTest.class.getResource("").getPath();
java.io.FileNotFoundException: D:\Documents%20and%20Settings\Administrator\workspace\lx\bin\1.txt (系统找不到指定的路径。)
public class FileTest3 {
public static void main(String[] args) throws Exception,IOException {
// TODO Auto-generated method stub
String test=FileTest3.class.getResource("").getPath();
String testtext=test+"1.txt";
testtext = testtext.substring(testtext.indexOf(":") + 1);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(testtext)));
String line = br.readLine();
System.out.println(line);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public class FileTest3 {
public static void main(String[] args) throws Exception,IOException {
String test=FileTest3.class.getResource("").getPath();
String testtext=test+"1.txt";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(testtext)));
String line = br.readLine();
System.out.println(line);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
先打印出你的test看看路径嘛
确保1。txt的路径和FileTest。class在同一个路径
Exception in thread "main" java.io.FileNotFoundException: D:\Documents%20and%20Settings\Administrator\workspace\lx\bin\1.txt (系统找不到指定的路径。)%20是什么东西呀?有这样的路径吗?除了%20之外还会出现%别的什么东西吗?
这是什么原因出现了%,这是URL编码的问题麻,这个路径已经被编码过了,所以要解码,你都没有解码就拿来用自然就有可能出现问题了。只要在程序中加一句:
testtext = java.net.URLDecoder.decode(testtext, "UTF-8");
问题解决。参考URL编码:
http://baike.baidu.com/view/204662.htm
http://www.cnblogs.com/xryyforver/archive/2010/08/20/1804480.html
java.io.FileNotFoundException: D:\Documents%20and%20Settings\Administrator\workspace\lx\bin\1.txt (系统找不到指定的路径。)
说的很明白,就是空格,程序没有任何问题,就是编码问题,楼上就说来了了嘛,真是,是个好人
那么我们在程序中要得到这个xml文件 路径 用如下代码 File file=new File("");
String url=file.getAbsolutePath()+"/a.xml";
System.out.println(url);不知道对你是否有帮助
String test= java.net.URLDecoder.decode(FileTest.class.getResource("").toString(),"UTF-8");
绝对管用;
20楼说的方式是正确的。用classLoader ,是使用相对路径,而不是绝对路径。