Java_初识IO流


活动地址:CSDN21天学习挑战赛

A_java_io流基础(一)

1、io流学习概要

  1. 文件

  2. IO流原理及其流的分类

  3. 节点流和处理流

  4. 输入流(重点)

    InputStream 字节输入流

    • File InputStream
    • Buffered InputStream
    • Object InputStream,

    Reader 字符输入流

    • File Reader
    • Buffered Reader
    • InputStream Reader
  5. 输出流(重点)

    OutputStream 字节输出流

    • File OutputStream
    • Buffere OutputStream
    • Object OutputStream

    Writer 字符输出流

    • File Writer
    • Buffered Writer
    • OutputStream Writer
  6. Properties类

2、文件

什么是文件?

文件就是保存数据的地方

文件流

文件在程序中是以流的形式来操作的

Java程序内存
输入流
文件磁盘
输出流

流:数据在数据源(文件)和程序(内存)之间所经历的路径

3、创建文件

一共可以通过三种方式创建文件

1、new File(FilePath)
public static void CreateTXT(){
    String FilePath="E:\\user_login.txt";// 文件路径以及需要创建的文件
    File file=new File(FilePath);
    try{
        file.createNewFile();
        将整体结构构建完毕之后 才可以通过createNewFile方法新建文件
    }
    catch(IOException e){
        // 打印错误信息
        e.printStackTrace();
    }
}
2、new File(File parent,String child)

该方法通过父级路径 以及 子文件构建也就是 文件名创建

 public static void create02(){
        File Parent_filepath=new File("D:\\List_Project\\IDEA_Project\\IO流文件\\");
        String FileName="user_login2.txt";
        File file =new File(Parent_filepath,FileName);
        try {
            file.createNewFile();
        }
        catch (IOException e){
            e.printStackTrace();
        }finally {
            System.out.println("操作执行完毕");
        }
    }
3、new File(String FilePath,String FileName)
public static void CreateTxT2(){
    String File_Name="E:\\";
    String File_Path="user_login2.txt";
    File file=new File(File_Path,File_Name);
    try{
        file.crateNewFile();
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

推荐使用第三种

4、获取文件的相关信息

常用方法

  • getName();获取文件名字
  • getAbsolutePath();获取文件绝对路径
  • getParent();获取文件的父级目录
  • length();获取文件的字节大小
  • exists();是否存在这个文件
  • isFile();是否为一个文件
  • isDirectory();是否为一个目录

代码如下

public static void infomation(){
        File file=new File("D:\\List_Project\\IDEA_Project\\IO流文件\\user_login.txt");
        // 输入相应方法获取对应信息
        // 获取文件名
        System.out.println(file.getName());
        // 获取文件绝对路径
        System.out.println(file.getAbsoluteFile());
        // 获取文件父级目录
        System.out.println(file.getParent());
        // 获取文件字节大小
        System.out.println(file.length());
        // 判断是否为文件
        System.out.println(file.isFile());
        // 判断是否为目录
        System.out.println(file.isDirectory());
        // 判断文件是否存在
        System.out.println(file.exists());
    }

5、目录的操作,与文件的删除

  • mkdir();创建一级目录
  • mkdirs();创建多级目录
  • delete();删除空目录或文件

在Java编程中目录也属于文件的一种

注意:如果需要删除目录 必须检查目录中没有存在文件

1、创建单级目录
String FilePath="D:\\a";
File file=new File(FilePath);
if(file.exists){
    sout(“当前目录已存在”)
}else{
    file.mkdir(); //创建单级目录
}
2、创建多级目录
String FilePath="D:\\a\\b\\c";
File file=new File(FilePath);
if(file.exists){
    sout(“当前目录已存在”)
}else{
    file.mkdirs(); //创建单级目录
}
3、删除目录或文件
String FilePath="D:\\a";
File file=new File(FilePath);
if(file.exists){
    file.delete(); // 删除当前文件夹
}else{
    sout(”当前文件不存在“)
}

6、IO流原理及流的分类

1、流的原理

1、I/O是input与output的缩写 该技术用于处理数据的传输,如读/写文件、网络通讯等

2、java程序中,对于数据的输入或者输出操作以(流Stream)的方式进行

3、java.io包下提供了各种"流"类和接口,用以获取不同种类的数据,并通过方法输出或者输入

4、输入流 input 将外部数据读取到内存中

5、输出流 output :将程序中的数据数据输出到外部文件文件中

外部文件A
输入流Input
程序
输出流OutPut
外部文件B
2、流的分类

按操作的数据单位不同分为:字节流(8 bit)、字符流(按字符)

按数据流的流分的不同分为:输出流、输入流

按照流的角色的不通过分为:节点流、处理流/包装流

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

使用字节流处理视频音频等文件

使用字符流处理文本文件

javaIO流 设计四十多个之类 其基础都源自这个抽象类

由这四个抽象类所衍生的子类都是以其父类名作为子类的后缀

Java之IO流
字节流
输出流
OutputStream
输入流
InputStream
字符流
输出流
Writer
输入流
Reader

7、InputStream 字节输入流

InputStream 实现子类分为三个

  • FileInputStream 文件输入流

  • FilterInputSteam

    • BufferedInputStream 缓冲字节输入流
  • ObjectInputSteam 对象字节输入流

1、FileInputStream

// 要求 读取Hello.txt 文件中的数据

public static void infoFile(){
        String FilePath="D:\\List_Project\\IDEA_Project\\IO流文件\\Hello.txt";
        File file=new File(FilePath);
        if (file.exists()){
            FileInputStream fileInputStream=null;
            try {
                // 用于读取文件
               fileInputStream =new FileInputStream(FilePath);
                try {
                   // read只读取一个字节
//                    int readContext;
                    // 如果文件中出现中文 使用字节方式去获取 则会报错
                    // 单个字节循环获取
//                    while ((readContext=fileInputStream.read())!=-1){
//                        System.out.print((char)readContext);
//                    }
                    // 字节数组形式获取
                    byte arr[]=new byte[4];
                    int length;
                    while ((length=fileInputStream.read(arr))!=-1){
                        for (int i = 0; i <length ; i++) {
                            System.out.print((char) arr[i]);
                        }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    fileInputStream.close(); // 释放资源
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

我们通过 FileInputStream 方法构造一个字节输入流的对象 其中参数为文件的绝对路径

读取时通过 Read()方法去实现 Read无参数传入 以一个字节一个字节全部读取

若传入一个 字符数组则可以快速读取数据 效率大大提升

FileInputStream.read()方法返回的值为当前所获取到的字节长度

如果已经获取完毕 从而无法获取则会返回-1

8、FileOutputStream 字节输出流

// 要求在Hello.txt 文件中写入数据

写入文件通过 FileOutputStream.write()方法进行实现

注意

1、该方法是以字节方式进行录入 可以通过 gerBytes()方法将字符串转换成字节数组

2、如果实例化FileOutputStream对象只传入一个参数 则当前对象的输出模式为覆盖

3、如果需要追加数据只需要 将FileOutputStream对象的append参数修改为true即可 默认为False

代码如下

public class FileOutputStream_ {
    public static void main(String[] args) {
        writerFile();
    }
    /*
    * 将数据写入文件
    * */
    public static void writerFile(){
        // 首先判断是否存在指定的文件 如果不存在则对其创建
        String FilePath="D:\\List_Project\\IDEA_Project\\IO流文件\\"; // 父级路径
        String FileName="Hello2.txt"; // 文件名
        File file=new File(FilePath,FileName);
        if (file.exists()){
            // 文件存在
            // 获取文件绝对路径
            String FileAbsPath=file.getAbsolutePath();
            // 创建 字节输出流对象 FileOutputStream
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream=new FileOutputStream(FileAbsPath,true);// 该方法实例化对象 数据录入模式为追加
//                fileOutputStream=new FileOutputStream(FileAbsPath);// 该方法实例化对象 数据录入模式为覆盖
                try {
                    String str="Hello,Word!";
                    // 该方法写入内容 会覆盖原来的内容
                    fileOutputStream.write(str.getBytes());// 将字符串 转换成字节数组
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    System.out.println("写入文件操作 执行完毕");
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }else {
            try {
                System.out.println("系统监测完毕,未发现指定文件;正在创建指定文件!");
                file.createNewFile();// 创建文件
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件创建失败");
            }
        }

    }
}

9、实践测试

// 要求 将C盘的图片音频视频等数据拷贝到 D盘
public class Test_1 {
    public static void main(String[] args) {

        infomation();
    }
    public static void infomation(){
        // 读取指定文件
        String FilePath="C:\\Users\\22100\\Pictures\\Saved Pictures"; // 文件路径
        String FileName="ceshi.jpg";// 文件名
        File file =new File(FilePath,FileName);
        // 录入指定文件
        String FilePath1="D:\\List_Project\\IDEA_Project\\IO流文件\\";
        String FileName2="ceshi.jpg";
        File file1=new File(FilePath1,FileName);
        
        // 查看是否存在 指定录入的文件
        if (!file1.exists()){
            try {
                file1.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 首先检测 是否存在需要拷贝的文件文件
        if (file.exists()){
            // 读取文件
            FileInputStream fileInputStream=null;
            FileOutputStream fileOutputStream=null;
            try {
                fileInputStream=new FileInputStream(file);
                fileOutputStream=new FileOutputStream(file1,true);
                try {
                    byte[]arr=new byte[100];
                    int lenght;
                    // 读取 指定的文件
                    while ((lenght=fileInputStream.read(arr))!=-1){
                        for (int i = 0; i <lenght; i++) {
                            // 将被读取的数据录入指定的文件之中
                            fileOutputStream.write(arr[i]);
                        }
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileInputStream.close(); // 释放资源
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }else {
            System.out.println("您所指定的文件不存在!");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值