java 系统找不到指定的文件,小弟不知错在哪里,请指教,谢谢!

mauricehawk 2009-02-24 10:38:35
package com.dao;

import java.io.*;

public class GoonieResult {
public static void readFileByChars(String fileName){
File file = new File(fileName);
System.out.println(fileName+"=====");
Reader reader = null;
try{
System.out.println("By chars");
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while((charread = reader.read(tempchars))!=-1){
if((charread == tempchars.length)&&(tempchars[tempchars.length -1]!='\r')){
System.out.print(tempchars);
}else{
for(int i=0; i<charread; i++){
if(tempchars[i] == '\r'){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
}catch(Exception e1){
e1.printStackTrace();
}finally{
if(reader != null){
try{
reader.close();
}catch(IOException e){

}
}
}
}

public static void main(String args[]){
String fileName = "D:\\test.txt";
GoonieResult.readFileByChars(fileName);
}
}

D:\test.txt=====
By chars
java.io.FileNotFoundException: D:\test.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.dao.GoonieResult.readFileByChars(GoonieResult.java:14)
at com.dao.GoonieResult.main(GoonieResult.java:43)


...全文
2844 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzbang 2009-02-25
  • 打赏
  • 举报
回复
经调试,已经ok了
*1、 D:\test.txt 文件指示方式错了 ,正确方式 D:/test.txt

*2、该文件要存在,如果不存在,会抛出异常




import java.io.*; 

public class GoonieResult {
public static void readFileByChars(String fileName){
File file = new File(fileName);
System.out.println(fileName+"=====");
Reader reader = null;
try{
System.out.println("By chars");
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while((charread = reader.read(tempchars))!=-1){
if((charread == tempchars.length)&&(tempchars[tempchars.length -1]!='\r')){
System.out.print(tempchars);
}else{
for(int i=0; i <charread; i++){
if(tempchars[i] == '\r'){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}
}catch(Exception e1){
e1.printStackTrace();
}finally{
if(reader != null){
try{
reader.close();
}catch(IOException e){

}
}
}
}

public static void main(String args[]){
String fileName = "D:/test.txt";
GoonieResult.readFileByChars(fileName);
}
}

a3070 2009-02-25
  • 打赏
  • 举报
回复
d:\\test.txt
huadis 2009-02-25
  • 打赏
  • 举报
回复
这么一个问题热度很高呀
lzbang 2009-02-25
  • 打赏
  • 举报
回复
在java中,文件路径要用 "/",而不能使用"\"的,否则报错
lzbang 2009-02-25
  • 打赏
  • 举报
回复


D:\test.txt 文件指示方式错了


正确方式 D:/test.txt
starscc 2009-02-25
  • 打赏
  • 举报
回复
检查下文件是否存在,或者你隐藏了扩展文件名......
gzfdxy 2009-02-24
  • 打赏
  • 举报
回复
package com.dao;

import java.io.*;

public class GoonieResult {
public static void readFileByChars(String fileName) {
File file = new File(fileName);
System.out.println(fileName + "=====");
Reader reader = null;
try {
System.out.println("By chars");
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length) &&
(tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {

}
}
}
}

public static void main(String args[]) {
String fileName = "D:\\test.txt";
GoonieResult.readFileByChars(fileName);
}
}
zhoushijin5201314 2009-02-24
  • 打赏
  • 举报
回复
d:\test.txt
这个文件不存在吧
看看是不是文件名写错了啊
chenchuanfeng001 2009-02-24
  • 打赏
  • 举报
回复
如果你电脑设置是隐藏已知扩展名,那么事实上文件名可能是test.txt.txt
改为String fileName = "D:\\test.txt.txt";
或者文件重命名为test.
日上三千 2009-02-24
  • 打赏
  • 举报
回复
既然是FileInputStream说找不到你的文件,那是真的没有这个文件。你本机上真的有d:\test.txt这个文件吗?
Daniel_Cao_ 2009-02-24
  • 打赏
  • 举报
回复
系统里没有D:\test.txt 文件
chenwei023 2009-02-24
  • 打赏
  • 举报
回复
确定文件名写对没?
chxy148 2009-02-24
  • 打赏
  • 举报
回复
原因可能是其中之一:
a.此文件不存在
b.扩展名被隐藏了
createWang 2009-02-24
  • 打赏
  • 举报
回复

package com.dao;

import java.io.*;

public class GoonieResult {
public static void readFileByChars(String fileName) {
File file = new File(fileName);
System.out.println(fileName + "=====");
Reader reader = null;
try {
System.out.println("By chars");
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length) &&
(tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {

}
}
}
}

public static void main(String args[]) {
/*

1.确定机器上是否真的存在此文本文件
2.机器配置的后缀名是不是被隐藏了

*/
String fileName = "D:\\test.txt";
GoonieResult.readFileByChars(fileName);
}
}
狂浪吻血 2009-02-24
  • 打赏
  • 举报
回复
File file = new File(fileName);
执行下file.getPAth或者getName之类的方法,打印出来看看。

snowfox3761 2009-02-24
  • 打赏
  • 举报
回复
File file = new File(fileName);
我注意到了这行,你是不是要生成这个文件后直接读取?
你在这行下面执行下file.getPAth或者getName之类的方法,打印出来看看。
怀疑文件生成的不对。
Allen114 2009-02-24
  • 打赏
  • 举报
回复
应该是隐藏了扩展名
ouyangxiaokang6 2009-02-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 chenchuanfeng001 的回复:]
如果你电脑设置是隐藏已知扩展名,那么事实上文件名可能是test.txt.txt
改为String fileName = "D:\\test.txt.txt";
或者文件重命名为test.
[/Quote]
up,应该就是你的文件不存在,好好查查

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧