关于Java文件输入输出流的问题

img

img


这两个我运行都出现了问题,Example6,7分别为他们的类名。

应该是因为文件的路径不对的原因,把Example_6.java 这个文件的真实路径填入即可,修改如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Example_6 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n = -1;
        byte[] a = new byte[1000]; //缓冲数组为1000,以便打印
        File f = new File("F:\\Example_6.java");
        //System.out.println(f.exists());
        
//        try {
//            f.createNewFile();
//        }catch(IOException e) {
//            System.out.println("失败啦!");
//        }
//        System.out.println(f.exists());
        
        try {
            InputStream in = new FileInputStream(f);
            
            while((n=in.read(a,0,1000))!=-1) {
                String s = new  String(a,0,n);
                System.out.println(s);
            }
            in.close();
        }catch(IOException e) {
            System.out.println("错误啦!");
        }
        
    }

}


img