Java文件操作和IO

操作系统的一个重要功能就是对文件的管理,每个操作系统都有自己的一套系统API调用,Java做为一个跨平台的语文,JVM针对不同的操作系统做了一层封装,我们只需要使用JDK提供的关于文件操作的API就可以完成不同系统上的文件操作。

File类

属性

签名属性说明
static StringpathSeparator依赖于系统的路径分隔符,String类型的表示
static charstatic char依赖于系统的路径分隔符,char类型的表示

 构造方法

注意斜杠与反斜杠的区别反斜杠需要转义

方法

 方法的使用

File对象的创建

public static void main(String[] args) {
        // 通过指定文件路径来创建一个File对象
        File file = new File("D:\\temp\\test\\hello.txt");
        System.out.println(file);
        File file1 = new File("D:/temp/test/hello.txt");
        System.out.println(file1);

        // 这只是JAVA层动的一个对象,并不一定必须在系统中真实存在
        File file2 = new File("D:/temp/test/test.txt");
        System.out.println(file2);
    }

方法的使用

    public static void main(String[] args) throws IOException {
        // 指定相对路径并创建一个File对象
        File file = new File("./test.txt");
        // 获取父目录
        System.out.println(file.getParent());
        // 获取文件名
        System.out.println(file.getName());
        // 获取路径
        System.out.println(file.getPath());
        // 获取绝对路径
        System.out.println(file.getAbsolutePath());
        // 获取一个标准路径
        System.out.println(file.getCanonicalPath());
        // 是否存在
        System.out.println(file.exists());
        // 是不是一个目录
        System.out.println(file.isDirectory());
        // 是不是一个文件
        System.out.println(file.isFile());

    }

文件内容的读写 —— 数据流

InputStream 概述

修饰符及
返回值类
方法签名
说明
int
read()
读取一个字节的数据,返回 -1 代表已经完全读完了
int
read(byte[] b)
最多读取 b.length 字节的数据到 b 中,返回实际读到的数 量;-1 代表以及读完了
int
read(byte[] b,
int off, int len)
最多读取 len - off 字节的数据到 b 中,放在从 off 开始,返 回实际读到的数量;-1 代表以及读完了
void
close()
关闭字节流

FileInputStream 概述

方法使用 

public static void main(String[] args) throws IOException {
        // 创建一个文件对象
        File file = new File("d:/temp/java78/hello.txt");
        // 创建一个输入流
        InputStream inputStream = new FileInputStream(file);
        // 读取文件内容
        while (true) {
            int read = inputStream.read();
            // 是否读完
            if (read == -1) {
                break;
            }
            System.out.println(read);
        }
        // 关闭
        inputStream.close();
    }

利用 Scanner 进行字符读取 

构造方法说明

 Scanner(InputStream is, String charset)

使用 charset 字符集进行 is 的扫描读取
 public static void main(String[] args) throws IOException {
        // 创建一个输入流
        FileInputStream inputStream = new FileInputStream("d:/temp/java78/hello.txt");
        // 根据创建输入流创建Scanner对象
        Scanner scanner = new Scanner(inputStream, "UTF-8");
        // 循环读取内容
        while (scanner.hasNextLine()) {
            String next = scanner.nextLine();
            System.out.println(next);
        }
        scanner.close();
        inputStream.close();
    }

 OutputStream 概述

修饰 符及 返回 值类
方法签名
说明
void
write(int b)写入要给字节的数据
void
write(byte[]
b)
b 这个字符数组中的数据全部写入 os
int
write(byte[]
b, int off,
int len)
b 这个字符数组中从 off 开始的数据写入 os 中,一共写 len
void
close()关闭字节流
void
flush()
重要:我们知道 I/O 的速度是很慢的,所以,大多的 OutputStream 为 了减少设备操作的次数,在写数据的时候都会将数据先暂时写入内存的 一个指定区域里,直到该区域满了或者其他指定条件时才真正将数据写入设备中,这个区域一般称为缓冲区。但造成一个结果就是我们写的数据,很可能会遗留一部分在缓冲区中。需要在最后或者合适的位置, 调用 flush (刷新)操作,将数据刷到设备中。
 public static void main(String[] args) throws IOException {
        // 先创建一个File对象
        File file = new File("d:/temp/java78/hello.txt");
        // 根据File对象创建一个输出流
        FileOutputStream outputStream = new FileOutputStream(file);
        // 向文件中写入内容
        outputStream.write(100);
        outputStream.write(101);
        outputStream.write(102);
        // 刷新缓冲区
        outputStream.flush();
        // 关闭流
        outputStream.close();
    }

PrintWriter

public static void main(String[] args) throws FileNotFoundException {
        // 创建一个输出流
        FileOutputStream outputStream = new FileOutputStream("d:/temp/java78/hello.txt");
        // 根据输出流,创建一个PrintWriter
        PrintWriter printWriter = new PrintWriter(outputStream);
        // 写入文件
        printWriter.println("你好世界!!!");
        printWriter.println("hello world.");
        printWriter.println("我是用PrintWriter写入的内容");
        // 强制刷新缓冲区
        printWriter.flush();
        // 关闭流
        printWriter.close();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值