当前位置 博文首页 > 文章内容

    Java IO流的介绍&基本使用

    作者: 栏目:未分类 时间:2020-07-20 18:01:52

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    IO流的基本介绍使用

     

    * @author: Connor-* @description:  File类 名称、路径
    *        getName() 名称
    *        getPath() 相对、绝对
    *        getAbsolutePath() 绝对
    *        getParent() 上层路径,没有返回null
    *   
    * @date: 2020年4月2日
    * @time: 下午1:37:12
    */
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class IOTest01 {
         
        @Test
        public void testA() {
    
            File file = new File("IO_study01/gtaf2.jpg");
            //基本操作
            System.out.println("名称"+file.getName());
            System.out.println("路径"+file.getPath());
            System.out.println("绝对路径"+file.getAbsolutePath());
            System.out.println("父路径"+file.getParent());
            System.out.println("父对象"+file.getParentFile());
    
      }

    文件的基本使用

    * @author: Connor-* @description:  File类 名称、路径
    *        getName() 名称
    *        getPath() 相对、绝对
    *        getAbsolutePath() 绝对
    *        getParent() 上层路径,没有返回null
    *   
    * @date: 2020年4月2日
    * @time: 下午1:37:12
    */
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class IOTest01 {
         
        @Test
        public void testA() {
    
            File file = new File("IO_study01/gtaf2.jpg");
            //基本操作
            System.out.println("名称"+file.getName());
            System.out.println("路径"+file.getPath());
            System.out.println("绝对路径"+file.getAbsolutePath());
            System.out.println("父路径"+file.getParent());
            System.out.println("父对象"+file.getParentFile());
    
      }

     

    * @author: Connor-* @description: File类 其他信息
    *
    *               length():文件的字节数,文件不存在返回0
    *               
    *               createNewFile():指定文件存在,返回false
    *               delete():删除已经存在的文件
    *
    * @date: 2020年4月2日
    * @time: 下午1:37:12
    */
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class IOTest03 {
         @Test
         public void testA() {
              File file2 = new  File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01/s.jpg");
              System.out.println(file2.length());
         }
         
         @Test
         public void testB() throws IOException {
              File file = new  File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01/io.txt");
              boolean b = file.createNewFile();
              System.out.println(b);
         }
    }

    文件夹操作

    /**
    *
    * @author: Connor-郭
    * @description: 创建目录
    *            1.mkdir():上级目录不存在。无法创建
    *            2.mkdirs():上级目录不存在一同创建
    *
    *
    * @date: 2020年4月2日
    * @time: 下午1:56:47
    */
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class DirDemo01 {
         
         @Test
         public void testA() {
    //        File file = new  File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01/dir/test");
    //        
    //        //创建目录 mkdirs
    //        boolean mkdirs = file.mkdirs();
    //        System.out.println(mkdirs);
              
              //创建目录 mkdir
              File file1 = new  File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01/dir/test2");
              boolean mkdir = file1.mkdir();
              System.out.println(mkdir);
         }
    }

    文件夹递归操作

    * @author: Connor-* @description:
    *            列出下一级
    *            1.list() 列出下级名称
    *            2.listFiles():列出下级File对象
    *
    * @date: 2020年4月2日
    * @time: 下午1:56:47
    */
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class DirDemo02 {
         public static void main(String[] args) {
              File file1 = new  File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01");
              
              pringName(file1, 0);
         }
         /**
          * 递归打印 文件夹子孙目录
          *
          * @param file
          * @param deep
          */
         public static void pringName(File file, int deep) {
         
              for (int i = 0; i < deep; i++) {
                  System.out.print("-"); //分隔符
              }
              if (null == file || !file.exists()) {//判断文件是否存在
                  return;
              }
              System.out.println(file.getName());//打印文件名
              
              if (file.isDirectory()) {//判断是否是文件夹
                  for (File s : file.listFiles()) {//文件夹继续遍历 递归
                       pringName(s, deep + 1);//递归调用
                  }
              }
         }
         @Test
         public void testA() {
              File file1 = new  File("D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01");
              // 列出下级名称
              String[] list = file1.list();
              for (String string : list) {
                  System.out.println(string);
              }
              // 列出下级File对象
              File[] listFiles = file1.listFiles();
              for (File file : listFiles) {
                  System.out.println(file.getAbsolutePath());
              }
         }
    }

    面向对象练习,获取文件大小

    /**
    *
    * @author: Connor-郭
    * @description: 使用面向对象 统计 文件大小.
    *
    *
    * @date: 2020年4月2日
    * @time: 下午1:56:47
    */
    public class DirDemo03Count {
         // 大小
         private long len;
         // 文件夹
         private String dirPath;
         // 源文件
         private File src;
         // 文件数量
         private int fileCount;
         // 文件夹数量
         private int DirCount = -1; // 根文件夹 子孙级包含自身文件保存自身
         public DirDemo03Count(String path) {
              this.dirPath = path;
              this.src = new File(path);
              count(this.src);
         }
         // 获取 文件大小
         public void count(File file) {
              // 判断文件非空,存在
              if (null != file && file.exists()) {
                  // 判断是否文件
                  if (file.isFile()) {
                       // 文件自增
                       this.fileCount++;
                       // 加长度
                       this.len += file.length();
                  } else {
                       // 文件夹自增
                       this.DirCount++;
                       // 继续遍历 子孙级文件
                       for (File f : file.listFiles()) {
                            // 递归调用
                            count(f);
                       }
                  }
              }
         }
         public long getLen() {
              return len;
         }
         public Integer getFileCount() {
              return fileCount;
         }
         public Integer getDirCount() {
              return DirCount;
         }
         public static void main(String[] args) {
              DirDemo03Count dirDemo03Count = new  DirDemo03Count(
                       "D:/software/JavaWebTools/SpringEclipse-workspace/IO_study01");
              System.out.println("文件大小  " +  dirDemo03Count.getLen());
              System.out.println("文件数量" +  dirDemo03Count.getFileCount());
              System.out.println("文件夹数量 " +  dirDemo03Count.getDirCount());
         }
    }

    文件编码

    **
    *  从字节到 字符  编码操作
    *  从字符到 字节  解码操作
    *  
    * @author: Connor-* @description:
    *   
    * @date: 2020年4月2日
    * @time: 下午5:00:26
    */
    public class ContentEncode {
         public static void main(String[] args) throws  Exception {
         
              decode();
         }
         
         //编码 字节到 字符
         public static void encode() throws Exception {
              String str = "郭宗鑫a";
              //编码: 字节数组
              byte[] bytes = str.getBytes(); //默认使用工程字符集 UTF-8   中文2字节,英文1字节
              System.out.println(bytes.length);
              
              bytes = str.getBytes("UTF-16LE");//默认1个字符为2个字节
              System.out.println(bytes.length);
              
              bytes = str.getBytes("GBK");//默认  中文2字节,英文1字节
              System.out.println(bytes.length);
         }
         
         //解码 字符到 字节
         public static void decode() throws Exception {
              
              String str = "郭宗鑫a";
              //编码: 字节数组
              byte[] bytes = str.getBytes(); //默认使用工程字符集 UTF-8   中文2字节,英文1字节
              
              //解码 字符
              str = new String(bytes,"UTF-8");
              System.out.println(str);
              
              str = new String(bytes,"UTF-16LE");
              System.out.println(str);
              
              str = new String(bytes,"GBK");
              System.out.println(str);
         }
         
    }

    个人记录,勿喷勿水