字节流+字符流

字节流和字符流的目的就是为了给文件中添加数据。

字节输出流OutputStream,是一个抽象类,是所有输出字节流类的超类,操作的都是字节。

其中子类FileOutputStream可用来写入数据到文件。

代码演示:写入数据到文件中

public static void main(String args[]) throws IOException {
        //明确目的地
        //只要文件没有就被创建,文件只要有就被覆盖
       FileOutputStream fos=new FileOutputStream("E:\io1127\demo11.txt",true);
       //换行方法
       fos.write("
".getBytes());
       //将字节写入文件    一个一个的写入
       fos.write(49);//1
       fos.write(48);//0
       fos.write(48);//0
       //将字节数组写入文件    使用数组形式
       byte[] bytes={65,66,67,68};
       fos.write(bytes);//A B C D
       fos.write(bytes,2,2);//C D
       //字符串转字节数组
       fos.write("你好".getBytes()); //你好
       //释放资源,流资源用完都要关闭
       fos.close();  
    }

文件中续写和换行一节异常处理

public static void main(String args[]) {
        //明确目的地
        //只要没有就创建,只要有就覆盖
       FileOutputStream fos=null;
    try {
        fos = new FileOutputStream("E:\io1127\demo11.txt",true);//默认是false,true为可以续写文件
        //换行
       fos.write("
".getBytes());
       //将字节写入文件
       fos.write(49);
       fos.write(48);
       fos.write(48);
       //将字节数组写入文件
       byte[] bytes={65,66,67,68};
       fos.write(bytes);
       fos.write(bytes,2,2);
       //字节转字符串
       fos.write("你好".getBytes()); 
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        //释放资源,流资源用完都要关闭
           try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }  
    }

异常解决简单方法try/catch就可以

字节输入流InputStream

其中子类FileInputStream可用来读取文件内容。

FileInputStream 从文件系统中的某个文件中获得输入字节。

读取字节

public static void main(String[] args) throws IOException {
        // 明确数据源
        FileInputStream fis=new FileInputStream("E:\io1127\demo11.txt");
        //一个字节一个字节读
        /*int len=fis.read();
        //强转    int与char可以相互转换
        System.out.println((char)len);*/
        int len=0;
        while((len=fis.read())!=-1){
            System.out.print((char)len);
        }
        System.out.println();
        //释放资源
        fis.close();

    }

读取字节数组

public static void main(String[] args) throws IOException {
        // 明确数据源
        FileInputStream fis=new FileInputStream("E:\io1127\demo11.txt");
        //创建字节数组
        byte[] bytes=new byte[2];
        //读取一个字节数组
        int len=0;
        while((len=fis.read(bytes))!=-1){
            //字节数组转字符串
            System.out.println(new String(bytes,0,len));
        }
        //释放资源
        fis.close();
    }

文件复制

public static void main(String[] args) throws IOException {
        // 文件复制
        //明确数据源
        FileInputStream fis=new FileInputStream("E:\io1127\demo11.txt");
        //明确目的地
        FileOutputStream fos=new FileOutputStream("E:\io1127\a\demo11.txt");
        //开始复制
        /*//一个字节一个字节复制
        int len=0;
        while((len=fis.read())!=-1){
            fos.write(len);
        }*/
        //开始复制
        //字节数组复制
        byte[] bytes=new byte[1024];
        int len=0;
        while((len=fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
        //释放资源
        fis.close();
        fos.close();
    }

文件复制要明确几点:

1.从哪来,输入

2.到哪去,输出

3.怎么做,方法

理顺思路

 

 

原文地址:https://www.cnblogs.com/xinzong/p/14451567.html