java中的IO流(File类的方法)

博客介绍了Java中File类,它是文件和目录路径名的抽象表示。还详细阐述了IO流,目的是读写文件内容,流以先进先出方式传输信息。介绍了字节流,包括字节输入流InputStream和字节输出流OutputStream,并给出了多个使用示例,如文件读取、拷贝等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • File类

  • 文件和目录路径名的抽象表示形式。
    /
    public class FileDemo01 {
    public static void main(String[] args) throws IOException {
    /

    * File(File parent, String child)
    根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
    File(String pathname)
    通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
    File(String parent, String child)
    根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
    */
    File file1=new File(“D:\AAA”); //“D:/AAA/haha.txt”
    System.out.println(file1);

    File file2=new File("D://","AAA//hehe.txt");
    System.out.println(file2);
    
    File file3=new File(file1,"haha.txt");
    System.out.println(file3);
    
     
    //1.boolean canWrite() 测试应用程序是否可以修改此抽象路径名表示的文件。  如果文件设置只读,就不可修改
    System.out.println("canWrite():"+file3.canWrite());
    //2.boolean setReadOnly()
    System.out.println("setReadOnly():"+file3.setReadOnly());
    System.out.println("canWrite():"+file3.canWrite());
    //3.boolean createNewFile()当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
    //如果文件所在路径不存在,报错找不到指定的路径,如果路径都存在创建一个新的文件
    System.out.println("createNewFile():"+file2.createNewFile());
    //4. boolean delete() 删除
    System.out.println("delete():"+file2.delete());
    System.out.println("delete():"+file1.delete());
    //5.boolean exists()   测试此抽象路径名表示的文件或目录是否存在。 
    System.out.println("exists():"+file3.exists());
    System.out.println("exists():"+file2.exists());
    /*
     * 6.File getAbsoluteFile() 
    	          返回此抽象路径名的绝对路径名形式。 
    	 String getAbsolutePath() 
    	          返回此抽象路径名的绝对路径名字符串。 
     */
    File file4=new File("hengheng.txt");
    System.out.println("getAbsoluteFile():"+file4.getAbsoluteFile());
    System.out.println("getAbsolutePath():"+file4.getAbsolutePath());
    
    System.out.println(new File("D:").getTotalSpace());
    System.out.println(new File("D:").getFreeSpace());
    
    //7.String getName()  返回由此抽象路径名表示的文件或目录的名称。 
    System.out.println("getName():"+file3.getName());
    System.out.println("getName():"+file1.getName());
    
    /*
     * 8.String getParent() 
              返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。 
       File getParentFile() 
              返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null。 
     */
    System.out.println("getParent():"+file3.getParent());
    System.out.println("getParentFile():"+file1.getParentFile());
    //9.boolean isAbsolute() 测试此抽象路径名是否为绝对路径名。 
    System.out.println("isAbsolute():"+file3.isAbsolute());
    System.out.println("isAbsolute():"+file4.isAbsolute());
    /*
     * 10.boolean isDirectory() 
    	          测试此抽象路径名表示的文件是否是一个目录。 
    	  boolean isFile() 
    	          测试此抽象路径名表示的文件是否是一个标准文件。 
    	  boolean isHidden() 
    	          测试此抽象路径名指定的文件是否是一个隐藏文件。 
     */
    System.out.println("isDirectory():"+file1.isDirectory());
    System.out.println("isFile():"+file1.isFile());
    
    //11.long lastModified()  返回此抽象路径名表示的文件最后一次被修改的时间。 
    System.out.println("lastModified():"+new SimpleDateFormat().format(new Date(new File("D:/test.txt").lastModified())));
    //12.long length()  返回由此抽象路径名表示的文件的长度。 
    System.out.println("length():"+file3.length());
    
    /*
     * 12.String[] list() 
    	  File[] listFiles() 
     */
    String[] strs=file1.list();
    System.out.println(Arrays.toString(strs));
    System.out.println(Arrays.toString(file1.listFiles()));
    
    /*
     * 13.boolean mkdir() 
    	          创建此抽象路径名指定的目录。 
    	  boolean mkdirs() 
    	          创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。 
     */
    File file6=new File("D:/DDD/EEE");
    File file7=new File("D:/CCCCC");
    File file8=new File("E:/CCCCC");
    System.out.println("mkdir():"+file6.mkdir());
    System.out.println("mkdir():"+file6.mkdirs());
    
    //boolean renameTo(File dest)  重新命名此抽象路径名表示的文件。 
    System.out.println("renameTo():"+file7.renameTo(file8));;
    

    }
    }

  • IO:

  • 目的:读写文件中的内容

  • 流:一连串流动的数据,先进先出的方式传输信息,管道

  • 数据源 目的地–>以程序为中心划分读入写出

  • 流的分类:

  •  按照流向分:
    
  •  		输入流
    
  •  		输出流
    
  •  按操作单元分:
    
  •  		字节流
    
  •  		字符流
    
  •  按功能分:
    
  •  		节点流
    
  •  		功能流
    
  • 分类之间是相符想成的,互不冲突

  • 字节流:是万能的 ***** 功能:节点流

  • InputStream 字节输入流 此抽象类是表示字节输入流的所有类的超类。

  •  FileInputStream 从文件系统中的某个文件中获得输入字节。
    
  • OutputStream 字节输出流

  • public class IODemo02 {
    public static void main(String[] args) throws IOException {
    //FileInputStream(File file)
    //1.建立联系
    File file=new File(“D:/AAA/haha.txt”);
    //2.选择流
    InputStream is=new FileInputStream(file);
    //3.读入
    //每次读取一个字节数据,如果已达到末尾返回-1
    int num=is.read();
    //4.处理数据
    System.out.println((char)num);
    System.out.println((char)(is.read()));
    System.out.println((char)(is.read()));
    System.out.println((char)(is.read()));
    System.out.println((char)(is.read()));
    System.out.println((char)(is.read()));
    System.out.println(is.read()); //-1
    //5.关闭
    is.close();
    }
    }

  • 每次手动读取一个字节长度,麻烦,循环读取
    */
    public class IODemo03 {
    public static void main(String[] args) throws IOException {
    //FileInputStream(File file)
    //1.建立联系
    File file=new File(“D:/AAA/haha.txt”);
    //2.选择流
    InputStream is=new FileInputStream(file);
    //3.读入
    //每次读取一个字节数据,如果已达到末尾返回-1
    int num;
    while((num=is.read())!=-1){
    System.out.println((char)num);
    }
    //5.关闭
    is.close();
    }
    }

  • 每次读取一个字节长度,效率低,一卡车一卡车的读

  • 1.准备卡车(容器,存储多个数据)
    */
    public class IODemo04 {
    public static void main(String[] args){
    //FileInputStream(File file)
    //1.建立联系
    File file=new File(“D:/AAA/haha.txt”);
    //2.选择流
    InputStream is=null;
    try {
    is = new FileInputStream(file);
    //3.准备卡车
    byte[] car=new byte[1024]; //一般使用1024的整数倍
    int len=-1;
    //int read(byte[] b) 返回值:返回读取到是数组b中的数据个数,没有读到返回-1
    //int len=is.read(car);
    while((len=is.read(car))!=-1){
    System.out.println(new String(car,0,6));
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    //5.关闭
    try {
    if(is!=null){
    is.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }
    }
    字节输出流 OutputStream 此抽象类是表示输出字节流的所有类的超类
    FileOutputStream文件输出流是用于将数据写入 File

    尝试:文件的拷贝
    数据源 程序 目的地
    */
    public class IODemo05 {
    public static void main(String[] args){
    //1.建立联系
    //File dest=new File(“D:/test.txt”);
    //2.选择流
    OutputStream out=null;
    try {
    out=new FileOutputStream(“D:/test.txt”,true); //第二个参数默认false不追加,true追加
    //3.准备数据
    int num=99;
    byte[] car=“告白气球”.getBytes();
    //4.写出
    //每次只写一个字节
    /out.write(num);
    out.write(101);
    /
    //每次写出多个
    out.write(car);
    //5.刷出
    out.flush();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally{
    //6.关闭
    try {
    if(out!=null){
    out.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值