2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.List;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
38 * @author quanzhong@huawei.com
39 * @version NFVO 0.5 Sep 23, 2016
41 public class FileUtils {
43 public static final String UTF8 = "UTF-8";
45 public static final String GBK = "GBK";
47 private static Logger log = LoggerFactory.getLogger(FileUtils.class);
53 * get current classPath as str. and the trailing slash will be remove
59 public static String getClassPath(){
60 String path = ClassLoader.getSystemClassLoader().getResource("./").getPath();
61 if(path.endsWith("/")){
62 path = path.substring(0, path.length()-1);
67 * read data from the file
76 public static byte[] readFile(File file, String charsetName) throws IOException {
78 FileReader reader = new FileReader(file);
79 StringBuilder buffer = new StringBuilder();
80 char[] cbuf = new char[1024];
82 while((legth = reader.read(cbuf)) != -1) {
83 buffer.append(new String(cbuf, 0, legth));
86 return buffer.toString().getBytes(charsetName);
92 * write data as file to the filePath
100 public static int writeFile(byte[] data, String filePath) {
102 OutputStream out = new FileOutputStream(filePath);
103 out.write(data, 0, data.length);
106 } catch (Exception e) {
107 log.error("write file fail,filePath:"+filePath, e);
113 * List all files in directory
116 * @throws FileNotFoundException
118 public static List<File> listFiles(File file) throws FileNotFoundException{
119 List<File> list = new ArrayList<>();
124 * List all files in directory
127 * @throws FileNotFoundException
129 private static void list(File file, List<File> list) throws FileNotFoundException{
131 throw new FileNotFoundException(file.getAbsolutePath());
133 File[] files = file.listFiles();
134 if(files != null && files.length > 0 ){
147 * createDirs (cycle create)
153 public static void createDirs(File file){
154 if(file.getParentFile().exists()){
157 createDirs(file.getParentFile());
169 public static void mkDirs(String path){
170 File file = new File(path);
176 public static String getSuperUrl(String file) {
177 return new File(file).getParentFile().getAbsolutePath();
185 * @return file this application absolute url
187 public static String getAppAbsoluteUrl() {
189 // ��ȡ��class���ڵľ���·��
190 String file = UnCompressUtil.class.getClassLoader().getResource("/") == null ? null
191 : UnCompressUtil.class.getClassLoader().getResource("/").toString();
193 file = UnCompressUtil.class.getProtectionDomain().getCodeSource()
194 .getLocation().getFile().substring(1);
196 // ��class�ļ���war��ʱ������"zip:D:/ ..."����·��
197 if (file.startsWith("zip")) {
198 file = file.substring(4);
200 // ��class�ļ���class�ļ���ʱ������"file:/F:/ ..."����·��
201 } else if (file.startsWith("file")) {
202 file = file.substring(6);
204 // ��class�ļ���jar�ļ���ʱ������"jar:file:/F:/ ..."����·��
205 } else if (file.startsWith("jar")) {
206 file = file.substring(10);
210 return ("/" + file).replace("%20", " ");
212 return file.replace("%20", " ");
215 public static String getWEBClassAbsoluteUrl() {
217 return getAppAbsoluteUrl();
224 public static File newFloder(String floder) {
225 File file = new File(floder);
226 if (!file.exists()) {
234 public static void copyFile(String oldPath, String newPath, boolean flag)
237 File oldfile = new File(oldPath);
238 if (oldfile.exists()) { // �ļ�����ʱ
242 if (new File(newPath).exists() && flag == true) {
246 FileInputStream fis = new FileInputStream(oldPath); // ����ԭ�ļ�
247 FileOutputStream fos = new FileOutputStream(newPath);
248 byte[] buffer = new byte[1024];
249 while ((byteread = fis.read(buffer)) != -1) {
250 fos.write(buffer, 0, byteread);
255 throw new FileNotFoundException("the " + oldfile + " is not exits ");
261 * @param filePathAndName
262 * String exp c:/fqf.txt
267 public static boolean delFiles(String filePathAndName) {
269 boolean flag = false;
270 File myDelFile = new File(filePathAndName);
271 if (!myDelFile.exists())
273 if (myDelFile.isDirectory()) {
274 File[] fs = myDelFile.listFiles();
275 for (int i = 0; i < fs.length; i++) {
277 flag = fs[i].delete();
278 if (fs[i].isDirectory()) {
279 flag = delFiles(fs[i].getAbsolutePath());
280 flag = fs[i].delete();
284 flag = myDelFile.delete();
291 * @param filePathAndName
294 * @throws IOException
296 public static File newFile(String fileName) throws IOException {
297 File file = new File(fileName);
299 newFloder(file.getParentFile().toString());
301 if (!file.exists()) {
302 file.createNewFile();
308 * the file down all the hidden files
312 public static List<File> getFiles(String path) {
314 List<File> list = new ArrayList<File>();
315 File file = new File(path);
316 if (!file.exists()) {
321 File[] files = file.listFiles();
322 for (int i = 0; i < files.length; i++) {
323 if (files[i].isFile() && !files[i].isHidden()) {
326 if (files[i].isDirectory() && !files[i].isHidden()) {
327 List<File> list2 = getFiles(files[i].getPath());// use myself
335 * causes the os of this computer is Windows
339 public static boolean isWindows() {
341 String os = System.getProperty("os.name").toLowerCase();
343 return (os.indexOf("win") >= 0);
347 public static void copy(String oldfile, String newfile, boolean flag)
349 File oldf = new File(oldfile);
350 File newf = new File(newfile);
351 boolean oisd = (oldfile.endsWith("/") || oldfile.endsWith("\\"));
352 boolean nisd = (newfile.endsWith("/") || newfile.endsWith("\\"));
355 if (!oldf.exists()) {
356 throw new Exception("the from data is not exists ");
359 if (oldf.exists() && !newf.exists()) {
360 if (newfile.endsWith("/") || newfile.endsWith("\\")) {
366 // Ŀ�����ļ�����Դ���ļ���
367 if (oldf.exists() && oisd && !nisd) {
369 "the from data is directory,but the to data is a file");
372 if (!oisd && !nisd) {
373 copyFile(oldf.getAbsolutePath(), newf.getAbsolutePath(), flag);
375 // Դ�Ǹ��ļ��У�Ŀ�����ļ���
377 newFloder(newf.getAbsolutePath());
378 List<File> list = getFiles(oldf.getAbsolutePath());
379 for (int i = 0; i < list.size(); i++) {
380 copyFile(list.get(i).getAbsolutePath(), newf.getAbsolutePath()
381 + "/" + list.get(i).getName(), flag);
384 // Դ�Ǹ��ļ���Ŀ���Ǹ��ļ���
386 newFloder(newf.getAbsolutePath());
387 copyFile(oldf.getAbsolutePath(), newf.getAbsolutePath() + "/"
388 + oldf.getName(), flag);
394 * causes the os of this computer is mac
398 public static boolean isMac() {
400 String os = System.getProperty("os.name").toLowerCase();
402 return (os.indexOf("mac") >= 0);
407 * causes the os of this computer is unix
411 public static boolean isUnix() {
413 String os = System.getProperty("os.name").toLowerCase();
415 return (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0);
424 public static boolean isUsed(String file) throws Exception {
425 File f = new File(file);
427 throw new Exception("the file is not exists ..");
429 File f1 = new File(file + ".temp");
440 * Remove the file name's extension (only remove the last)
447 public static String getBaseFileName(File file){
448 if(file.getName().lastIndexOf(".") > 0){
449 return file.getName().substring(0,file.getName().lastIndexOf("."));
451 return file.getName();
457 * fix file path to linux seperate,and remove the head and end slash
464 public static String fixPath(String path){
465 String newPath = path;
469 newPath = newPath.replaceAll("\\\\", "/");
470 if(newPath.startsWith("/")){
471 newPath = newPath.substring(1, newPath.length());
473 if(newPath.endsWith("/")){
474 newPath = newPath.substring(0, newPath.length()-1);
480 * fix file path to linux seperate,and add the head and end slash
487 public static String getFriendlyPath(String path){
488 String newPath = path;
492 newPath = newPath.replaceAll("\\\\", "/");
493 if(!newPath.startsWith("/")){
494 newPath = "/"+newPath;
496 if(!newPath.endsWith("/")){
497 newPath = newPath + "/";