io流

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1. File类的使用

  1. 创建File实例

File file1 = new Flie(“hello.txt”)//相较于当前moudle 相对路径

File file1 = new Flie(“D:\workspace\hello.txt”)//绝对路径

File file = new File("hello.txt");
File file2 = new File(pathname  child  )  构造器2 指明上层目录
File file3 =new Flie(file2  "hi.txt") 
public void Test1(){
    File file = new File("hello.txt");
    File file1 = new File("E:\\qqFile\\1769935880\\FileRecv\\软件L171-17L0903114-王鹏翔.docx");
    System.out.println(file);
    System.out.println(file1);
    File file2 = new File("1769935880","FileRecv");
    System.out.println(file2);
    File file3 = new File(file2,"123");
    System.out.println(file3);
    
    hello.txt
E:\qqFile\1769935880\FileRecv\软件L171-17L0903114-王鹏翔.docx
1769935880\FileRecv
1769935880\FileRecv\123

public string getAbsolutePath():获取绝对路径

public string getPath() :获取路径
public string getName() :获取名称
public string getParent():获取上层文件目录路径。若无,返回null
public long length():获取文件长度(即:字节数)。不能获取目录的长度。public long LastNodified():获取最后一次的修改时间,毫秒值
public string[]list():获取指定目录下的所有文件或者文件目录的名称数组

public File[] listFiLes():获取指定目录下的所有文件或者文件目录的FiLe数组

String[] list1 = file1.list();

for (String s: list1) {
     System.out.println(s);
}
File[] files1 = file1.listFiles();
for (File s:
     files1) {
    System.out.println(s);
}

QC7 笔记本控制中心软件ControlCenterI_2.2.0.18
QC7 笔记本控制中心软件ControlCenterI_2.2.0.18.zip
W10数字许可C#版v3.6.0(1).zip
win10 激活教程
任务书示例.doc
其他
图片1.png
图片2.png

-------------------------------------------
E:\qqFile\1769935880\FileRecv\ControlCenterI_2.2.0.18.zip
E:\qqFile\1769935880\FileRecv\GPUTweakII-Version2295
E:\qqFile\1769935880\FileRecv\GPUTweakII-Version2295.zip
E:\qqFile\1769935880\FileRecv\MobileFile
E:\qqFile\1769935880\FileRecv\QC7 笔记本控制中心软件ControlCenterI_2.2.0.18
E:\qqFile\1769935880\FileRecv\QC7 笔记本控制中心软件ControlCenterI_2.2.0.18.zip
E:\qqFile\1769935880\FileRecv\W10数字许可C#版v3.6.0(1).zip
E:\qqFile\1769935880\FileRecv\win10 激活教程
E:\qqFile\1769935880\FileRecv\任务书示例.doc
E:\qqFile\1769935880\FileRecv\其他
E:\qqFile\1769935880\FileRecv\图片1.png
E:\qqFile\1769935880\FileRecv\图片2.png


Process finished with exit code 0

public booLean isDirectory():判断是否是文件目录

pgblic booLean isFile():判断是否是文件

public boolean exists():判断是否存在

public boolean canRead() :判断是否可读

public boolean canwrite() :判断是否可写

public booLean isHidden() :判断是否隐藏

File类创建文件的功能

创建硬盘中对应的文件或文件目录
public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false
public boolean mkdir():创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层不存在,就不创建了

public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建
删除磁盘中的文件或文件目录
public booLean delete():删除文件或者文件夹,删除的前提是文件夹里面没有内容,需要通过遍历进行删除操作
删除注意事项:
Java中的删除不走回收站。

io流涉及到读取文件内容的部分

后续File类的对象会作为参数传递到流的构造器中

非文本的数据用字节流

抽象基类字节流字符流
输入流InputStreamReader
输出流OutputStreamWriter

注意需要保证流资源一定执行close操作,需要tyr-catch-finally 包围住

Reader返回一个字符,需要用foreach遍历才能输出所有字符,当返回值为-1时输入结束

需要注意的时,FileReader用Try Catch Finally包裹,当文件不存在时,会报出文件不存在的异常

File写在main方法中时,相对路径时这个工程的相对,与@Test不同,Test相对路径是在这个Moudul下,可以用 Code\\wpx.txt 这个指定上一级来精确定位

读入文件 FileReader

File file = new File("E:\\多线程\\新建文本文档.txt");//File类实例化
FileReader fr = new FileReader(file);//流实例化
int data = fr.read();//读入操作
try {
    while (data != -1){
        System.out.print((char) data);
        data = fr.read();

    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    fr.close();
}

缓冲

添加一个步骤

    File file = new File("E:\\多线程\\新建文本文档.txt");
        FileReader fr = new FileReader(file);
      
        char[] cbuf = new char[5];
        int len;
        while ((len = fr.read(cbuf)) != -1){
//            方法一
            for (int i = 0; i < len; i++) {
                System.out.print(cbuf[i]);
            }
//            方法二
            String s = new String(cbuf,0,len);
            System.out.print(s);

        }

数据写出FileWriter

具体操作类似

public void test6() throws IOException {
    File file = new File("hellowpx.txt");
    FileWriter writer = new FileWriter(file);
    writer.write("我爱王鹏翔!!!\n");
    writer.write("我更爱王鹏翔!!!");
        writer.close();

复制文本

public void test7() {
    FileReader fr = null;
    FileWriter fw = null;
    try {
        File file = new File("hellowpx.txt");
        File file1 = new File("hello1.txt");
        fr = new FileReader(file);
        fw = new FileWriter(file1);
        int len;
        char[] chars = new char[5];
        while ((len=fr.read(chars))!= -1){

                fw.write(chars,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fr.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

复制图片

public  void test8() {
    FileInputStream fi = null;
    FileOutputStream fo = null;
    try {
        File file = new File("b.jpeg");
        File file1 = new File("c.jpg");
        fi = new FileInputStream(file);
        fo = new FileOutputStream(file1);
        byte[] bytes = new byte[1024];
        int len;
        while ((len=fi.read(bytes)) != -1){
            fo.write(bytes,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fi.close();
            fo.close();//以下个为准
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

缓冲流的使用

缓冲流是处理流之一,读写速度更快

缓冲流需要在节点流后面再造两个处理流,关闭先关闭外层,再关闭内层,一般关闭外层流同时,内层流会自动关闭,可以省略.

public  void test8()  {
    FileInputStream fi = null;
    FileOutputStream fo = null;
    BufferedInputStream bis= null;
    BufferedOutputStream bos= null;
    try {
        File file = new File("b.jpeg");
        File file1 = new File("c.jpg");
        fi = new FileInputStream(file);
        fo = new FileOutputStream(file1);
         bis = new BufferedInputStream(fi);
         bos = new BufferedOutputStream(fo);
        byte[] bytes = new byte[1024];
        int len;
        while ((len=bis.read(bytes)) != -1){
            bos.write(bytes,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bis != null){
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bos !=null){
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

步骤一样

File file = new File("b.jpeg");
File file1 = new File("c.jpg");
fi = new FileInputStream(file);
fo = new FileOutputStream(file1);
 bis = new BufferedInputStream(fi);
 bos = new BufferedOutputStream(fo);
byte[] bytes = new byte[1024];
int len;
while ((len=bis.read(bytes)) != -1){
    bos.write(bytes,0,len);
}//以上先try catch finally
  bis.close();//然后对这两个分别在不等于null的时候trycatch
  bos.close();

缩减写法

br = new BufferedReader(new FileReader(new File(“hello1.txt”)));
bo = new BufferedWriter(new FileWriter(new File(“wpx.txt”)));

public void test9()  {
    BufferedReader br = null;
    BufferedWriter bo = null;
    try {
        br = new BufferedReader(new FileReader(new File("hello1.txt")));
        bo = new BufferedWriter(new FileWriter(new File("wpx.txt")));
        char[] chars = new char[5];
        int len;
        while ((len =br.read(chars)) != -1){
            bo.write(chars,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bo!= null){

            try {
                bo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }if (br!=null){
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

方法二使用String

readline可以一次读一行

String data;
while ((data = br.readLine())!= null){
    bo.write(data+"\n");
}

转换流的使用

将输入的字节流转换为字符流 或者之间互相转换

InputStreamReader; 字节--》字符
OutputStreamWriter; 字符————》字节

输入流输出流,打印流

因为 System.in 返回的是字节

所以要用转换流把字符转成字符,然后用缓冲流的Readline方法,读入一行字符串。

BufferedReader br = null;
try {
    InputStreamReader isr = new InputStreamReader(System.in);
    br = new BufferedReader(isr);

    while (true){
        System.out.println("输入字符串");
        String data = br.readLine();
        if (("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data))){
            System.out.println("程序结束!");
            break;
        }else {
            String s = data.toUpperCase(Locale.ROOT);
            System.out.println(s);
        }
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (br != null){
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

打印流

printStream 字节输出

printWriter 字符输出

System.Setout 输出到文件 不会自动创建文件

数据流

DataInputStream

DataOutputStream

用于读写基本数据类型的数据

对象流

ObjectInputStream 读取对象数据 需要反序列化

ObjectOutputStream 保存对象数据 需要序列化

对象序列化机制,自定义可序列化

这个对象类必须是可序列化的,把内存中的Java对象转换成二进制流,这种二进制流能持久地保存到硬盘上或者通过网络节点发送,当其他程序获得这个对象,可以恢复成原来的Java对象。

会自动创建不存在的文件

@Test//序列化
public  void test1() throws IOException {
    ObjectOutputStream ois = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream("wpx.txt"));
        oos.writeObject(new String("woaiwpx"));
        oos.flush();
        //oos.writeObject(new person("wpx",27));
        //1要实现自定义对象序列化,必须在自定义对象类里实现java.io.Serializable 接口 ,这个接口是标识接口,识别到则为可序列化的类
        //2还需要提供序列版本号 public static final long serialVersionUID = xxxxL;随便填一个
        //如果不写这个UID 则会使用这个类默认的UID,如果对这个类进行了修改 UID会改变,写上面的常量是为了提供稳定的读写。
        //3保证里面所有的属性的类也是可序列化的,重读12步骤即可
    } finally {
        oos.close();
    }

}
@Test/反序列化
public  void  test2() throws IOException, ClassNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("wpx.txt"));
    Object obj = ois.readObject();
    String str = (String)obj;
    //Person p = (Person) ois.readObject();
    // System.out.println(p);
    System.out.println(str);
    ois.close();
}

随机存取文件流

RandomAccessFile类

创建时除了指定文件以外,还要指定一个mode参数

r 只读,rw 读写, rwd 读写 内容更新 , rws 读写,内容元素更新

如果写出时文件存在,会从头覆盖

从源文件插入

使用.seek方法调用指针

NIO.2

框架部分对io的封装

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值