[Java] Java一次创建多级目录 →→→→→进入此内容的聊天室

来自 , 2019-04-01, 写在 Java, 查看 146 次.
URL http://www.code666.cn/view/4511dccb
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.StringTokenizer;
  7.  
  8. public class FileUtil {
  9.  
  10.         public static  void CreateFolders(final String folders) {
  11.                 StringTokenizer st = new StringTokenizer(folders, File.separator);
  12.                 StringBuilder sb = new StringBuilder();
  13.                 String osname = System.getProperty("os.name");
  14.                 if (osname.compareToIgnoreCase("linux") == 0)
  15.                         sb.append(File.separator);
  16.  
  17.                 while (st.hasMoreTokens()) {
  18.                         sb.append(st.nextToken());
  19.                         File file = new File(sb.toString());
  20.                         if (!file.exists())
  21.                                 file.mkdir();
  22.                         sb.append(File.separator);
  23.                 }
  24.  
  25.         }
  26.  
  27.        
  28.         public static  boolean CopyFile(final String src, final String dest) {
  29.                 File srcFile = new File(src);
  30.                 File destFile = new File(dest);
  31.                 try {
  32.                         if(!destFile.exists())
  33.                                 destFile.createNewFile();
  34.                        
  35.                         FileInputStream fin = new FileInputStream(srcFile);
  36.                         FileOutputStream fout = new FileOutputStream(destFile);
  37.                         int n = 0;
  38.                         byte[] buf = new byte[4096];
  39.                         while ( (n = fin.read(buf)) != -1 ) {
  40.                                 fout.write(buf, 0, n);
  41.                         }
  42.                        
  43.                         fout.close();
  44.                         fin.close();
  45.  
  46.                 } catch (FileNotFoundException e) {
  47.                         e.printStackTrace();
  48.                         return false;
  49.                 } catch (IOException e) {
  50.                         e.printStackTrace();
  51.                         return false;
  52.                 }
  53.  
  54.                 return true;
  55.         }
  56.  
  57. }
  58.  
  59. //java/5513

回复 "Java一次创建多级目录"

这儿你可以回复上面这条便签

captcha