IO文件夹拷贝(文件内含有文件和文件夹)

 1 /**
 2      * 文件夹拷贝(文件内含有文件和文件夹)
 3      * 
 4      * @param src
 5      * @param des
 6      */
 7     private static void copy(String src, String des) {
 8         File file1 = new File(src);
 9         File[] fs = file1.listFiles();
10         File file2 = new File(des);
11         if (!file2.exists()) {
12             file2.mkdirs();
13             for (File f : fs) {
14                 if (f.isFile()) {
15                     fileCopy(f.getPath(), des + "\\" + f.getName()); // 调用文件拷贝的方法
16                 } else if (f.isDirectory()) {
17                     copy(f.getPath(), des + "\\" + f.getName());
18                 }
19             }
20         }
21     }
22 
23     /**
24      * 文件拷贝的方法
25      */
26     private static void fileCopy(String src, String des) {
27         BufferedReader br = null;
28         PrintStream ps = null;
29         try {
30             br = new BufferedReader(new InputStreamReader(new FileInputStream(src)));
31             ps = new PrintStream(new FileOutputStream(des));
32             String s = null;
33             while ((s = br.readLine()) != null) {
34                 ps.println(s);
35                 ps.flush();
36             }
37 
38         } catch (FileNotFoundException e) {
39             e.printStackTrace();
40         } catch (IOException e) {
41             e.printStackTrace();
42         } finally {
43             try {
44                 if (br != null)
45                     br.close();
46                 if (ps != null)
47                     ps.close();
48             } catch (IOException e) {
49                 e.printStackTrace();
50             }
51         }
52     }

 3.  读取文件内容

/**
     * 读取文件信息
     * @param src  文件路径
     * @return String
     */
    public static String readCacert(String src) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(src))));
            String CacertStr = null;
            while (null != (CacertStr = br.readLine())) {
                sb.append(CacertStr);
                sb.append("\n");
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

 4. 写入文件

    /**
     * 将String型写入文件中
     * @param serverCertificate 证书字符串
     * @param path  写入路径
     */
    public static void writePem(String src, String path) {
        
        File file = new File(path);
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            bw.write(src);
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 5.  创建文件

/**
     * 创建文件
     */
    public static File createFile(String path, String fileName) {
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();// 创建目录
        }
        File file = new File(path, fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

 6.  如果不存在就创建新文件, 如果存在就覆盖

/**
     * 将String型写入文件中
     * 
     * @param serverCertificate
     *            证书字符串
     * @param path
     *            写入路径
     */
    public static void writeFile(String src, String path, String fileName) {
        File file = createFile(path, fileName);
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
            bw.write(src);
            bw.flush();
            bw.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 创建文件
     */
    public static File createFile(String path, String fileName) {
        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();// 创建目录
        }
        File file = new File(path, fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

7.  追加字符串到指定文件中

/**
     * 追加字符串到指定文件中
     * @param filePath
     * @param src
     */
    public static void appendStrToFile(String src, String filePath) {
        try {  
              FileWriter fw = new FileWriter(filePath, true);  
              BufferedWriter bw = new BufferedWriter(fw);  
              Date d = new Date();
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              bw.append(sdf.format(d)+"##");  
              bw.write(src);
              bw.write("\n");
              bw.close();  
              fw.close();  
          } catch (Exception e) {  
              e.printStackTrace();  
          }
    }

8. 读取文件信息

/**
     * 读取文件信息
     * @param src
     * @return String
     */
    public static String readFile(String path) {
        StringBuilder sb = new StringBuilder();
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String CacertStr = null;
            while (null != (CacertStr = br.readLine())) {
                sb.append(CacertStr);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

 

posted @ 2017-12-13 17:37  林**  阅读(484)  评论(0编辑  收藏  举报