I/O流之文件流

2017-08-20  本文已影响19人  安安静静写代码

文件流可以分为字节流和字符流
字节流
字节流可以对任何文件进行操作 ,但效率不如字符流高
字节流分为字节输入流和字节输出流
输入输出是相对于电脑屏幕来说
输入流FileInputStream 的read(byte b[])函数是从指定文件中读出数据字符数组b[]中
用字节输入流向文件输入数据之前,此文件必须存在,否则会抛异常

package com.qf.demo3;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;

public class Test {

    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File("abc.txt"));     
            int  num = fis.available();// 查看剩余的   字节个数
            System.out.println(num);    
            // 效率高
            byte[] b = new  byte[10];   
            // 文件内容     
            // 偏移量+ 读取的个数  <= 数组的长度
            int num2 = fis.read(b, 5, 6);// p偏移量 偏移的是数组的  几个
            System.out.println(Arrays.toString(b));
            System.out.println(num2);   
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

字节输出流FileOutputStream 的write()函数

package com.qf.demo3;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test2 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {                       // 输出流 没有文件会自动帮助创建, 输入流必须要求读取文件存在
            fos = new FileOutputStream(new File("def.txt"));// 只要输出流与文件关联, 就会自动的帮助创建文件
            
            fos.write(97);      
            fos.write(98);      
            fos.write(new byte[]{97,98,99,100});
                                    // 偏移量是便宜的数组的       偏移量+ 长度 <= 数组长度
            fos.write(new byte[]{97,98,99,100}, 2, 2);          
            // 解决   执行了 write 文件中有可能不能里面将内容写进去
            fos.flush();// 刷新       
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test3 {

    public static void main(String[] args) {
//       1  创建流对象
        FileInputStream fis =  null;
        FileOutputStream fos =null;
         try {
             fis = new FileInputStream(new File("abc.txt"));
             fos = new FileOutputStream(new File("aaa.txt"),true);// boolean append   true 追加    false 覆盖
            
            // 2  读
            byte[] bs = new  byte[10];
            int num = 0;
            while((num = fis.read(bs))!=-1){
                // 3 写
                fos.write(bs,0,num);
                fos.flush();
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        
    }
}

字符输入流FileReader 以字节的形式将数据从文件中读出来

package com.qf.demo4;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class Test {

    public static void main(String[] args) {
        FileReader reader = null;
        try {
            reader = new FileReader(new File("二狗.txt"));
            
            int num = reader.read();
            System.out.println(num);
            
            char[] cbuf = new char[10];
            // 返回值 代表 读取的数据个数    读取出来的内容 放到char数组中
            int num2 = reader.read(cbuf);
            System.out.println(Arrays.toString(cbuf));
    
            int num3 = reader.read(cbuf, 3, 7);
            System.out.println(Arrays.toString(cbuf));
            System.out.println("有效个数"+num3);
            // 前面只读了18 
    
            // 还剩下28个
            reader.skip(28);// 跳过28个
            
            int num4 = reader.read();
            System.out.println(num4);
    
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }
}

字符输出流 FileWriter

package com.qf.demo4;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Test2 {

    public static void main(String[] args) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(new File("副班长"),true);
            
            writer.write("明天会更好");

            writer.write(new char[]{'后','天','呵','呵'});
            
            writer.write(new char[]{'大','大','大','大','后','天','放','假'},0 , 6);
            
            writer.write("开心就好", 1, 3);
        
            writer.append("说的太对了");
            writer.append('错');
            writer.append("呵呵呵呵呵额呵呵哈", 2, 6);// 左闭右开
            writer.flush();//  刷新
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读