FileUtil.java |
/* Copyright (C) 2009 CSE,IIT Bombay http://www.cse.iitb.ac.in This file is part of the ConStore open source storage facility for concept-nets. ConStore is free software and distributed under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License; you can copy, distribute and transmit the work with the work attribution in the manner specified by the author or licensor. You may not use this work for commercial purposes and may not alter, transform, or build upon this work. Please refer the legal code of the license, available at http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode ConStore is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ package iitb.con.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * File util to retrieve file meta information * * @author prathabk * */ public class FileUtil { /** File separator */ public static final String FILE_SEPARATOR = System.getProperty("file.separator"); /** File data path */ public static final String FILE_DATA_PATH = new File("").getAbsolutePath() + FILE_SEPARATOR; /** * Return <tt>true</tt> if file exists in the system * @param name file name * @return <tt>true</tt> if file exists */ public static boolean isFileExists(String name) { File file = new File(name); return file.exists(); } /** * Create the file if it does not exists * @param name file name * @return {@link Result} */ public static Result createFile(String name) { try { File file = new File(name); if(!file.exists()) return new Result(file.createNewFile()); else return new Result(false, "File '" + name + "' already exists"); }catch(IOException ioe) { return new Result(false, "Error in the file creation", ioe.getCause()); } } /** * Deletes the file if it exists * @param name file name * @return <tt>true</tt> on success */ public static boolean deleteFile(String name) { File file = new File(name); if(file.delete()) return true; else return false; } /** * Gets the file name from the filepath string <p> * Eg: Returns "filename.txt" for the string "/usr/test/filename.txt" * @param name filepath string * @return file name */ public static String getName(String name) { if(name.endsWith(FILE_SEPARATOR)) name = name.substring(0, name.length() - 1); String lastName = name.substring(name.lastIndexOf(FILE_SEPARATOR) + 1, name.length()); return lastName; } /** * Creates the directory(-ies) for the given filepath string * @param name filepath string * @return <tt>true</tt> on success */ public static boolean createDirForFilePathString(String name) { File file = new File(name); if(!file.exists()) { String path = name.substring(0,name.lastIndexOf(FILE_SEPARATOR)); return createDir(path); } else { return true; } } /** * Creates directory for the given path string * @param path path string * @return <tt>true</tt> on success */ public static boolean createDir(String path) { File file = new File(path); return file.mkdirs(); } /** * Copy the file to the specified destination * @param src file source name * @param dst file destination name * @throws IOException on failure */ public static void copy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } /** * Renames and move the file to the specified destination * @param oldFullPathName old path name along with file name * @param newFullPathName new path name along with file name * @return <tt>true</tt> on success */ public static boolean renameMove(String oldFullPathName, String newFullPathName) { File file = new File(oldFullPathName); File dest = new File(newFullPathName); return file.renameTo(dest); } /** * Deletes the entire directory structure of the specified directory * @param dir directory * @return <tt>true</tt> on success */ public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // The directory is now empty so delete it return dir.delete(); } /** * Deletes the specified directory * @param name directory name * @return <tt>true</tt> on success */ public static boolean deleteDir(String name) { File dir = new File(name); return deleteDir(dir); } }