java IO (一)

package cn.sasa.demo2;

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

import javax.management.RuntimeErrorException;

public class IODemo1 {
    public static void main(String[] args) throws IOException {
        /**
         * 字节流 、 字符流 
         * 程序到文件,输出 output 
         * 文件到程序,输入 input
         * 
         */
        //output();
        //outputAppend();
        //tryIOException();
        input();
    }

    /***
     * 文件的写入 java.io.OutputStream 所有字节输出流的父类
     * 
     * 作用: 从java程序 写出文件 写任意文件
     * 
     * write(int b) 写入一个字节 write(byte[] b) 写入字节数组 
     * write(byte[] b, int off, int len)
     * close() 关闭流对象,释放资源 flush()
     * 
     * FileOutputStream 写入数据文件
     * 
     * 步骤: 
     * 1、创建流子类的对象,绑定数据目的, 流对象的构造方法可以创建文件,如果文件存在,那么覆盖原有的文件 
     * 2、调用write
     * 3、close()释放资源
     * 
     */
    static void output() throws IOException {
        FileOutputStream output = new FileOutputStream("d:/sasa/test1221.txt");
        // 写入一个字节
        // output.write(97);

        // 写入字节数组
        byte[] writeByte = { 97, 98, 99, 100 };
        output.write(writeByte);

        // 写字节数组的一部分
        output.write(writeByte, 1, 2);

        // 写入字符串的字节数组
        output.write("lalala".getBytes());

        output.close();
        System.out.println("ok");
    }

    /***
     * 文件的续写和换行
     * @throws IOException 
     * 
     */
    static void outputAppend() throws IOException {
        File file = new File("d:/sasa/test1221.txt");
        //第二个参数表示在原来的基础上追加字节
        FileOutputStream output = new FileOutputStream(file, true);
        output.write("hello".getBytes());
        
        //换行 

        output.write("
".getBytes());
        output.write("blabla".getBytes());
        
        output.close();
        System.out.println("ok");
    }

    /***
     * IO流的异常处理
     * try  catch  finally
     */
    static void tryIOException() {
        FileOutputStream output = null;
        try {
           output = new FileOutputStream("d:/sasa/test1221.txt",true);
           output.write(100);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.getMessage());
            throw new RuntimeErrorException(null, "文件写入失败");
        }finally {
            try {
                //对象如果建立失败,没有占用资源,不用释放
                if(output != null)
                    output.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                throw new RuntimeErrorException(null, "关闭资源失败");
            }
        }
    }
    
    /***
     * 字节输入流
     * java.io.InputStream 所有字节输入流的父类
     * 作用:读取任意文件,每次读取1字节
     * 
     * int read()读取1字节
     * int read(byte[] b)
     * 
     * FileInputStream 读取文件
     * 步骤:
     * 1、创建字节输入流的子类对象
     * 2、调用read() 每执行一次,自动读取下一个字节,返回值返回读取到的字节,如果是结尾,则返回-1
     * 3、关闭资源
     * @throws IOException 
     */
    static void input() throws IOException {
        FileInputStream input = new FileInputStream("d:/sasa/test1221.txt");
        //读取一个字节
//        int readInt = input.read();
//        System.out.println(readInt);
//        int readInt1 = input.read();
//        System.out.println(readInt1);
//        int readInt2 = input.read();
//        System.out.println(readInt2);
//        int readInt3 = input.read();
//        System.out.println(readInt3);
        
//        int content = 0;
//        while((content = input.read()) != -1) {
//            //content = input.read();
//            System.out.print((char)content);
//        }
        
//        //int read(byte[] b) 读取字节数组 
//        byte[] inputByte = new byte[2];//2字节2字节读取
//        int len = input.read(inputByte);//返回的是读取到的有效字节,-1表示已读完
//        System.out.println(new String(inputByte));
//        System.out.println(len);
        
        int len = 0;
        byte[] inputByte = new byte[4];
        while((len = input.read(inputByte)) != -1) {
            System.out.print(new String(inputByte,0,len));
        }
        
        input.close();
    }
}
原文地址:https://www.cnblogs.com/SasaL/p/10168731.html