本文主要介绍 Java IO 中几个常见的操作,读取字符,写入字符,复制文件,复制文件夹等。

直接上代码啦。

  1. package com.liuyanzhao.io;
  2. import java.io.*;
  3. import java.util.Date;
  4. public class FileDemo {
  5.     /**
  6.      * 输出文件基本信息
  7.      * @param filePath  文件的路径
  8.      */
  9.     public static void getFileInfo(String filePath) {
  10.         File file = new File(filePath);
  11.         System.out.println("文件名称:"+file.getName());
  12.         System.out.println("文件是否存在:"+file.exists());
  13.         System.out.println("文件相对路径:"+file.getPath());
  14.         System.out.println("文件绝对路径:"+file.getAbsolutePath());
  15.         System.out.println("是否可执行文件:"+file.canExecute());
  16.         System.out.println("文件可以读取:"+file.canRead());
  17.         System.out.println("文件可以写入:"+file.canWrite());
  18.         System.out.println("文件上级路径:"+file.getParent());
  19.         System.out.println("文件大小:"+file.length()+"B");
  20.         System.out.println("文件最后修改时间:"new Date(file.lastModified()));
  21.         System.out.println("是否文件类型:"+file.isFile());
  22.         System.out.println("是否文件夹类型:"+file.isDirectory());
  23.     }
  24.     /**
  25.      * 读取文件字符
  26.      * @param filePath  文件路径
  27.      * @throws IOException
  28.      */
  29.     public static void readFile(StringBuilder sb,String filePath) throws IOException {
  30.         File file = new File(filePath);
  31.         if(file.exists()) {
  32.             FileReader fileReader = new FileReader(file);//创建文件字符输入流
  33.             char[] data = new char[20];//表示一次循环读取20个字符
  34.             int rs = 0;
  35.             //fileReader.read()将字符读入data数组中,并返回读取字符的数量
  36.             while((rs=fileReader.read(data))>0) {
  37.                 sb.append(data,0,rs);
  38.             }
  39.             fileReader.close();
  40.         } else {
  41.             System.out.println("File Not Found!");
  42.         }
  43.     }
  44.     /**
  45.      * 写入字符到文件中
  46.      * @param sb        写入的内容
  47.      * @param filepath  被写入的文件路径
  48.      * @throws IOException
  49.      */
  50.     public static void writeFile(StringBuilder sb,String filepath) throws IOException {
  51.         File file = new File(filepath);
  52.         if(file.exists()) {
  53.             FileWriter fileWriter = new FileWriter(file);//创建文件字符输出流
  54.             fileWriter.write(sb.toString());
  55.             fileWriter.close();
  56.         } else {
  57.             System.out.println("File Not Found!");
  58.         }
  59.     }
  60.     /**
  61.      * 新建文件
  62.      * @param filepath      文件路径
  63.      * @throws IOException
  64.      */
  65.     public static void createFile(String filepath) throws IOException {
  66.         File file = new File(filepath);
  67.         file.createNewFile();
  68.     }
  69.     /**
  70.      * 复制文件
  71.      * @param sfpath    源文件路径
  72.      * @param dfpath    目标文件路径
  73.      * @throws IOException
  74.      */
  75.     public static void fileCopy(String sfpath,String dfpath) throws IOException {
  76.         File sourFile = new File(sfpath);
  77.         File desFile = new File(dfpath);
  78.         if(!sourFile.exists()) {
  79.             System.out.println("Source File Not Found");
  80.             System.exit(0);
  81.         }
  82.         desFile.createNewFile();  //新建文件
  83.         FileInputStream fin = new FileInputStream(sourFile);
  84.         FileOutputStream fout = new FileOutputStream(dfpath);
  85.         byte[] data = new byte[512];
  86.         int rs = -1;
  87.         while((rs=fin.read(data))>0) {
  88.             fout.write(data,0,rs);
  89.         }
  90.         fout.close();
  91.         fin.close();
  92.         System.out.println("文件复制成功!");
  93.     }
  94.     /**
  95.      * 复制源文件夹的内容到目标文件夹中,需要递归操作
  96.      * @param sourFiles     源文件夹下的子文件数组
  97.      * @param desFile       目标文件
  98.      * @throws IOException
  99.      */
  100.     public static void copyFolder(File[] sourFiles,File desFile) throws IOException {
  101.         if(!desFile.exists()) {
  102.             desFile.mkdir();
  103.         }
  104.         //复制操作
  105.         for(int i=0;i<sourFiles.length;i++) {
  106.             //如果文件是文件类型,则复制文件
  107.             if(sourFiles[i].isFile()) {
  108.                 FileInputStream fin = new FileInputStream(sourFiles[i]);
  109.                 FileOutputStream fout = new FileOutputStream(
  110.                         new File(desFile.getPath()+
  111.                         File.separator+sourFiles[i].getName())
  112.                 );
  113.                 int count = fin.available();
  114.                 byte[] data = new byte[count];
  115.                 if((fin.read(data) != -1)) {
  116.                     fout.write(data);
  117.                 }
  118.                 fout.close();
  119.                 fin.close();
  120.             }
  121.             //如果文件是文件夹类型,则新建文件夹,并递归操作
  122.             if(sourFiles[i].isDirectory()) {
  123.                 File des = new File(desFile.getPath()+
  124.                         File.separator+sourFiles[i].getName());
  125.                 des.mkdir();
  126.                 copyFolder(sourFiles[i].listFiles(),des);//递归调用方法本身
  127.             }
  128.         }
  129.     }
  130.     /**
  131.      * 删除文件
  132.      * @param filepath      文件路径
  133.      */
  134.     public static void deleteFile(String filepath) {
  135.         File file = new File(filepath);
  136.         if(!file.exists()) {
  137.             System.out.println("File Not Found");
  138.         }
  139.         boolean rs = file.delete();
  140.         if(rs) {
  141.             System.out.println("Delete Successfully");
  142.         } else {
  143.             System.out.println("Delete Failed");
  144.         }
  145.     }
  146.     public static void main(String args[]) throws IOException {
  147.         //获得文件信息测试
  148. //        String filepath = "/Users/liuyanzhao/Desktop/Test.txt";//默认路径为项目根目录
  149. //        getFileInfo(filepath);
  150.         //读取文件字符测试
  151. //        String filepath2 = "/Users/liuyanzhao/Desktop/Test.txt";
  152. //        StringBuilder sb = new StringBuilder();
  153. //        readFile(sb,filepath2);
  154. //        System.out.println(sb);
  155.         //写入字符测试
  156. //        StringBuilder sb2 = new StringBuilder("你好啊,我是言曌!");
  157. //        String filepath3 = "/Users/liuyanzhao/Desktop/Test.txt";
  158. //        writeFile(sb2,filepath3);
  159.         //新建文件测试
  160. //        String filepath4 = "/Users/liuyanzhao/Documents/temp/1.txt";
  161. //        createFile(filepath4);
  162.         //复制文件测试
  163. //        String sfilepath = "/Users/liuyanzhao/Desktop/uploads.zip";
  164. //        String dfilepath = "/Users/liuyanzhao/Desktop/uploads2.zip";
  165. //        fileCopy(sfilepath,dfilepath);
  166.         //复制文件夹测试
  167. //        String soufolderpath = "/Users/liuyanzhao/Desktop/uploads";
  168. //        String desfolderpath = "/Users/liuyanzhao/Documents/temp";
  169. //        File souFolder = new File(soufolderpath);
  170. //        File desFolder = new File(desfolderpath);
  171. //        copyFolder(souFolder.listFiles(),desFolder);
  172.         //删除文件测试
  173. //          String filepath5 = "/Users/liuyanzhao/Documents/temp/1.txt";
  174. //          deleteFile(filepath5);
  175.     }
  176. }

上面的代码只介绍了一些基本的简单的操作,以后遇到其他的再补充吧。最后再上一张结构图吧

Java中 IO 常用操作

相关文章: