Java文件与IO

前言认识文件

狭义上的文件(file)。针对硬盘这种持久化存储的I/O设备,当我们想要进行数据保存时,往往不是保存成一个整体,而是独立成一个个的单位进行保存,这个独立的单位就被抽象成文件的概念,文件由文件夹以及目录来管理;

绝对路径与相对路径

绝对路径: 是固定的,是不变的,唯一路径
相对路径 : 是根据你当前所在的节点所往后推导的路径

普通文件与二进制文件

普通文件: 用记事本打开能看
二进制文件: 用记事本打开看不了
也可以看下面字符集的编码格式

Java中的操作文件

File 类

构造方法
在这里插入图片描述

 public static void main(String[] args) throws IOException {
    
    
        File file = new File("..\\hello-world.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());
        // 创建一个新的文件
        System.out.println(file.createNewFile());
        //删除一个文件
         System.out.println(file.delete());
         //创建目录
          System.out.println(dir.mkdir());
          //文件重命名
           System.out.println(file.renameTo(dest));
     
   }

文件的读写

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() 关闭字节流

InputStream 只是一个抽象类,要使用还需要具体的实现类。

 try (InputStream is = new FileInputStream("hello.txt")) {
    
    
            while (true) {
    
    
                int b = is.read();
                if (b == -1) {
    
    
                    // 代表文件已经全部读完
                    break;
               }
                
                System.out.printf("%c", b);
           }
       }
public static void main(String[] args) throws IOException {
    
    
//文件内容中填充中文
        try (InputStream is = new FileInputStream("hello.txt")) {
    
    
            byte[] buf = new byte[1024];
            int len;
            while (true) {
    
    
                len = is.read(buf);
                if (len == -1) {
    
    
                    // 代表文件已经全部读完
                    break;
               }
                // 每次使用 3 字节进行 utf-8 解码,得到中文字符
                // 利用 String 中的构造方法完成
                // 这个方法了解下即可,不是通用的解决办法
                for (int i = 0; i < len; i += 3) {
    
    
                    String s = new String(buf, i, 3, "UTF-8");
                    System.out.printf("%s", s);
               }
           }
       }
   }

对字符类型直接使用 InputStream 进行读取是非常麻烦且困难的,所以,我们使用一种我们之前比较熟悉的类来完成该工作,就是 Scanner 类。

try (InputStream is = new FileInputStream("hello.txt")) {
    
    
           try (Scanner scanner = new Scanner(is, "UTF-8")) {
    
    
               while (scanner.hasNext()) {
    
    
                   String s = scanner.next();
                   System.out.print(s);
               }
           }
       }

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(刷新)操作,将数据刷到设备中。

 try (OutputStream os = new FileOutputStream("output.txt")) {
    
    
 // 字符写入
            os.write('H');
            os.write('e');
            os.write('l');
            os.write('l');
            os.write('o');
            // 不要忘记 flush
            os.flush();
       }
 try (OutputStream os = new FileOutputStream("output.txt")) {
    
    
           String s = "Nothing";
            byte[] b = s.getBytes();
            os.write(b);
          
            // 不要忘记 flush
            os.flush();
       }
 try (OutputStream os = new FileOutputStream("output.txt")) {
    
    
            String s = "你好中国";
            byte[] b = s.getBytes("utf-8");
         os.write(b);
            
            // 不要忘记 flush
            os.flush();
       }

猜你喜欢

转载自blog.csdn.net/qq_56454895/article/details/131693921
今日推荐