IO流基本使用

IO流

Input、Output

输入:将文件以数据流的形式读取到Java程序中

输出:通过Java程序将数据流写到文件中

File类

java.io.File类专门用来创建文件对象的,表示某个文件

方法描述
public File(String path)根据路径创建文件对象
public String getName()获取文件名
public String getParent()获取文件目录
public File getParentFile()获取文件所在目录的file对象
public String getPath()获取文件路径
public boolean exists()判断文件(夹)是否存在
public boolean isDirectory()判断对象是否为目录
public boolean isFile()判断对象是否为文件
public long length()获取文件大小
public boolean createNewFIle()根据对象创建文件
public boolean delete()删除文件
public boolean mkdir()创建文件目录
public boolean renameTo(File file)重命名
public class IoTest {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\Desktop\\新建文件夹\\1.txt");
        System.out.println(file);
        System.out.println(file.getName());
        System.out.println(file.getParent());
        System.out.println(file.getParentFile());
        System.out.println(file.getPath());
        System.out.println(file.exists());
        System.out.println(file.isDirectory());
        System.out.println(file.isFile());
        System.out.println(file.length());
        File file2 = new File("C:\\Users\\Desktop\\新建文件夹\\2.txt");
        try {
            //创建文件时,如果已存在,则创建失败,返回false
            System.out.println(file2.createNewFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //删除文件
        System.out.println(file2.delete());
        //创建目录
        System.out.println(file2.mkdir());
        //重命名文件
        System.out.println(file.renameTo(file2));
    }
}

Java中的流有很多种分类

  • 输入流、输出流,按方向划分
  • 字节流、字符流,按单位划分
  • 节点流、处理流,按功能划分

操作基本一样,不同流会有不同的方法

字节流

输入字节流:InputStream

输出字节流:OutputStream

InputStream

方法描述
int read()以字节为单位读取数据
int read(byte b[])将数据存入byte数组,返回数据长度
int read(byte b[],int off,int len)将数据存入byte数组的指定区间
byte readAllBytes()将数据转换成一个byte数组
int available()当前数据流中为读取的数据个数
void close()关闭数据流
public class InputStreamTest {
    public static void main(String[] args) {
        //获取文件对象
        File file = new File("C:\\Users\\Desktop\\新建文件夹\\2.txt");
        FileInputStream fileInputStream = null;
        try {
            //获取输入流
            fileInputStream = new FileInputStream(file);
            byte[] bytes = new byte[1024];
//            System.out.println(fileInputStream.read(bytes));
            System.out.println(fileInputStream.read(bytes,5,11));
            System.out.println(Arrays.toString(bytes));
            //输出剩余可读数量
            System.out.println(fileInputStream.available());
            int temp;
            //-1表示没有可读内容
            while ((temp = fileInputStream.read())!=-1){
                //输出读取内容
                System.out.println(temp);
                //输出剩余可读数量
                System.out.println(fileInputStream.available());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭输入流操作
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

OutputStream

方法描述
void write(int b)以字节流为单位写数据
void write(byte b[])将byte数组中的数写出
void write(byte b[],int off,int len)将byte数组中指定区间的数据写出
void flush()强制将缓冲区的数据同步到输出流中
void close()关闭数据流
public class OutputStreamTest {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\Desktop\\新建文件夹\\2.txt");
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            //写入单个数据
            fileOutputStream.write(104);
            //写入byte数组
            byte[] bytes = {104,104,32,104};
            fileOutputStream.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

字符流

字符流是有别于字节流的另一种数据流,两者区别在于每次传输的数据单位不同,一个是以字节为单位进行处理,一个是以字符为单位进行处理,相当于两根不同的管子抽水

输入字符流:Reader

输出字符流:Writer

Reader和Writer都是抽象类,使用的时候需要用它们的非抽象子类

Reader

常用方法

方法描述
int read()以字符为单位读取数据
int read(char chars[])将数据读入char数组,并返回数长度
int read(char chars[],int off,int len)将数据读入char数组的指定区间,并返回数据长度
void close关闭数据流
transferTo(Writer out)将数据直接读入字符输出流

英文字母、数字、标点符号、一个字符就是一个字节、使用字节流和字符流 结果没有区别,字符流是针对文本文件操作,操作其他类型会破坏文件结构

public class ReaderTest {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\Desktop\\新建文件夹\\1.txt");
        Reader reader = null;
        try {
            reader = new FileReader(file);
            //
            char[] chars = new char[1024];
            int read = reader.read(chars);
            System.out.println(read);
            //遍历
            for (char aChar : chars) {
                System.out.println(aChar);
            }
            //单个单个读
            int temp;
            while ((temp = reader.read())!=-1){
                System.out.println(temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Writer

方法描述
void write(int c)以字符为单位写数据
void write(char chars[])将char数组中的数据写出
void wirte(char chars[],int off,int len)将char数组中指定区间的数据写出
void write(String str)将String数据写出
void writer(String str,int off,int len)将String指定区间的数据写出
void flush()强制将缓冲区的数据同步到输出流中
void close关闭数据流

缓冲流

缓冲流属于处理流,不能直接访问文件

能直接访问文件的就是节点流(字节流、字符流)

不能直接访问文件的就是处理流,(缓冲流)必须基于节点流,构造处理流的时候必须将节点流作为参数传入到处理流中

字节缓冲流

字节输入缓冲流:BufferedInputStream

字节输出缓冲流:BufferedOutputStream

依赖字节流

字符缓冲流

字符输入缓冲流:BufferedReader

字符输出缓冲流:BufferedWriter

依赖字符流

文件复制

IO流实现,文本类型可以使用字符流,除了文本类型,其他类型,只能使用字节流处理,因为使用字符流会破环内部接口,导致无法使用

public class BufferedTest {
    public static void main(String[] args) {
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;
        OutputStream outputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            inputStream = new FileInputStream("E:\\xfm\\新建文件夹\\2.jpg");
            bufferedInputStream = new BufferedInputStream(inputStream);
            outputStream = new FileOutputStream("E:\\xfm\\新建文件夹\\a.jpg");
            bufferedOutputStream = new BufferedOutputStream(outputStream);
            int temp;
            while ((temp = bufferedInputStream.read())!=-1){
                bufferedOutputStream.write(temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedInputStream.close();
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
                inputStream.close();
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值