Java基础---I/O流

文章目录

说明:
I/O流这一块内容看的是b站上尚硅谷宋红康老师的课程,b站视频链接其中P577~619是集合相关的讲解。


I/O流

一、File类的使用

1、概述

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

File能创建、删除、重命名文件和目录并获得文件大小、修改时间等,但File不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出。

想要在Java程序中表示一个真实存在的文件或目录,那么必须有一个File对象,但是Java程序中的一个File对象,可能没有一个真实存在的文件或目录

File对象常常作为参数传递给流的构造器,指明读取或写入的“终点”。

2、创建File类实例

(1)如何创建File类的实例

File(String filePath)

File(String parentPath, String childPath)

File(File parentPath, String childPath)

(2)相对路径:相较于某路径下,指明的路径

绝对路径:包含盘符在内的文件或文件目录的路径

(3)路径分隔符:可以使用File.seperator代替\或/,以便在任何系统下都可以识别
(4)测试代码

package org.test2;

import org.junit.Test;

import java.io.File;

/**
 * Created by luyangsiyi on 2020/2/8
 */
public class FileTest {
   
    /*
    1.如何创建File类的实例
    File(String filePath)
    File(String parentPath, String childPath)
    File(File parentPath, String childPath)

    2.相对路径:相较于某路径下,指明的路径
    绝对路径:包含盘符在内的文件或文件目录的路径
    3.路径分隔符:可以使用File.seperator代替\\或/,以便在任何系统下都可以识别
     */
    @Test
    public void test(){
   
        //构造器1
        File file1 = new File("hello.txt");//相对于当前module
        File file2 = new File("/Users/luyangsiyi/Desktop/SharonLu/hello.txt");
        System.out.println(file1);//hello.txt
        System.out.println(file2);///Users/luyangsiyi/Desktop/SharonLu/hello.txt

        //构造器2
        File file3 = new File("/Users/luyangsiyi/Desktop","SharonLu");
        System.out.println(file3);///Users/luyangsiyi/Desktop/SharonLu

        //构造器3
        File file4 = new File(file3,"hello.txt");
        System.out.println(file4);///Users/luyangsiyi/Desktop/SharonLu/hello.txt
    }
}

3、File类的常用方法

(1)File类的获取功能

@Test
public void test1(){
   
    //File类的获取功能
    File file1 = new File("hello.txt");
    File file2 = new File("/Users/luyangsiyi/Desktop/SharonLu");

    System.out.println(file1.getAbsoluteFile());
    System.out.println(file1.getPath());
    System.out.println(file1.getName());
    System.out.println(file1.getParent());
    System.out.println(file1.length());
    System.out.println(new Date(file1.lastModified()));
    //Users/luyangsiyi/Desktop/SharonLu/Work/WorkStation/IdeaWorkStation/test/hello.txt
    //hello.txt
    //hello.txt
    //null
    //11
    //Sat Feb 08 20:20:08 CST 2020

    System.out.println();

    System.out.println(file2.getAbsoluteFile());
    System.out.println(file2.getPath());
    System.out.println(file2.getName());
    System.out.println(file2.getParent());
    System.out.println(file2.length());
    System.out.println(new Date(file1.lastModified()));
    ///Users/luyangsiyi/Desktop/SharonLu
    ///Users/luyangsiyi/Desktop/SharonLu
    //SharonLu
    ///Users/luyangsiyi/Desktop
    //256
    //Sat Feb 08 20:20:08 CST 2020
}

如下两个方法适用于文件目录:


@Test
public void test2(){
   
    File file = new File("/Users/luyangsiyi/Desktop/SharonLu/Work/WorkStation/IdeaWorkStation/test");
    String[] list = file.list();//获得指定目录下的所有文件或者文件目录的名称数值
    for(String s:list){
   
        System.out.println(s);
    }
    //out
    //jdbc.properties
    //hello.txt
    //test.iml
    //.idea
    //src
    File[] files = file.listFiles();//获得指定目录下的所有文件或者文件目录的File数组
    for(File f:files){
   
        System.out.println(f);
    }
    ///Users/luyangsiyi/Desktop/SharonLu/Work/WorkStation/IdeaWorkStation/test/out
    ///Users/luyangsiyi/Desktop/SharonLu/Work/WorkStation/IdeaWorkStation/test/jdbc.properties
    ///Users/luyangsiyi/Desktop/SharonLu/Work/WorkStation/IdeaWorkStation/test/hello.txt
    ///Users/luyangsiyi/Desktop/SharonLu/Work/WorkStation/IdeaWorkStation/test/test.iml
    ///Users/luyangsiyi/Desktop/SharonLu/Work/WorkStation/IdeaWorkStation/test/.idea
    ///Users/luyangsiyi/Desktop/SharonLu/Work/WorkStation/IdeaWorkStation/test/src
}
(2)File的重命名功能

@Test
public void test3(){
   
    /*public boolean renameTo(File dest):把文件重命名为指定的文件路径
    * 比如:file1.renameTo(file2)为例:
    * 要想保证返回为true,需要file1在硬盘中存在,且file2在硬盘中不存在*/
    File file1 = new File("hello.txt");
    File file2 = new File("/Users/luyangsiyi/Desktop/SharonLu/hello1.txt");
    boolean renameTo = file1.renameTo(file2);//把文件重命名为指定的文件路径
    System.out.println(renameTo);
}
(3)File类的判断功能

@Test
public void test4(){
   
    File file = new File("hello.txt");
    System.out.println(file.isDirectory());
    System.out.println(file.isFile());
    System.out.println(file.exists());
    System.out.println(file.canRead());
    System.out.println(file.canWrite());
    System.out.println(file.isHidden());
    //false
    //true
    //true
    //true
    //true
    //false
}
(4)File类的创建和删除功能

@Test
public void test5() throws IOException {
   
    //文件的创建和删除
    File file = new File("hi.txt");
    if(!file.exists()){
   
        file.createNewFile();
        System.out.println("创建成功!");
    }else{
   
        file.delete();
        System.out.println("删除成功!");
    }
}

@Test
public void test6(){
   
    //文件目录的创建,上一层存在
    File file1 = new File("/Users/luyangsiyi/Desktop/SharonLu/Work/" +
            "WorkStation/IdeaWorkStation/test/hello");
    boolean mkdir = file1.mkdir();
    if(mkdir){
   
        System.out.println("创建成功");
    }

    //文件目录的创建,上一层不存在
    File file2 = new File("/Users/luyangsiyi/Desktop/SharonLu/Work/" +
            "WorkStation/IdeaWorkStation/test/test/hello");
    boolean mkdirs = file2.mkdirs();
    if(mkdir){
   
        System.out.println("创建成功");
    }
    
    /*mkdir()和mkdirs()的区别:如果创建的文件目录的上一层目录不存在,mkdirs()就一并创建了,而mkdir()不会*/
}

二、IO流原理及流的分类

1、原理

I/O是Input/Output的缩写,I/O技术用于处理设备之间的数据传输,如读写文件、网络通讯等。

Java程序中,对于数据的输入/输出操作以“流”的方式进行。

java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。

  • 输入:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。

  • 输出:将程序(内存)数据输出到磁盘、光盘等存储设备中。

2、流的分类

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

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

  • 按流的角色的不同分为:节点流、处理流

Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的。

由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。

抽象基类 字节流 字符流
输入流 InputStream Reader
输出流 OutputStream Writer

三、文件流(或节点流)

抽象基类 InputStream OutputStream Reader Writer
节点流(文件流) FileInputStream (read(byte[] buffer)) FileOutputStream (write(byte[] buffer,0,len) FileReader (read(char[] cbuf) FileWriter (write(char[] cbuf,0,len)

1、FileReader读入数据的基本操作

(1)read()

package org.test2;

import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 说明:
 * 1.read()返回读入的一个字符,如果达到文件末尾,返回-1
 * 2.异常的处理:为了保证流资源一定会执行close()操作,所以需要try-catch-finally处理
 * 3.读入的文件一定要存在,否则会报FileNotFoundException
 *
 * Created by luyangsiyi on 2020/2/8
 */
public class FileReaderWriterTest {
   
    @Test
    public void testFileReader() {
   
        //将hello.txt文件读入到程序中,并输出到控制台

        FileReader fr = null;
        try {
   
            //1.实例化File类的对象,指明要操作的文件
            File file = new File("hello.txt");//相较于当前module下
            //2.提供具体的流
            fr 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值