java 代码
- import java.io.*;
- /**
- * name:FileUtil.java
- *
- * desc: 本类是文件操作实用类,提供常用的文件操作方法。
- *
- *
- */
- public class FileUtil
- {
- /**
- * 方法根据指定参数名创建一个空文件
- * @param filePath 文件路径名
- * @throws IOException
- */
- public static File makeFile(String filePath)throws IOException, Exception
- {
- //参数无效
- if (filePath == null || filePath.trim().length() == 0)
- {
- throw new IllegalArgumentException("argument is not valid!");
- }
- File file = new File(filePath);
- file.getParentFile().mkdirs();
- file.createNewFile();
- //文件不存在
- if (!file.exists())
- {
- throw new Exception("create file '" + filePath + "' failed");
- }
- return file;
- }
- /**
- * 方法根据指定参数创建目录
- * @param path 路径名
- * @throws IOException
- * @throws Exception
- */
- public static void makeDir(String path)throws IOException, Exception
- {
- //参数无效
- if (path == null || path.trim().length() == 0)
- {
- throw new IllegalArgumentException("argument is not valid!");
- }
- File file = new File(path);
- //目录不存在时,创建目录
- if (!file.exists())
- {
- //创建目录失败时,延时1秒再创建
- if (!file.mkdirs())
- {
- try
- {
- final long sleepTime = 1000;
- Thread.sleep(sleepTime);
- file.mkdirs();
- }
- catch(InterruptedException ie)
- {
- file.mkdirs();
- }
- }
- }
- //目录创建失败
- if (!file.exists())
- {
- throw new Exception("create dir '" + path + "' failed!");
- }
- }
- /**
- * 方法执行文件的拷贝,不支持递归拷贝
- * @param src 源文件
- * @param dest 目的文件
- * @throws IOException
- * @throws Exception
- */
- public static void copy(File src, File dest)throws IOException, Exception
- {
- //参数无效
- if (src == null || dest == null)
- {
- throw new IllegalArgumentException("argument is not valid!");
- }
- //参数相等
- if (src.equals(dest))
- {
- throw new IllegalArgumentException("src file equals to dest file!");
- }
- org.apache.tools.ant.util.FileUtils fileUtils =
- org.apache.tools.ant.util.FileUtils.newFileUtils();
- fileUtils.copyFile(src, dest, null, true, false);
- }
- /**
- * 方法执行文件的拷贝,不支持递归拷贝
- * @param src 源文件path
- * @param dest 目的文件path
- * @throws Exception
- */
- public static void copy(String src, String dest)throws Exception
- {
- File srcFile = new File(src);
- File desFile = new File(dest);
- copy(srcFile, desFile);
- }
- /**
- * 复制整个文件夹的内容
- * @param oldPath 源目录
- * @param newPath 目的目录
- * @return
- */
- public static void copyFolder(String oldPath, String newPath)throws
- IOException, Exception
- {
- FileInputStream input = null;
- FileOutputStream output = null;
- // try
- // {
- new File(newPath).mkdirs(); // 如果文件夹不存在 则建立新文件夹
- File a = new File(oldPath);
- String[] file = a.list();
- File temp = null;
- for (int i = 0; i < file.length; i++)
- {
- try
- {
- if (oldPath.endsWith(File.separator))
- {
- temp = new File(oldPath + file[i]);
- }
- else
- {
- temp = new File(oldPath + File.separator + file[i]);
- }
- if (temp.isFile())
- {
- input = new FileInputStream(temp);
- output = new FileOutputStream(newPath + File.separator
- + temp.getName());
- byte[] b = new byte[1024 * 5];
- int len;
- while ((len = input.read(b)) != -1)
- {
- output.write(b, 0, len);
- }
- }
- // 如果是子文件夹
- if (temp.isDirectory())
- {
- copyFolder(oldPath + File.separator + file[i], newPath
- + File.separator + file[i]);
- }
- }
- catch (Exception e)
- {
- throw e;
- }
- finally
- {
- try
- {
- if (output != null)
- {
- output.flush();
- output.close();
- output = null;
- }
- if (input != null)
- {
- input.close();
- input = null;
- }
- }
- catch (IOException ioe)
- {
- // do nothing;
- }
- }
- }
- }
- // catch (Exception e)
- // {
- // throw e;
- // }
- // finally
- // {
- // try
- // {
- // output.flush();
- // output.close();
- // output = null;
- // input.close();
- // input = null;
- // }
- // catch(IOException ioe)
- // {
- // //do nothing;
- // }
- //
- // }
- // }
- /**
- * 文件打包方法,完成一个tar包的创建
- *
- * @param tarFile
- * tar包名
- * @param subFiles
- * 打包文件
- * @throws Exception
- */
- public static void tar(String tarFile, String[] subFiles)throws Exception
- {
- // 参数为空
- if (tarFile == null || subFiles == null)
- {
- throw new IllegalArgumentException("argument is null!");
- }
- //压缩包格式不正确
- if (!tarFile.toLowerCase().endsWith(".tar"))
- {
- throw new IllegalArgumentException("must be a tar file!");
- }
- String currentPath = System.getProperty("user.dir");
- String commandPath = currentPath + File.separator + "tar";
- StringBuffer cmdBuffer = new StringBuffer(commandPath + " -cvf " + tarFile);
- for (int i = 0; i < subFiles.length; i++)
- {
- cmdBuffer.append(" " + subFiles[i]);
- }
- Process p = null;
- try
- {
- p = Runtime.getRuntime().exec(cmdBuffer.toString());
- //压缩文件失败
- if (p.waitFor() != 0)
- {
- throw new IOException("tar -cvf " + tarFile + " failed!");
- }
- }
- //关闭输入输出流
- finally
- {
- if (p != null)
- {
- p.getErrorStream().close();
- p.getInputStream().close();
- p.getOutputStream().close();
- }
- }
- }
- /**
- * 文件打包方法,完成一个tar包的创建
- *
- * @param tarFile
- * tar包名
- * @param subFiles
- * 打包文件
- * @throws Exception
- */
- public static void tar(String commandPath, String tarFile, String[] subFiles)
- throws Exception
- {
- if (commandPath == null || tarFile == null || subFiles == null)
- {
- throw new IllegalArgumentException("argument is null!");
- }
- if (!tarFile.toLowerCase().endsWith(".tar"))
- {
- throw new IllegalArgumentException("must be a tar file!");
- }
- commandPath = commandPath + "/tar";
- StringBuffer cmdBuffer = new StringBuffer(commandPath + " -cvf " + tarFile);
- for (int i = 0; i < subFiles.length; i++)
- {
- cmdBuffer.append(" " + subFiles[i]);
- }
- Process p = null;
- System.out.println(" command : " + cmdBuffer.toString());
- try
- {
- p = Runtime.getRuntime().exec(cmdBuffer.toString());
- if (p.waitFor() != 0)
- {
- throw new IOException("tar -cvf " + tarFile + " failed!");
- }
- }
- finally
- {
- if (p != null)
- {
- InputStream is = p.getErrorStream();
- StringBuffer sb = new StringBuffer();
- byte[] buffer = new byte[1024];
- while (is.read(buffer, 0, buffer.length) != -1)
- {
- sb.append(new String(buffer));
- }
- System.out.println("error message: " + sb.toString());
- p.getErrorStream().close();
- p.getInputStream().close();
- p.getOutputStream().close();
- }
- }
- }
- public static void tar2(String tarFilePath, String baseDirPath)throws Exception
- {
- TarUtil tarUtil = new TarUtil();
- File tarFile = new File(tarFilePath);
- File baseDir = new File(baseDirPath);
- tarUtil.setDestFile(tarFile);
- tarUtil.setBasedir(baseDir);
- tarUtil.execute(null);
- }
- /**
- * 文件解包方法,完成tar包的解压
- * @param tarFile tar包路径
- * @param storePath 存放路径
- * @throws Exception
- * @deprecated replaced by untar2()
- */
- public static void untar(String tarFile, String storePath)throws Exception
- {
- //参数无效
- if (tarFile == null || tarFile.trim().length() == 0)
- {
- throw new IllegalArgumentException("argument is empty!");
- }
- //压缩包格式不正确
- if (!tarFile.toLowerCase().endsWith(".tar"))
- {
- throw new IllegalArgumentException("must be a tar file!");
- }
- String tarCommand = null;
- //默认存放路径
- if (storePath == null || storePath.trim().length() == 0)
- {
- tarCommand = "tar -xvf " + tarFile;
- }
- //制定存放路径
- else
- {
- tarCommand = "tar -C " + storePath + " -xvf " + tarFile;
- }
- String path = System.getProperty("java.library.path");
- System.out.println("path is : " + path);
- String currentPath = System.getProperty("user.dir");
- String pathSeparator = System.getProperty("path.separator");
- System.setProperty("java.library.path", currentPath + pathSeparator + path);
- String command = tarCommand;
- Process p = null;
- try
- {
- p = Runtime.getRuntime().exec(command);
- if (p.waitFor() != 0)
- {
- throw new IOException("tar -xvf " + tarFile + " failed!");
- }
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- //关闭输入输出流
- finally
- {
- if (p != null)
- {
- p.getErrorStream().close(); &n