栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA-IO流①

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA-IO流①

1、 文件
 public static void main(String[] args){
      //使用路径去创建一个Flie对象
      //两种写法:
      //第一种绝对路径,以根目录开头(windows的根路径是盘符,Mac和Linux都是根)
      //              使用使用\去描述我们的文件位置,或者使用单个/去分割文件的文职
      //第二种相对轮径,相对我们当前代码位置的一个路径
      
      //如何去看他当前的一个相对位置在哪里
      //第一步,通过相对位置,写一个czytetcazy.txt文件
      //Flie flie=new Flie("czytestczy.txt");
      //第二步,使用flie对象的creatNewFile()方法,将这个文件创建出来
      //if(!flie.exits()){
      //    try{
      //        file.creatNewFile();
      //    }catch(IOExcrption e){
      //        e.prnitStackTrace();
      //    }
      //}
      //第三步,执行该代码,看看这个czytestczy.txt在哪里
      //IDEA的相对路径起点位置在我们这个工程项目的位置。
      
      File file=new File("e:/KB18/test.txt");
      //判断对象是否存在
      System.out.println(file.exists());
      //判断当前文件对象是否为文件夹
      System.out.println(file.isDirectory());
      //获取相对路径(你过你实例化File对象使用的是绝对路径,这里会返回绝对路径)
      System.out.println(file.getPath());
      //获取绝对路径
      System.out.println(file.getAbsolutePath());
      //获取名称
      System.out.println(file.getName());
      //删除次对象
      System.out.println(file.delete());
      //创建文件
      try{
          //创建文件
          System.out.println(file.creatNewFile());
          //创建文件夹
          System.out.println(file.mkdir());
      }catch(Exception e){
          e.printStackTrace();
      }
      //返回文件是多少字节
      System.out.println("文件大小"+file.length());
  }
2、 IO流

2.1 什么是IO流

IO流归根结底就是输入/输出流

输入输出流我们一般会用两种方法进行分类

第一种方式,根据方向分IO流

​ 我们把从外界读入java环境种的操作,叫做输入流:InputStream,输入又称之为读操作Reader

​ 我们把从java环境输出到外界的流称为输出流:OutputStream,输出又称之为写操作Wirter

第二种方法,根据每次读取的一个长度进行区分

​ 一个称之为字节流:InputStream、OutputStream

​ 一个称之为字符流:Reader、Writer

2.2、 关于流的特点

​ 与栈不同,流是属于先进先出,可以类似于羽毛球球盒

2.3、 IO流四大家族

在JDK的java.io包下存在各种IO流,其中大种类分为以下四个大流,他们是四大流的头目

​ 字节流:InputStream、OutputStream

​ 字符流:Reader、Writer

这四个类均属于抽象类

对于io包下的所有类,如果是以Stream结尾,我们均可以认知为是字节流;

​ 如果是以Reader,Writer结尾,我们均可以认知为是字符流;

​ 四个最主要的类均实现了我们Closeable接口,说明这四个流再每次使用完之后也必须关闭(请每次关闭之前确认该流已经执行完毕,不会被再次调用,这个时候才会关流,否则回报错)(每次使用完都必须关系流,因为每个流其实都是属于正在占用系统的资源,为了防止资源被占用或这出现一些意外的问题,请记得关流)

​ 其中关于OutputStream和Writer均实现了一个叫做Flushable接口,该接口可让我们的输出流进行刷新缓冲层,在关流或者这个文件完成传递之前必须要进行刷新,否则会出现文件传递不完整,写出文件出现,包括但不限于,文件不完成,文件无法打开,文件无法正常使用等等问题。

2.4、 字节流和字符流的区别

首先要说明文本文件和非文本文件的区别

txt文件就属于文本文本,java文件是不是属于文本我呢见?java 文件也属于文本文件。就是一般可以直接用文本编辑器打开,显示正常的文件就是文本文件,对于我们计算机而言,所有的非文本文件都会存在自己的已给格式问题。

字符流是专门用来进行操作文本文件的,而字节流可以操作所有文件。

3、 使用FileInputStream读取文件

简单的读取

  
  public static void main(String[] args){
      FileInputStream fis=null;
      try{
          fis=new FileInputStream("test.txt");
          int readNum=0;
          while((readNum=fis.read())!=-1){
              System.out.print((char)readNum);
          }
      }catch(FileNotFoundException e){
          e.printStackTrance();
      }catch(IOException e){
          e.printStackTrance();
      }finally{
          if(fis!=null){
              try{
                  fis.colse();
              }ctach(IOEcxeption e){
                  e.printStackTrance();
              }
          }
      }
  }

使用一个缓冲带的形式读取文字

public static void main(String[] args){
      try{
          fis=new FileInputStream("test.txt");
          int readNum=0;
          //创建一个袋子,每次先把袋子装满,再去把整个袋子卸货
          byte[] bs=new byte[3];
          while ((readNum=fils.read(bs))!=-1){
              //通过new String将我们的byte[]袋子执行装成String
              //第一个参数为袋子
              //第二个参数为起点位置
              //第三个参数为袋子的重点位置
              System.out.print(new String(bs,0,readNum));
          }
      }catch(FileNotFoundException e){
          e.printStackTrace();
      }catch(IOException e){
          e.printStackTrace();
      }finally{
          if(fis!=null){
              try{
                  fis.colse();
              }catch(IOException e){
                  e.printStackTrace();
              }
          }
      }
  }
  
  FileInputStream fis=new FileInputStream("test.txt");
  //这个方法是返回整个文字可以读取的字节长度的大小
  System.ouyt.println("fis========"+fis.available());
4、 使用FileOutPutStream将文件新增内容

  public static void main(String[] args){
      FileOutputStream pos=null;
      try{
          //此时第二个参数boolean决定我们是在目标文件的末尾处添加,还是整体的覆盖
          //如果为ture则会追加操作,如果是false 则是覆盖
          //如果不写这个参数,默认是false,默认覆盖
          pos=new FileOutputStream("test.txt",false);
          byte[] byte="你好中国123123".getByte("UTF-8");
          pos.write(butes);
      }catch(FileNotFoundExcrption e){
          e.printStackTrace();
      }catch(UnsupportedEncodingExcrption e){
          e.printStackTrace();
      }catch(IOExcrption e){
          e.printStackTrace();     
      }finally{
          if(pos!=mull){
              try{
                  pos.flush();
                  pos.close();
              }catch(IOExcption e){
                  e.printStackTrace();     
              }
          }
      }
  }
5、文件复制

  public static void main(String[] args){
      //复制文件
      //一边读取文件一边复制文件
      FileOutputStream fos=null;
      FileInputStream fis=null;
      try{
          fou=new FileOutputStream("E:\KB18\test.jpg");
          fis=new FileInputStream("E:\KB18\图片本体1.jpg");
          byte[] b=new byte[1024];
          int readNum=0;
          //使用read方法读取数,并且将值赋给readNum确定每次读取的字节数
          while((readNum=fis.read(b))!=-1){
              System.out.println(readNum);
              fos.write(b,0,readNum);
          }
      }catch(Exception e){
          e.printStackTrace();
      }finally{
          if(fis!=null){
              try{
                  fis.close();
              }ctach(IOException e){
                  e.printStackTrace();
              }
          }
          if(fos!=null){
              try{
                  fos.flush();
                  fos.close();
              }catch(IOException e){
                  e.printStackTrace();
          
      }
          }
      }
  }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/855368.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号