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)

解决方案 »

  1.   

    系统里没有D:\test.txt 文件
      

  2.   

    既然是FileInputStream说找不到你的文件,那是真的没有这个文件。你本机上真的有d:\test.txt这个文件吗?
      

  3.   

    如果你电脑设置是隐藏已知扩展名,那么事实上文件名可能是test.txt.txt
    改为String fileName = "D:\\test.txt.txt";
    或者文件重命名为test. 
      

  4.   

    d:\test.txt
    这个文件不存在吧
    看看是不是文件名写错了啊
      

  5.   

    File file = new File(fileName); 
    我注意到了这行,你是不是要生成这个文件后直接读取?
    你在这行下面执行下file.getPAth或者getName之类的方法,打印出来看看。
    怀疑文件生成的不对。
      

  6.   

    File file = new File(fileName);
    执行下file.getPAth或者getName之类的方法,打印出来看看。 
     
      

  7.   


    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);
        }
    }
      

  8.   

    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);
        }
    }
      

  9.   

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

  10.   

    在java中,文件路径要用 "/",而不能使用"\"的,否则报错
      

  11.   

    经调试,已经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);