IO

本文详细介绍了Java中 FileInputStream 和 FileOutputStream 的使用,包括读取和写入字节、文件复制、文件属性判断等操作。同时展示了如何处理文件异常及确保流的正确关闭,强调了内存与硬盘交互的效率问题。
摘要由CSDN通过智能技术生成

问题:

我不是占两个byte么?

import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamTeast {
    public static void main(String[] args) {
        FileInputStream  fis =null;
            //创建输入对象
            try {
                fis =new FileInputStream("myfile");
                byte[] bytes =new byte[2];

                int readCount=0;
                while((  readCount=fis.read(bytes)) != -1){
                    System.out.println(new String(bytes, 0, readCount));
                }
            } catch (IOException fileNotFoundException) {
                fileNotFoundException.printStackTrace();
            } finally {
            //一定要分开try,不要一起try
            //一起try的时候,其中一个出现异常,可能会影响另一个流的关闭
            if (fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            }
        }
    }

在这里插入图片描述


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

//内存和硬盘交互频繁,浪费资源.
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamTeast {
    public static void main(String[] args) {
        FileInputStream  fis=null;
        try {
            fis =new FileInputStream("D:\\123");
          int readData =0;
          while((readData=fis.read())!=-1){
              System.out.println(readData);
          }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

//有袋子背
import java.io.FileInputStream; 
import java.io.IOException; 
public class FileInputStreamTeast {
    public static void main(String[] args) {
        FileInputStream  fis=null; 
        try {
            fis =new FileInputStream("D:\\123");
            byte[] bytes =new byte[4];
            int readCount =0;
            while((readCount = fis.read(bytes) )!= -1){
                System.out.println( new String(bytes , 0,readCount)); 
            } 
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述在这里插入图片描述

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class FileInputStreamTeast {
    public static void main(String[] args) {
        FileOutputStream  fos=null;
        try {
            //myfile文件不存在的时候会自动新建
            //将源文件清空然后重新写入
            //fos =new FileOutputStream("myfile");
            //以追加的方式在文件末尾写入.不会清空原文件内容.
            fos = new FileOutputStream("myfile",true);
            //开始写
            byte[] bytes =new byte[]{97,98,99,100};
            //将byte数组全部写出
            fos.write(bytes);
            fos.write(bytes);//abcd
            //将byte数组的一部分写出
            fos.write(bytes,0,2);
            //字符串
            String s= "\n我爱Java";
            //将字符串转化为byte字符数组
            byte[] bs =s.getBytes(StandardCharsets.UTF_8);
            //写入
            fos.write(bs);
            //写完之后,最后一定要刷新
            fos.flush(); 
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述

在这里插入图片描述

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileInputStreamTeast {
    public static void main(String[] args) {
        FileInputStream  fis =null;
        FileOutputStream  fos=null;
        try {
            //创建输入对象
            fis =new FileInputStream("myfile");
            //创建输出对象
            fos = new FileOutputStream("myfile2");
            //最核心的:一边读一边写
            byte[] bytes =new byte[1024*1024];//1M一次最多拷贝1M
            int readCount =0;
            while ((readCount=fis.read(bytes))!=-1){
                fos.write(bytes,0,readCount);
            }
            //写完之后,输入流最后一定要刷新
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //一定要分开try,不要一起try
            //一起try的时候,其中一个出现异常,可能会影响另一个流的关闭
            if (fis!=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

能用记事本编辑的都是普通文本文件,与后缀名无关.

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

File类

在这里插入图片描述

//file类的常用的方法
public static void main(String[] args) throws IOException {
      File f1=new File("D:\\file");
        //判断文件是否存在
       System.out.println(f1.exists());
        //如果不存在,则以文件的形式创建出来
        if(!f1.exists()){
            //文件形式创建
            f1.createNewFile();
        }
        //如果不存在,则以文件的形式创建出来
        if(!f1.exists()) {
            f1.mkdir();
        }
        //多重目录创建
        File f2 =new File("D:/a/b/c/c/d/f");
        if (!f2.exists()){
            f2.mkdirs();
        }
        File f3 =new File("D:\\user\\MobileFile\\mingyu.txt");
        //获取当前路径的父路径
         String parentPath =f3.getParent();
         System.out.println(parentPath);
 
      File parentFile =f3.getParentFile();
      System.out.println(parentFile.getAbsolutePath());
       File f4 =new File("myfile");
       System.out.println(f4.getAbsolutePath());//获取性对路径定义下的绝对路径
        //获取文件名
      System.out.println(f3.getName());
        //判断是不是一个目录
       System.out.println(f3.isFile());//true

        //判断是不是一个文件
        System.out.println(f3.isDirectory());//false
        long haomiao =f3.lastModified();//1970到现在的总毫秒数
        //将总毫秒数转换成日期
        Date time =new Date(haomiao);
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-Mm-dd HH:mm:ss  SSS");
        String strTime =sdf.format(time);
        System.out.println(strTime);
        File f=new File("D:\\user\\MobileFile ");
        File[] files =f.listFiles();
        for (File file: files) {
           System.out.println(file.getAbsolutePath());//文件绝对路径
            System.out.println(file.getName());//文件名称
        }
    }

小试牛刀:拷贝目录

package cn.zmy.demo1;
import java.io.*;
/*
拷贝目录
 */
public class CopyAll {
    public static void main(String[] args) {
        //拷贝源
        File srcFile =new File("D:\\aaa\\");
        //拷贝目标
        File destFile = new File("C:\\");
        //调用方法拷贝
        CopyDir(srcFile,destFile);
    }
    /**
     * 拷贝目录
     * @param srcFile 拷贝源
     * @param destFile 拷贝目标
     */
    private static void CopyDir(File srcFile, File destFile) {
        if(srcFile.isFile()){
            //srcFile如果是一个文件的话,递归结束.
            //是文件的时候需要拷贝.
            //....一边读一边写.
            FileInputStream in =null;
            FileOutputStream out=null;
            try {
                //读到这个文件
                in =new FileInputStream(srcFile);
                String path=(destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath()+"\\" ) +srcFile.getAbsolutePath().substring(3);
                //写到这个文件
                out =new FileOutputStream(path);
                //一边读一边写
                byte[] bytes =new byte[1024*1024];
                int readCount=0;
                while((readCount=in.read(bytes))!=-1){
                    out.write(bytes,0,readCount);
                }
                out.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (out == null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (in == null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return;
        }
        //获取源下面的子目录
      File[] files= srcFile.listFiles();
      for(File file:files){
          //获取所有文件的(包括目录和文件)绝对路径
          //System.out.println(file.getAbsolutePath());
          if(file.isDirectory()){
              //新建对应的目录
              //System.out.println(file.getAbsolutePath());
              String srcDir =file.getAbsolutePath();//源目录
            //System.out.println(srcDir.substring(3));
              String destDir = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath()+"\\" ) +srcDir.substring(3);
           //   System.out.println(destDir);
              File newFile = new File(destDir);
              if(!newFile.exists()){
                  newFile.mkdirs();
              }
          }
          //递归调用
          CopyDir(file,destFile);    //这个file可能是文件,也可能是目录
      }
    }
}

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值