Java tutorial
/** * Copyright (c) 2014 Baidu, Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.baidu.rigel.biplatform.ma.file.serv.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; /** * ?? * * @author jiangyichao * */ public final class LocalFileOperationUtils { /** * RESULT */ public static final String RESULT = "result"; /** * FAILE */ public static final String FAIL = "fail"; /** * MSG */ public static final String MSG = "msg"; /** * SUCCESS */ public static final String SUCCESS = "success"; /** * */ private static final Logger LOG = Logger.getLogger(LocalFileOperationUtils.class); /** * LocalFileOperationUtils */ LocalFileOperationUtils() { } /** * * * @param filePath * ? */ public static boolean createFile(String filePath) { try { if (StringUtils.isBlank(filePath)) { return false; } File file = new File(filePath); // if (filePath.endsWith("/")) { return false; } int pos = filePath.lastIndexOf("/"); // ????? String dir = filePath.substring(0, pos); File dirFile = new File(dir); if (!dirFile.exists()) { dirFile.mkdirs(); } return file.createNewFile(); } catch (IOException e) { LOG.error(e.getMessage(), e); return false; } } /** * content * * @param file * ? * @param content * * @param code * ?? */ public static boolean writeFile(File file, byte[] content) { FileOutputStream fileOutputStream = null; try { if (file == null) { return false; } if (content == null) { return false; } if (content.length == 0) { return false; } fileOutputStream = new FileOutputStream(file); // fileOutputStream.write(content); fileOutputStream.flush(); return true; } catch (IOException e) { LOG.error(e.getMessage(), e); return false; } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { LOG.error(e.getMessage(), e); } } } } /** * ??? * * @param oldFile * * @param newFile * * @return * @throws IOException */ private static boolean copy(File oldFile, File newFile) { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(oldFile); fileOutputStream = new FileOutputStream(newFile); byte[] buf = new byte[1024]; int len = 0; // ?? while ((len = fileInputStream.read(buf)) != -1) { fileOutputStream.write(buf, 0, len); fileOutputStream.flush(); } return true; } catch (IOException e) { LOG.error(e.getMessage(), e); return false; } finally { try { if (fileInputStream != null) { fileInputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { LOG.error(e.getMessage(), e); } } } /** * ? * @param oldFilePath * @param newFilePath * @param replace * @return */ public static Map<String, Object> mv(String oldFilePath, String newFilePath, boolean replace) { File oldFile = new File(oldFilePath); File newFile = new File(newFilePath); Map<String, Object> result = preCheck(oldFile, newFile, replace); if (result != null) { return result; } return doAction(oldFile, newFile, true); } /** * ??? * @param newFilePath * @param replace * @param oldFile * @param newFile */ private static Map<String, Object> preCheck(File oldFile, File newFile, boolean replace) { Map<String, Object> result = new HashMap<String, Object>(); // ? if (!oldFile.exists()) { result.put(RESULT, FAIL); result.put(MSG, "?"); return result; } // ? if (newFile.exists() && !replace) { result.put(RESULT, FAIL); result.put(MSG, "?"); return result; } if (!newFile.exists()) { boolean rs = false; try { rs = newFile.createNewFile(); } catch (IOException e) { LOG.error(e.getMessage(), e); return null; } // ? if (!rs) { result.put(RESULT, FAIL); result.put(MSG, ""); return result; } } return null; } /** * ?? * * @param oldFilePath * * @param newFilePath * * @param replace * ? * @return */ public static Map<String, Object> copy(String oldFilePath, String newFilePath, boolean replace) { File oldFile = new File(oldFilePath); File newFile = new File(newFilePath); Map<String, Object> result = preCheck(oldFile, newFile, replace); if (result != null) { return result; } // ? return doAction(oldFile, newFile, false); } /** * * ? * @param oldFile * * @param newFile * * @param delete * ? * @return */ private static Map<String, Object> doAction(File oldFile, File newFile, boolean delete) { Map<String, Object> result = new HashMap<String, Object>(); // ?? if (!copy(oldFile, newFile)) { result.put(RESULT, FAIL); result.put(MSG, "?"); return result; } // ??? if (delete) { if (!oldFile.delete()) { result.put(RESULT, FAIL); result.put(MSG, ""); return result; } } result.put(RESULT, "success"); result.put(MSG, "?/?"); return result; } }