美文网首页
I/O流之文件流

I/O流之文件流

作者: 安安静静写代码 | 来源:发表于2017-08-20 18:05 被阅读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();
                }
            }
        }
    }
}

  • 复制文件
  • 1 创建流对象
  • 2 读
  • 3 写
  • 4 关流
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();
                }
            }
        }
    }
}

相关文章

  • I/O流之文件流

    文件流可以分为字节流和字符流字节流字节流可以对任何文件进行操作 ,但效率不如字符流高字节流分为字节输入流和字节输...

  • java基础-I/O流

    知识点 I/O流的基本知识 I/O流中常用流的关系 I/O流之FileInputSrteam,FileOutput...

  • 流?I/O操作?阻塞?epoll?

    一、流?I/O操作? 阻塞? (1) 流 可以进行I/O操作的内核对象 文件、管道、套接字…… 流的入口:文件描述...

  • 文件访问

    系统调用I/O与基于流的I/O 区别: 基于流的文件操作函数的名字都是以字母f开头,而系统调用函数则不同。例如流函...

  • Java I/O相关

    I/O类的结构体系 I/O体系包含字节流、字符流、文件流和一个接口Serializable。字节流包含输入流Inp...

  • I/O的学习之字符流

    I/O的学习之字符流 今天的学习内容 字符流FileReader 字符流FileWriter 字符流的拷贝 带缓冲...

  • Java-I/O流

    总结 I/O流分类 按照操作单元划分,字节I/O系统和字符I/O系统。 按照流的流向分,可以将流分为输入流和输出流...

  • 19-io_文件权限掩码_动态库_静态库

    I/O * 标准IO:库 带缓存 通过流FILE * 操作文件 * 文件IO...

  • I/O流

    java常见的I/O流

  • I/O流

    输入输出流 Java.io包几乎包含了所有操作输入、输出需要的类。所有这些流类代表了输入源和输出目标。Java.i...

网友评论

      本文标题:I/O流之文件流

      本文链接:https://www.haomeiwen.com/subject/lpifdxtx.html