文件的输入输出

OutputStream###

public static void main(String[] args) throws IOException{
        FileOutputStream fos = new FileOutputStream("D:\\1.txt");
            String s = "hello";
            fos.write(s.getBytes());
            
            fos.flush();
            fos.close();    
    }

InputStream###

public static void main(String[] args) throws IOException{
        FileInputStream fis = new FileInputStream("D:\\src\\1.txt");
        /*
        int ch;
        while((ch = fis.read()) != -1) {
            System.out.print((char)ch);
        }
        */
        //定义一个刚刚好的缓冲区,不用再循环了
        byte[] buf = new byte[fis.available()];
        fis.read(buf);
        System.out.println(new String(buf));
        fis.close();
    }

Reader###

public static void main(String[] args) throws IOException{
        //创建一个文件读取流对象,和指定名称的文件相关连
        //如果文件不存在,会发成异常FileNotFoundException
        FileReader fr = new FileReader("D:\\src\\1.txt");
        
        /*方法一
        //调用读取流对象的read方法
        //read() 一次读一个字符,而且会自动往下读
        
        int ch;
        while((ch = fr.read()) != -1) {
            System.out.print((char)ch);
        }
        */
        //方法二
        //定义字符数组,用于存储读到的字符
        //read(char[]) 返回的是读到字符个数
        char[] buf = new char[1024];
        int num;
        while((num = fr.read(buf)) != -1) {
            //把buf里面的字符打包成字符串输出
            System.out.print(new String(buf, 0, num));
        }
    }

Writer###

public static void main(String[] args) throws IOException {
		//创建一个FileWriter对象。该对象一被初始化就必须要明确被操作的文件
		//而且该文件就会被创建到指定的目录下。如果该目录下已有同名文件,将被覆盖
		//明确数据存放的目的地
		FileWriter fw = new FileWriter("D:\\1.txt");
		
		//调用Writer方法,将字符串写入到流中
		fw.Writer("Hello");

		//刷新流对象中的缓冲数据,将数据刷到目的地中
		fw.flush();
		
		//关闭资源
		//和flush的区别:刷新缓冲并关闭资源,close刷新后,流会关闭
		fw.close();
	}

对已有文件的续写###

public static void main(String[] args) throws IOException{
        FileWriter fw = new FileWriter("D:\\1.txt", true);
        
        fw.write("\r\nworld!");    // \r\n 换行续写
        fw.close();
    }

文件的拷贝###

public static void main(String[] args) throws IOException{
        //定义读取流和文件相关联
        FileReader fr = new FileReader("D:\\1.txt");
        //创建目的地
        FileWriter fw = new FileWriter("D:\\2.txt");
        char[] buf = new char[1024];
        int len;
        //通过不断读写来拷贝数据
        while((len = fr.read(buf)) != -1) {
            fw.write(buf, 0, len);
        }
        fw.close();
        fr.close();
    }

IO异常的处理###

public static void main(String[] args) {
        FileWriter fw = null;
        try {
            fw = new FileWriter("D:\\1.txt");
            fw.write("Hello");
        } catch (IOException e) {
            System.out.println("User: "+e.toString());
        } finally {
            try {
                if(fw != null) {
                    fw.close();
                } 
            } catch (IOException e) {
                    System.out.println("User: "+e.toString());
            }
        }
    }
posted @ 2016-04-27 15:23  Rancvl  阅读(133)  评论(0编辑  收藏  举报