想进大厂?这些文件操作IO流你应该了解(内含详细截图)

                                                           

                              大家好,我是爱动漫更爱编程的小工同学~

                                                            

File概述

属性

构造方法

方法

代码示例

示例1:普通文件的创建、删除

import java.io.File;
import java.io.IOException;

public class Main2 {
    public static void main(String[]args) throws IOException
    {
        //目前来说,这个文件是不存在的
        File  file=new File("hello -world.txt");
        //判断文件是否存在
        System.out.println(file.exists());
        //判断file是否是一个目录
        System.out.println(file.isDirectory());
        //判断file是否是一个普通文件
        System.out.println(file.isFile());
        //创建一个空文件
        System.out.println(file.createNewFile());
      
        System.out.println(file.exists());
       
        System.out.println(file.isDirectory());
       
        System.out.println(file.isFile());
        //删除一个普通文件
        System.out.println(file.delete());

        System.out.println(file.exists());
    }
}
   

//运行结果 
   false
   false
   false
   true 
   true
   false
   true
   true
   false

示例2:读取文件的信息

import java.io.File;
import java.io.IOException;

public  class Main2  {
    public static void main(String[] args) throws IOException {
        File file=new File("..\\hello-world.txt");
        //返回File对象的父目录文件路径
        System.out.println(file.getParent());
        //返回文件名
        System.out.println(file.getName());
        //返回File对象的文件路径
        System.out.println(file.getPath());
        //返回File对象的绝对路径
        System.out.println(file.getAbsolutePath());
        //返回File对象修饰过的绝对路径
        System.out.println(file.getCanonicalPath());
    }
}

//输出结果

      ..
      hello-world.txt
      ..\hello-world.txt
      D:\untitled1\..\hello-world.txt
      D:\hello-world.txt

示例3:创建目录

import java.io.File;
import java.io.IOException;

public  class Main2  {
    public static void main(String[] args) throws IOException {
        //some-parent 和some-dir都不存在
        File dir=new File("some-parent\\some-dir");
        System.out.println(dir.isDirectory());
        System.out.println(dir.isFile());
        //使用mkdir()时,如果中间目录不存在,则无法创建成功,mkdirs()可以解决这个问题0
        System.out.println(dir.mkdirs());
        System.out.println(dir.isDirectory());
        System.out.println(dir.isFile());
    }
}

运行结果

   false
   false
   true
   true
   false

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

InputStream

方法

 FileInputStream

构造方法

代码示例

示例1: 文件的完全读入

注意:需要先在项目目录下准备好一个hello.txt文件,

​
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static void main(String[] args)  throws IOException {
        try(InputStream is=new FileInputStream("D:\\untitled1\\src\\Internet\\hello.txt")){
            while(true)
            {
                int b=is.read();
                if( b==-1){
                    break;
            }
                System.out.println(b);
            }
        }
    }
}

​文件内容:12345
运行结果:49 50 51 52 53
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static void main(String[] args)  throws IOException {
        try(InputStream is=new FileInputStream("D:\\untitled1\\src\\Internet\\hello.txt")){
           byte[]buf=new byte[1024];
           int len;
            while(true)
            {
                len=is.read(buf);
                if( len==-1){
                    break;
            }
               for(int i=0;i<len;i++)
               {
                   System.out.print(buf[i]);
               }
            }
        }
    }
}
文件内容:12345
运行结果:49 50 51 52 53

相比较而言,后一种的IO次数更少,性能更好

示例2:利用Scanner进行字符读取

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        try(InputStream is=new FileInputStream("D:\\untitled1\\src\\Internet\\hello.txt"))
        {
            Scanner scanner=new Scanner(is,"UTF-8");
            while(scanner.hasNext())
            {
                String s=scanner.next();
                System.out.print(s);
            }
        }
    }
}
文件内容:你好,世界
运行结果:你好,世界

OutputStream

方法

 代码示例

示例1:利用OutputStreamWriter进行字符写入

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {
    public static void main(String[] args) throws IOException {
        try(OutputStream os=new FileOutputStream("D:\\untitled1\\src\\Internet\\hello.txt"))
        {
            //1单个字符读入
            os.write('H');

            os.write('e');

            os.write('l');

            os.write('l');

            os.write('o');
            //2 以byte数组形式读入
            byte[]b=new byte[]{(byte)'W',(byte)'o',(byte)'r',(byte)'l',(byte)'d'};
            os.write(b);
            //3 读入中文
            String s="你好中国";
            byte[]B=s.getBytes("utf-8");
            os.write(B);
            os.flush();
            }
        }
    }

运行结果

我们还可以用PrintWriter类来完成输出,因为它提供了我们熟悉的print/println/printf方法

代码示例

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        try(OutputStream os=new FileOutputStream("D:\\untitled1\\src\\Internet\\hello.txt"))
        {
           try {
               OutputStreamWriter osWrite=new OutputStreamWriter(os,"UTF-8");
            {
                try(PrintWriter writer=new PrintWriter(osWrite))
                    {
                    writer.println("我是第一行");
                    writer.print("我是第二行\r\n");
                    writer.printf("%d: 我是第三行\r\n",1+1);
                    writer.flush();
                    }
                }
            } catch (UnsupportedEncodingException e) {
               e.printStackTrace();
           }
        }
        }
    }

运行结果

    🚀每日一图

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值