I/O之字节型文件流

  • 字节型文件输入流
package com.uncle.test_io.teststream;
/**
 *
 字节型文件输入流
 FileInputStream
 1.包   java.io
 2.了解一下继承关系    InputStream类  字节型输入流的父类
 3.创建对象
 调用一个带File类型的构造方法
 调用一个带String类型的构造方法
 4.常用方法
 int code = read();	每次从流管道中读取一个字节   返回字节的code码
 *int count = read(byte[] )  每次从流管道中读取若干个字节  存入数组内  返回有效元素个数
 int count = available();   返回流管道中还有多少缓存的字节数
 skip(long n)   跳过几个字节  读取
 多线程--->利用几个线程同时读取文件
 10000字节    5个小人同时读取
 1-2000   2001-4000   4001-6000   6001-8000   8001-10000
 D当做服务器    E当做客户端
 *close()	将流管道关闭---必须要做  最好放在finally里  注意代码的健壮性  判断严谨
 */

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

public class TestFileInputStream {
    
    

    public static void main(String[] args){
    
    
        FileInputStream fis = null;
        try {
    
    
            fis = new FileInputStream(new File("D://test//Test.txt"));
            fis.skip(5);
            int code = fis.read();
            System.out.println((char)code);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                if(fis!=null) {
    
    
                    fis.close();//关闭的是流通道  不是file对象   关闭这件事情必须要做
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }


//        try {//流---管道
//            FileInputStream fis = new FileInputStream("D://test//Test.txt");
//            int v = fis.available();//流管道中有多少缓存的字节  读取网络数据的数据可能会有问题
//            System.out.println(v);
//            //创建一个空的数组--->小推车
//            byte[] b = new byte[5];
//            int count = fis.read(b);//去文件里读东西 装入数组内   读取到的有效字节个数
//            while(count!=-1){
    
    
//                String value = new String(b,0,count);//第一次  a b c d e
//                System.out.print(value);     //第二次  f g \r\n h
//                count = fis.read(b);         //第三次  i j k l m
//            }                                //第四次  n \r \n o p
//        } catch (IOException e) {            //第五次  q \r \n o p  count--->1  此时的数组 5个元素
//            e.printStackTrace();
//        }


//        try {//理解为 文件是一个仓库  fis对象搬运工   推一个平板车
//            //创建一个字节型的文件输入流   读取一个文件中的内容
//            File file = new File("D://test//Test.txt");
//            FileInputStream fis = new FileInputStream(file);//真实去读文件
//            int code = fis.read();//读取一个字节   -1
//            while(code!=-1) {
    
    
//                System.out.print((char)code);//读取的字节对应的Unicode码   0---65535
//                code = fis.read();//读取一个字节   -1
//            }
//        } catch (FileNotFoundException e) {//编译时异常
//            e.printStackTrace();
//        } catch (IOException e) {
    
    
//            e.printStackTrace();
//        }
    }
}

  • 字节型文件输出流
package com.uncle.test_io.teststream;
/**
 * 字节型文件输出流
 * 		FileOutputStream	将数据写入文件中
 * 		1. java.io
 * 		2.继承OutputStream  所有字节型输出流的父类
 * 		3.创建对象
 * 			调用一个带File参数   还有File boolean重载
 * 			调用一个带String参数   还有String boolean重载
 * 		4.常用方法
 * 			write(int code);  将给定code对应的字符写入文件   '='
 * 			write(byte[])    将数组中的全部字节写入文件   getByte()
 * 			flush();	将管道内的字节推入(刷新)文件
 * 			close();	注意在finally中关闭
 */

import java.io.*;

public class TestFileOutputStream {
    
    

    public static void main(String[] args){
    
    
        FileOutputStream fos = null;
        try {
    
    
            fos = new FileOutputStream("D://test//bbb.txt",true);
            //创建一个数组  带着信息
            //byte[] b = new byte[]{97,98,99};
            String str = "1+1=2";//String-->byte[]
            byte[] b = str.getBytes();
            fos.write(b);
            fos.flush();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                if(fos!=null) {
    
    
                    fos.close();
                }
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }


//        try {
    
    
//            //创建一个字节型文件输出流
//            File file = new File("D://test//aaa.txt");
//            //创建的是文件输入流  若文件路径有问题  则抛出异常  FileNotFoundException
//            //创建的是文件输出流  若文件路径有问题  则直接帮我们创建一个新的文件
//            //将内容写入文件
//            FileOutputStream fos = new FileOutputStream(file,true);
//            fos.write(49);//a   a是写入在哪里了?  1+1=2
//            fos.write('+');
//            fos.write(49);
//            fos.write('=');
//            fos.write(50);
//            fos.flush();//刷新  将管道中的字节 推入文件中
//            System.out.println("写入完毕");
//        } catch (IOException e) {
    
    
//            e.printStackTrace();
//        }

    }

}

  • 文件流示意图
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_51945027/article/details/112970890