package test.io;import java.io.*;public class ReadFile{
public static void main(String[] args){
byte[] buff = new byte[1024];
int n;
FileInputStream fis = null;
System.out.println("输入文件名:");
try{

fis = new FileInputStream(args[0]);
while((n=fis.read(buff)) != -1){
System.out.write(buff,0,n);
}
}catch(FileNotFoundException f){
System.out.println("文件未找到!");
f.printStackTrace(); }catch(IOException ee){
System.out.println("读错误。");
}finally{
try{
fis.close();
}catch(IOException e){
System.out.println("文件错误");
                System.exit(1);
}
}

}
}
编译通过了,运行时不等我输入数据就运行了,错误如下:
E:\self>java test.io.ReadFile
Exception in thread "main" java.lang.NullPointerException
        at test.io.ReadFile.main(ReadFile.java:25)哪位有抽点时间帮我看看吧。

解决方案 »

  1.   

    由于你没有输入参数,因此构造的对象fis不存在,因此在调用fis.close()会抛出空指针异常
      

  2.   


    package test.io;import   java.io.*; public   class   ReadFile{ 
    public   static   void   main(String[]   args){ 
    byte[]   buff   =   new   byte[1024]; 
    int   n; 
    FileInputStream   fis   =   null; 
    System.out.println("输入文件名:"); 

    try{ 
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String str=br.readLine();
    if(null==str)
    return;
    fis   =   new   FileInputStream(str); 

    while((n=fis.read(buff))   !=   -1){ 
    System.out.write(buff,0,n); 
    //FileOutputStream fos=new FileOutputStream("d:\1.txt");
    //fos.write(buff,0,n);
    } //end while
    }catch(FileNotFoundException   f){ 
    System.out.println("文件未找到!"); 
    f.printStackTrace(); 
    }catch(IOException   ee){ 
    System.out.println("读错误。"); 
    }catch(NullPointerException e){
    System.err.println("没有输入参数");
    }catch(ArrayIndexOutOfBoundsException e){
    e.printStackTrace(); 
    }finally{ 
    try{ 
    if(null!=fis)
    fis.close(); 
    }catch(IOException   e){ 
    System.out.println("文件错误"); 
    System.exit(1); 

    } //end try...catch()
    } //end main()
    } //end class ReadFile
      

  3.   


    import java.io.*;public class FileInputStreamTest {
        public FileInputStreamTest() {
        }
        public static void main(String[] args){
            FileInputStream fls = null;
            File file = null;
    int n;
            try{
                file = new File("FileInputStreamTest.java");
                fls = new FileInputStream(file);
                while((n=fls.read())!=-1){
                    //char ch = (char)fls.read();
                    System.out.write (n);
                }
                fls.close();
            }catch(FileNotFoundException fnfe){
                System.out.println ("找不到文件!!");
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
        }
    }
      

  4.   

    感谢你对我的问题的回答,我想通过DOS用键盘传参数,我记得好像是用main(String[] args)里的参数 args[i]就行来着 但现在不知怎么用了,当时好像没用到流。
    我这个程序现在运行的问题是我还没有从键盘输入数据,他就运行了。我是说在DOS下输入参数!!这样的话该怎么做啊??不知道我说的意思你明白了吗?
      

  5.   

    main里的参数是运行的时候就指定的
    比如说java a 参数一 参数二 参数三
    你 要在运行 的时候就需要用到流了.System.in对像.
      

  6.   

    Dos下输入参数 跟在类名后面 按顺序是 args[0] args[1]...java [-options] class [args...]具体如:
    java test.io.ReadFile abc.txt
      

  7.   

    E:\self> java   test.io.ReadFile  filepath  试下这样。
    你的COMMANDLINE都没有肯定不行
      

  8.   

    按有几个群,你不妨加进去,可以和大家一起讨论啊.........46986340,28039577,4804620                                                                    
    在那里看看有无能回答你的,谢谢,LZ,甭忘了给俺分哦,谢谢LZ
      

  9.   

    package test.io;import   java.io.*; public   class   ReadFile{ 
        public   static   void   main(String[]   args){ 
            byte[]   buff   =   new   byte[1024]; 
            int   n; 
            FileInputStream   fis   =   null; 
            System.out.println("输入文件名:"); 
            
            try{ 
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                String str=br.readLine();
                if(null==str)
                    return;
                fis   =   new   FileInputStream(str); 
                
                while((n=fis.read(buff))   !=   -1){ 
                    System.out.write(buff,0,n); 
                    //FileOutputStream fos=new FileOutputStream("d:\1.txt");
                    //fos.write(buff,0,n);
                } //end while
            }catch(FileNotFoundException   f){ 
                System.out.println("文件未找到!"); 
                f.printStackTrace(); 
            }catch(IOException   ee){ 
                System.out.println("读错误。"); 
            }catch(NullPointerException e){
                System.err.println("没有输入参数");
            }catch(ArrayIndexOutOfBoundsException e){
                e.printStackTrace(); 
            }finally{ 
                try{ 
                    if(null!=fis)
                        fis.close(); 
                }catch(IOException   e){ 
                    System.out.println("文件错误"); 
                    System.exit(1); 
                } 
            } //end try...catch()
        } //end main()
    } //end class ReadFile
      

  10.   

    import java.io.*;public class FileInputStreamTest {
        public FileInputStreamTest() {
        }
        public static void main(String[] args){
            FileInputStream fls = null;
            File file = null;
            int n;
            try{
                file = new File("FileInputStreamTest.java");
                fls = new FileInputStream(file);
                while((n=fls.read())!=-1){
                    //char ch = (char)fls.read();
                    System.out.write (n);
                }
                fls.close();
            }catch(FileNotFoundException fnfe){
                System.out.println ("找不到文件!!");
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
        }
    }