java复习之IO流

File类的使用:
java.io.file:文件和文件目录路径的抽象表示形式,与平台无关

File 能新建,删除,重命名文件和目录,但file不能访问文件内容本身

1.File类申名在java.io包下


```java
package file;

import org.junit.Test;

import java.io.File;
import java.util.Date;
import java.util.Timer;

public class FileTest {
    /**
     * 向对路径:相对于某个路径下,指明的路径
     * 绝对路径:包含盘符在内的文件或者文件目录的路径
     */
    File file = new File("hello.txt");//相对于当前的module路径

    File file1 = new File("C:\\Users\\PC\\Desktop\\bite\\java40\\review");

    //构造器2:
    File file3 = new File("C:\\Users\\PC\\Desktop\\bite\\java40\\review", "dadadad");

    //构造器3:
    File file4 = new File("file3", "hi.txt");

    //具体方法:
    @Test
    public void test1() {
        File file1 = new File("hi.txt");
        File file2 = new File("d:\\io\\hello.txt");

        System.out.println(file1.getAbsolutePath());
        System.out.println(file1.getName());
        System.out.println(file1.getPath());
        System.out.println(file1.getParent());
        System.out.println(file1.length());
        System.out.println(new Date(file1.lastModified()));

        System.out.println(file2.getAbsolutePath());
        System.out.println(file2.getName());
        System.out.println(file2.getPath());
        System.out.println(file2.getParent());
        System.out.println(file2.length());
        System.out.println(file2.lastModified());


    }

    @Test
    public void test3() {
        File file1 = new File("D:\\workspace_idea1\\JavaSenior");
        String[] files = file1.list();
    }
    /**
    file1.renameTo(file2)
     需要file1在硬盘中存在且file2不存在
     */
    @Test
    public void test4(){
        File file1 = new File("hello.txt");
        File file2 = new File("d:\\io\\hi.txt");

        boolean b = file1.renameTo(file2);


    }
    @Test
    public void test5(){
        File file1 = new File("hello.txt");
        System.out.println(file1.isFile());
        System.out.println(file1.isDirectory());
        System.out.println(file1.exists());
        System.out.println(file1.canRead());
        System.out.println(file1.canWrite());
        System.out.println(file1.isHidden());

        /*
        创建硬盘中对应的文件或文件目录:
        public boolean createNewFile(),若存在则不创建,返回
        mkdir
        mkdirs7777777
         */
    }
}

流的分类

流向的不同
输入:

输出:

按照数据单位不同划分:字节流,字符流

按照角色的不同:节点流,处理流

```java
public class FileReaderWriterTest {
    @Test
    public void testFileReader() throws IOException {
        File file = new File("hello.txt");//相较于当前module
        //2.提供具体的流
        FileReader fr = new FileReader(file);
        //3.数据的读入过程:
        //返回读入的一个字符,如果达到文件末尾,返回-1
        int read = fr.read();
        while (read != -1) {
            System.out.println((char) read);
            read = fr.read();
        }
        //4.流的关闭操作
        fr.close();
    }
}
public void testFileReader1() throws IOException {
    //1.对read方法的升级:使用read的重载方法
    //1.file类的实例化
    File file = new File("hello.txt");
    //2.FileReader的实例化
    FileReader fr = new FileReader(file);
    //3.读入的操作细节
    char[] cbuf = new char[5];
    int read = fr.read(cbuf);
    while(read != -1){
        for(int i = 0;i < read;i++){
            System.out.println(cbuf[i]);
        }
        read = fr.read(cbuf);
    }
    //4.资源的关闭
    fr.close();
}
@Test
    public void testFileWriter() throws IOException {
        //1.提供File类的对象,指明写出到的文件

        File file = new File("hello1.txt");

        //2.提供FileWriter的对象,用于数据的写出

        FileWriter fw = new FileWriter(file);

        //3.写出的具体操作,如果存在,覆盖原文件
        String str = "I Have A Shit";
        fw.write(str);
        fw.write("you need to have a dream!");
        //4.流的关闭操作
        fw.close();
}

缓冲流

package io;

import org.junit.Test;

import java.io.*;

/**

  • 处理流之一:
  • 缓冲流的使用
  • BufferedInputStream
  • BufferedOutPutStream
  • Reader
  • Writer
  • 2.提高流的读取,写入速度,内部提供了一个缓冲区
  • 实现非文本文件的复制
    */
    public class BufferedTest {
    @Test
    public void Buffered() throws IOException {
    //造文件
    File src = new File(“QQ截2019022422329.png”);
    File desc = new File(“djakdja.png”);
    //造流对象,处理流作用在节点流上
    FileInputStream fis = new FileInputStream(src);
    FileOutputStream fos = new FileOutputStream(desc);
    //造处理流,缓冲流
    BufferedInputStream bis = new BufferedInputStream(fis);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    //3.具体的赋值细节
    byte[] buffer = new byte[10];
    int len;
    while((len = bis.read(buffer)) != -1){
    bos.write(buffer,0,len);
    bos.flush();//刷新缓冲区
    }
    bis.close();
    bos.close();
    fis.close();
    fos.close();
    }
    }
    ``
    (2)转换流
    在这里插入图片描述
package io;

import org.junit.Test;

import java.io.*;

/**
 * 转换流的使用:
 * 1.转换流:属于字符流
 * InputStreamReader 字节的输入流转换为字符的输入流
 * OutputStreamWriter 字符的输出流转换为一个字节的输出流
 *
 * 2.作用:提供字节流与字符流之间的转换
 *
 * 3.解码:字节,字节数组 ---- > 字符数组、字符串
 * 编码:字符数组,字符串 -----> 字节,字节数组
 *
 * 4.字符集
 *  编码表的由来,常见的编码表又那些
 * ASCII:美国标准交换码,用一个字节7位
 * ISO8859-1:拉丁码表,欧洲码表
 * GB2312:中国的中文编码表.最多两个字节编码所有字符
 * Unicode:国际标准码:融合了目前人类使用的所有字符,为每一个字符分配唯一的字符码
 * UTF-8:变长的编码方式,可用1-4个字节来表示一个字符
 */
public class ChangeStream {
    @Test
    public void test1() throws IOException {
        FileInputStream fis = new FileInputStream("dbcp.txt");
        InputStreamReader inputStreamReader =
                new InputStreamReader(fis,"Utf-8");
        char[] cbuf = new char[20];
        int len;
        while((len = inputStreamReader.read(cbuf)) != -1){
            String str = new String(cbuf,0,len);
            System.out.println(str);
        }
        inputStreamReader.close();
    }
}
/**
     * System.in
     * System.out
     *
     * 1.1
     * System.in:标准的输入流,默认从键盘输入
     * System.out:标准的输出流,默认从控制台输出
     *
     * 1.2
     * System类的setIn()/重新指定输入和输出流
     *
     */
    @Test
    public void test1() throws IOException {
        BufferedReader br = null;
        InputStreamReader isr = new InputStreamReader(System.in);
        br = new BufferedReader(isr);
        while(true){
            String data = br.readLine();
            if("e".equalsIgnoreCase(data) || "exits".equalsIgnoreCase(data)){
                System.out.println("程序结束");
                break;
            }
        }
    }

打印流

/**
 * PrintStream
 * PrintWriter
 */

在这里插入图片描述
数据流用于存储读取基本类型的数据或字符串

对象流,用于处理对象的流
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值