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 {
79 try( FileReader reader = new FileReader(file)){
80 StringBuilder buffer = new StringBuilder();
81 char[] cbuf = new char[1024];
83 while((legth = reader.read(cbuf)) != -1) {
84 buffer.append(new String(cbuf, 0, legth));
87 return buffer.toString().getBytes(charsetName);
88 }catch(IOException e){
89 log.error("read file", e);
97 * write data as file to the filePath
105 public static int writeFile(byte[] data, String filePath) {
107 OutputStream out = new FileOutputStream(filePath)){
108 out.write(data, 0, data.length);
111 } catch (Exception e) {
112 log.error("write file fail,filePath:"+filePath, e);
118 * List all files in directory
121 * @throws FileNotFoundException
123 public static List<File> listFiles(File file) throws FileNotFoundException{
124 List<File> list = new ArrayList<>();
129 * List all files in directory
132 * @throws FileNotFoundException
134 private static void list(File file, List<File> list) throws FileNotFoundException{
136 throw new FileNotFoundException(file.getAbsolutePath());
138 File[] files = file.listFiles();
139 if(files != null && files.length > 0 ){
152 * createDirs (cycle create)
158 public static void createDirs(File file){
159 if(file.getParentFile().exists()){
162 createDirs(file.getParentFile());
174 public static void mkDirs(String path){
175 File file = new File(path);
181 public static String getSuperUrl(String file) {
182 return new File(file).getParentFile().getAbsolutePath();
190 * @return file this application absolute url
192 public static String getAppAbsoluteUrl() {
194 // ��ȡ��class���ڵľ���·��
195 String file = UnCompressUtil.class.getClassLoader().getResource("/") == null ? null
196 : UnCompressUtil.class.getClassLoader().getResource("/").toString();
198 file = UnCompressUtil.class.getProtectionDomain().getCodeSource()
199 .getLocation().getFile().substring(1);
201 // ��class�ļ���war��ʱ������"zip:D:/ ..."����·��
202 if (file.startsWith("zip")) {
203 file = file.substring(4);
205 // ��class�ļ���class�ļ���ʱ������"file:/F:/ ..."����·��
206 } else if (file.startsWith("file")) {
207 file = file.substring(6);
209 // ��class�ļ���jar�ļ���ʱ������"jar:file:/F:/ ..."����·��
210 } else if (file.startsWith("jar")) {
211 file = file.substring(10);
215 return ("/" + file).replace("%20", " ");
217 return file.replace("%20", " ");
220 public static String getWEBClassAbsoluteUrl() {
222 return getAppAbsoluteUrl();
229 public static File newFloder(String floder) {
230 File file = new File(floder);
231 if (!file.exists()) {
239 public static void copyFile(String oldPath, String newPath, boolean flag)
242 File oldfile = new File(oldPath);
243 if (oldfile.exists()) {
247 if (new File(newPath).exists() && flag) {
251 try(FileInputStream fis = new FileInputStream(oldPath);
252 FileOutputStream fos = new FileOutputStream(newPath)){
253 byte[] buffer = new byte[1024];
254 while ((byteread = fis.read(buffer)) != -1) {
255 fos.write(buffer, 0, byteread);
259 throw new FileNotFoundException("the " + oldfile + " is not exits ");
265 * @param filePathAndName
266 * String exp c:/fqf.txt
271 public static boolean delFiles(String filePathAndName) {
273 boolean flag = false;
274 File myDelFile = new File(filePathAndName);
275 if (!myDelFile.exists())
277 if (myDelFile.isDirectory()) {
278 File[] fs = myDelFile.listFiles();
279 for (int i = 0; i < fs.length; i++) {//no need to assign
282 if (fs[i].isDirectory()) {
283 delFiles(fs[i].getAbsolutePath());
288 flag = myDelFile.delete();
295 * @param filePathAndName
298 * @throws IOException
300 public static File newFile(String fileName) throws IOException {
301 File file = new File(fileName);
303 newFloder(file.getParentFile().toString());
305 if (!file.exists()) {
306 file.createNewFile();
312 * the file down all the hidden files
316 public static List<File> getFiles(String path) {
318 List<File> list = new ArrayList<>();
319 File file = new File(path);
320 if (!file.exists()) {
325 File[] files = file.listFiles();
326 for (int i = 0; i < files.length; i++) {
327 if (files[i].isFile() && !files[i].isHidden()) {
330 if (files[i].isDirectory() && !files[i].isHidden()) {
331 List<File> list2 = getFiles(files[i].getPath());// use myself
339 * causes the os of this computer is Windows
343 public static boolean isWindows() {
345 String os = System.getProperty("os.name").toLowerCase();
347 return (os.indexOf("win") >= 0);
351 public static void copy(String oldfile, String newfile, boolean flag)
353 File oldf = new File(oldfile);
354 File newf = new File(newfile);
355 boolean oisd = (oldfile.endsWith("/") || oldfile.endsWith("\\"));
356 boolean nisd = (newfile.endsWith("/") || newfile.endsWith("\\"));
358 if (!oldf.exists()) {
359 throw new FileNotFoundException("the from data is not exists");
361 if (oldf.exists() && !newf.exists()) {
362 if (newfile.endsWith("/") || newfile.endsWith("\\")) {
368 if (oldf.exists() && oisd && !nisd) {
369 throw new FileNotFoundException("the from data is directory,but the to data is a file");
371 if (!oisd && !nisd) {
372 copyFile(oldf.getAbsolutePath(), newf.getAbsolutePath(), flag);
375 newFloder(newf.getAbsolutePath());
376 List<File> list = getFiles(oldf.getAbsolutePath());
377 for (int i = 0; i < list.size(); i++) {
378 copyFile(list.get(i).getAbsolutePath(), newf.getAbsolutePath()
379 + "/" + list.get(i).getName(), flag);
383 newFloder(newf.getAbsolutePath());
384 copyFile(oldf.getAbsolutePath(), newf.getAbsolutePath() + "/"
385 + oldf.getName(), flag);
391 * causes the os of this computer is mac
395 public static boolean isMac() {
397 String os = System.getProperty("os.name").toLowerCase();
399 return (os.indexOf("mac") >= 0);
404 * causes the os of this computer is unix
408 public static boolean isUnix() {
410 String os = System.getProperty("os.name").toLowerCase();
412 return (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0);
421 public static boolean isUsed(String file) throws IOException {
422 File f = new File(file);
424 throw new FileNotFoundException("the file is not exists ..");
426 File f1 = new File(file + ".temp");
437 * Remove the file name's extension (only remove the last)
444 public static String getBaseFileName(File file){
445 if(file.getName().lastIndexOf(".") > 0){
446 return file.getName().substring(0,file.getName().lastIndexOf("."));
448 return file.getName();
454 * fix file path to linux seperate,and remove the head and end slash
461 public static String fixPath(String path){
462 String newPath = path;
466 newPath = newPath.replaceAll("\\\\", "/");
467 if(newPath.startsWith("/")){
468 newPath = newPath.substring(1, newPath.length());
470 if(newPath.endsWith("/")){
471 newPath = newPath.substring(0, newPath.length()-1);
477 * fix file path to linux seperate,and add the head and end slash
484 public static String getFriendlyPath(String path){
485 String newPath = path;
489 newPath = newPath.replaceAll("\\\\", "/");
490 if(!newPath.startsWith("/")){
491 newPath = "/"+newPath;
493 if(!newPath.endsWith("/")){
494 newPath = newPath + "/";