java中的IO

本文详细介绍了Java IO体系结构,包括File类的使用、输入输出概念、流的分类(字节流与字符流)、节点流与处理流的区别,以及Print流、对象输入输出流的应用。涵盖了File对象操作、输入输出操作方法和典型示例,是理解Java IO核心概念的实用教程。
● File
● 输入及输出的概念
● 输入流与输出流
● 字节流与字符流
● 输入输出节点字节流
● 节点流与处理流
● 输入输出节点字符流
Print
● 对象输入输出流
整个javaIO体系结构图:

一.File
● File类是java.io包中很重要的一个类;
File类的对象可以表示文件,还可以表示目录,在程序中一个File类对象可以代
表一个文件或目录;
File对象可以对文件或目录的属性进行操作,如:文件名、最后修改日期、文件
大小等;
File对象无法操作文件的具体数据,即不能直接对文件进行读/写操作。
File类的构造方法:
File d = new File("D:/Demo.txt");//绝对路径,完整的一个路径 File d1 = new File("demo.txt");//相对路径,不是全路径,是俩个文件相对同一个父级的路径
● File类的常用方法
 File d1 = new File("demo.txt");
        
        File d4 = new File("D:/Demo4.txt");
      d1.getAbsoluteFile()   );//获取绝对路径
        d4.getParent());//获取父级路径
        d4.length());//文件内容长度(以字节为单位),在UTF-8中,一个汉字是三个字节
       d4.canRead());//可以读,返回true
        d4.canWrite());//可以写,返回true
        d4.exists());//文件是否存在
       d4.isFile());//是否是文件

        if(!d4.exists()){
            try {
                d4.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       d4.isDirectory());//是否是目录
        d4.delete());//文件是否删除,删除时目录必须为空
        File f  =new File("D:/demo4");
                 f.mkdir();//创建单级目录
        System.out.println( f.lastModified());
        File g  =new File("D:/demo5/demo6");
            g.mkdirs();//创建多级目录
        g.delete();//删除时,删除小的文件,但是如果文件夹里有文件则删除不了
    }
二.输入及输出的概念
输入输出(I/O
把电脑硬盘上的数据读到程序中,称为输入,input,进行数据的read 操作从程序往外部设备写数据,称为输出,即output,进行数据的write操作
三.输入与输出流
流按着数据的传输方向分为:
输入流:往程序中读叫输入流。
输出流:从程序中往外写叫输出流。
● InputStream和OutputStream的子类都是字节流 可以读写二进制文件,主要处理音频、图片、歌曲、字节流,处理单元 为1个字节。
● Reader和Writer的子类都是字符流 主要处理字符或字符串,字符流处理单元为1个字符。 字节流将读取到的字节数据,去指定的编码表中获取对应文字

四.字节流与字符流

从数据流编码格式上划分为
字节流
字符流
字节流中常用类
字节输入流 FileInputStream
字节输出流 FileOutputStream
● 字符流中常用类
字符输入流 FileReader
字符输出流 FileWriter
五.输入输出字节流
InputStream的基本方法
读取一个字节并以整数的形式返回(0~255),如果返回-1已到输入流的末尾。
int read() throws IOException
读取一系列字节并存储到一个数组buffer,
返回实际读取的字节数,如果读取前已到输入流的
末尾返回-1
int read(byte[] buffer) throws IOException
关闭流释放内存资源
void close() throws IOException
● OutputStream的基本方法
向输出流中写入一个字节数据,该字节数据为参数b的低8位
void write(int b) throws IOException
将一个字节类型的数组中的从指定位置(off)开始的 len个字节写入到输出流
void write(byte[] b, int off, int len) throws IOException
关闭流释放内存资源
void close() throws IOException
public class streamDemo1 {
    public static void main(String[] args) throws IOException {
        FileInputStream a = new FileInputStream("D:/demo0.txt");
        FileOutputStream b = new FileOutputStream("D:/demo8.txt");
        /*int a1 = a.read();
        int a2 = a.read();
        int a3 = a.read();
        int a4 = a.read();
        int a5 = a.read();
        System.out.println(a1);//读取对应的编码
        System.out.println(a2);
        System.out.println(a3);
        System.out.println(a4);
        System.out.println(a5);*/
      int g = 0;
       while ((g=a.read())!=-1){
           System.out.println(g);
           b.write(g);
       }
    }
}

read()的读取,一次只能读取一个字节

public class steamDemo2 {
    public static void main(String[] args) throws IOException {
        FileInputStream a=null;
        FileOutputStream b1 = null;
        try {
            a = new FileInputStream("D:/demo7.txt");
            b1 = new FileOutputStream("D:/demo9.txt");
            int v = 0;
            while ((v=a.read())!=-1){//read只能每次读取一个字节,返回字节编码(int类型),读完后返回-1
                System.out.println(v);
                b1.write(v);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println( e.getMessage());
        }finally {
            if(a!=null){
                a.close();
            }
            if(b1!=null){
                b1.close();
            }
        }
    }
}

read()读取一个数组的字节

public class streamDemo3 {
    public static void main(String[] args) throws IOException {
        FileInputStream a=null;
        FileOutputStream b1 = null;
        try {
             a = new FileInputStream("D:/demo7.txt");
             b1 = new FileOutputStream("D:/demo9.txt");
             byte[]g = new byte[5];
             int v = 0;
             while ((v=a.read(g))!=-1){//a.read()只能每次读取一个字节,返回字节编码(int类型),读完后返回-1
                 System.out.println(v);//a.read(byte[] g)一次可以读一个数组的字节,返回的是数组实际装入的内容,读完后返回-1
                 b1.write(g,0,v);//b1.write(v)一次向目标写出一个字节,//b1.write(g,0,v)一次向目标文件写出一个数组,从指定位置开始,写出size个字节
             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println( e.getMessage());
        }

    }
}

六.输入输出字符流

●Reader 的基本方法
读取一个字符并以整数的形式返回, 如果返回-1已到输入流的末尾。
int read() throws IOException
读取一系列字符并存储到一个数组buffer,
返回实际读取的字符数,如果读取前
已到输入流的末尾返回-1
int read( char[] cbuf) throws IOException
关闭void close() throws IOException
• Writer 的基本方法
向输出流中写入一个字符数据,该字节数据为参数b的16位
void write(int c) throws IOException
一个字符类型的数组中的数据写入输出流
void write( char[] cbuf) throws IOException
将一个字符类型的数组中的从指定位置(off set)开始的 length个字符写入到
输出流
void write( char[] cbuf, int off set, int length) throws IOException
关闭void close() throws IOException
public class readDemo1 {
    //一个汉字在Utf-8中是三个字节,底层存储单位是字节,字符流是如何做到一次读一个字符的汉字
    //Reader  字符输入流的基类
    //FileReader  exdent  InputsteamReader(转换流,将读到的字节按照指定的charset转换为字节)
    //Writer  字符输出流的基类
    //FileWrite     字符流只能读取纯文本文件




    public static void main(String[] args) throws IOException {
        FileReader fileReader  =new FileReader("D:/demo10.txt");
        FileWriter fileWriter = new FileWriter("D:/demo11.txt");
        int c = 0;
        char[] chars = new char[5];
        while ((c=fileReader.read(chars))!=-1){
            System.out.println(chars);
            fileWriter.write(chars,0,c);
        }
        fileReader.close();
        fileWriter.close();
    }
}

七.节点流与处理流
根据封装类型不同流又分为
节点流 处理流
● 节点流:
如果流封装的是某种特定的数据源,如文件、字符串、字符串数组等,则称为节点流。
● 处理流:
如果流封装的是其它流对象,称为处理流。 处理流提供了缓冲功能,提高读写效率,同时增加了一些新的方法
●节点流中常用类
字节输入流 FileInputStream
字节输出流 FileOutputStream
字符输入流 FileReader
字符输出流 FileWriter
● 处理流中常用类
缓冲字节输出流 BufferedOutputStream
缓冲字节输入流 BufferedInputStream
缓冲字符输入流 BufferedReader
缓冲字符输出流 BufferedWriter

以字符流为例:

 public static void main(String[] args) throws IOException {
       FileReader fileReader = new FileReader("D:/demo10.txt");
        FileWriter fileWriter = new FileWriter("D:/demo12.txt",true);//写入数据时,保留之前的,向后面增加内容
       BufferedReader b = new BufferedReader(fileReader);
       BufferedWriter b1 = new BufferedWriter(fileWriter);
       String line =null;
       while ((line=b.readLine())!=null){//一次读取一行
           System.out.println(line);
           b1.write(line);
           b1.newLine();//换行
       }
       b1.flush();
       b.close();
       b1.close();
    }
}

注意:这里的底层数组默认长度是8192;假如每次写进去的数据
是自己定义的一个数组的长度(1024),你需要传俩次才可以把底层数组写满,但是底层源码规定了
必须得传第三次才可以写进文件里,读写read时,如果自定义数组不大于底层数组,会把读到的数据存到底层数组中

八.Print流

打印流: 单向的从程序中向目标输出数据

PrintWriter:
字符打印流 print方法可以打印各种类型数据
在javaweb项目中,服务器端向客户端响应数据以打印流的方式响应.
例如:
public static void main(String[] args) throws FileNotFoundException {
            //  打印流:  单向的从程序中向目标输出数据
            //printWrite  打印字符流
            PrintWriter printWriter = new PrintWriter("D:/demo.html");

            printWriter.print("<h>你好客户端</h>");
            printWriter.print("<h>你好客户端</h>");
            printWriter.print("<h>你好客户端</h>");

            printWriter.close();
        }

九.对象输入输出流

前提:

对象的寿命通常随着生成该对象的程序的终止而终止。 有时候,可能需要将对象的状态保存下来,在需要时再将对象恢复。
对象的输入输出流 :
主要的作用是用于写入对象信息与读取对象信息。 对象信息 一旦写到文件上那么对象的信息就可以做到持久化了.
对象的输出流:ObjectOutputStream
对象的输入流:ObjectInputStream
● 在ObjectInputStream 中用readObject()方法可以直接读取一个对象,ObjectOutputStream中用writeObject()方法可以直接将对象保存到输出流中

序列化:

对象的输出流将指定的对象写入到文件的过程,就是将对象序列化的过程

反序列化:

 FileOutputStream f  =new FileOutputStream("D:/ssss");
        ObjectOutputStream b = new ObjectOutputStream(f);
        Date date = new Date();
        String s = "abc";
        b.writeObject(date);
        b.writeObject(s);

        f.close();
        b.close();
对象 的输入流将指定序列化好的文件读出来的过程,就是对象反序列化的过程。

FileInputStream f1 = new FileInputStream("D:/ssss");
        ObjectInputStream b1 = new ObjectInputStream(f1);
        Date date1 = (Date) b1.readObject();
        String n = (String) b1.readObject();

        System.out.println(date1);
        System.out.println(n);
对象的输出流将对象写入到文件中称之为对象的序列化,所以被序列化
对象的类必须要实现 Serializable接口。 Serializable接口中没有任何方法。当一个类声明实现Serializable接口后,表明该类可被序列化。
在类中可以生成一个编号
private static final long serialVersionUID = -5974713180104013488L;
随机生成 唯一的 serialVersionUID 用来表明实现序列化类的不同版本间的兼容性。某个类在与之对应的对象已经序列化出去后做了修改,该对象依然可以被正确反序列化.
如果不显示生成序列号,那么将会隐式生产,但是隐式生成后,类一旦发生改变,序列号也会随之改变.
十.transient关键字
transient关键字,被它修饰的数据存入文件后,读取不出来
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值