zoukankan      html  css  js  c++  java
  • java中的文件操作总结(干货)

    转载:http://www.jb51.net/article/105418.htm

    File类简介

     1 package com.file;
     2  
     3 import java.io.File;
     4 import java.io.IOException;
     5  
     6 /**
     7  * Created by elijahliu on 2017/2/10.
     8  */
     9 public class filetest {
    10   public static void main(String[] args) {
    11     File file = new File("hello.txt");
    12     //是否存在
    13     if (file.exists()) {
    14       //文件
    15       System.out.println(file.isFile());
    16       //路径(文件夹)
    17       System.out.println(file.isDirectory());
    18  
    19       File nameto = new File("new Hello.txt");
    20       file.renameTo(nameto);//这里就是重命名文件的操作,直接新建一个file对象然后使用renameTo方法可以重命名文件
    21  
    22     } else {
    23       System.out.println("文件不存在");
    24       try {
    25         file.createNewFile();
    26         System.out.println("文件已被创建");
    27       } catch (IOException e) {
    28         System.out.println("文件无法创建");
    29       }
    30     }
    31     if (file.exists()) {
    32       //删除文件
    33       file.delete();
    34       System.out.println("删除文件");
    35     } else {
    36     }
    37   }
    38 }

    文件夹操作

     1 package com.file;
     2  
     3 import java.io.File;
     4  
     5 /**
     6  * Created by elijahliu on 2017/2/11.
     7  */
     8 public class HelloFolder {
     9   public static void main(String[] args) {
    10     File folder = new File("my new folder");
    11     if (folder.mkdir()) {//创建文件夹 判断是否成功
    12       System.out.println("文件夹创建完成");
    13       File newfolder = new File("myn new foleder - new");
    14       folder.renameTo(newfolder);//这里重命名了文件夹 文件夹的重命名是可以单独更改一级的文件夹名的 而这一级下面的文件夹不变 保存目录结构
    15       if (folder.delete()) {
    16         System.out.print("done");//这里的删除只能删除空文件夹,如果文件夹中有东西,那么则不能删除,不问三七二十一直接删除一个非空文件夹是非常不负责任的
    17       } else {
    18         System.out.println("fail");
    19       }
    20  
    21     }else{
    22       if (folder.exists()) {
    23         System.out.println("文件夹已经存在不用创建");
    24       }else{
    25         System.out.println("文件夹创建失败");
    26       }
    27     }
    28     File folders = new File("my new folder/one/two/three/main");
    29     folders.mkdirs();//在java中用mkdir只能创建一个,mkdirs可以创建多级目录
    30  
    31   }
    32 }

    文件属性设置

    public void printFiles(File dir,int tab) {//tab为不同目录结构的缩进量
      if (dir.isDirectory()) {
        File next[] = dir.listFiles();//判断如果是目录 则返回目录所有的文件名数组用于遍历文件夹
        for (int i = 0;i<next.length;i++) {//层次缩进输出
          System.out.print("---");
        }
        for(int i = 0;i<next.length;i++) {//这里用了递归获取目录结构
          System.out.println(next[i].getName());
          if (next[i].isFile()) {
            printFiles(next[i],++tab);
     
          }
        }
      }
    }

    文件简单读写

    package com.file;
     
    import java.io.*;
     
    /**
     * Created by elijahliu on 2017/2/11.
     */
    public class ReadFile {
      public static void main(String[] args) {
        File file = new File("new Hello.txt");
        if(file.exists()){
          System.err.print("exsit");
          try (FileInputStream fis = new FileInputStream(file)) {//文件输入流 这是字节流
     
            InputStreamReader isr = new InputStreamReader(fis,"GBK");//inputstreamReader是一个字节流,将字节流和字符流转化的时候,就需要根据你的编译器(我用的是GBK)制定一个编码方式,不然就会乱码
            BufferedReader br = new BufferedReader(isr);//字符缓冲区
     
            String line;
            while((line = br.readLine())!=null){//这里将缓冲区里的内容如果非空就读出来打印
              System.out.println(line);
     
            }
            br.close();//最后将各个线程关闭
            isr.close();
            fis.close();
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
        File newfile = new File("newtext.txt");
        try {
          FileOutputStream fos = new FileOutputStream(newfile);//这里如果文件不存在会自动创建文件
          OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");//和读取一样这里是转化的是字节和字符流
          BufferedWriter bw = new BufferedWriter(osw);//这里是写入缓冲区
     
          bw.write("厉害了我的哥");//写入字符串
     
          bw.close();//和上面一样 这里后打开的先关闭 先打开的后关闭
          osw.close();
          fos.close();
          System.out.println("done");
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
     
      }
    }
  • 相关阅读:
    Linux中带颜色输出的printf使用简介(33)
    Windows中检测当前是否有窗口全屏
    duilib WindowImplBase BUG修复 --- 按一次ESC键, 关闭多个窗口
    【转】OSI七层模型与TCP/IP五层模型
    linux修改文件所有者和文件所在组
    【转】开发人员该如何应对线上故障
    【转】jstack命令的使用
    【转】【JVM】jmap命令详解----查看JVM内存使用详情
    【转】【JVM】jstat命令详解---JVM的统计监测工具
    阿里开源java诊断工具Arthas
  • 原文地址:https://www.cnblogs.com/seven1314pp/p/9104814.html
Copyright © 2011-2022 走看看