Issue-id: OCS-9
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / util / FileUtil.java
1 /**
2  * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.msb.wrapper.util;
17
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24
25 public final class FileUtil {
26
27     /**
28      * Read all the files under a folder
29      */
30     public static File[] readFileFolder(String filepath) throws FileNotFoundException, IOException {
31         File file = new File(filepath);
32         if (file.isDirectory()) {
33             File[] filelist = file.listFiles();
34             return filelist;
35         }
36        
37         return null;
38     }
39
40     public static String readFile(String Path) throws IOException{
41         BufferedReader reader = null;
42         String fileContent = "";
43         try {
44             FileInputStream fileInputStream = new FileInputStream(Path);
45             InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
46             reader = new BufferedReader(inputStreamReader);
47             String tempString = null;
48             while ((tempString = reader.readLine()) != null) {
49                 fileContent += tempString;
50             }
51             reader.close();
52         } catch (IOException e) {
53             throw e;
54         } finally {
55             if (reader != null) {
56                 try {
57                     reader.close();
58                 } catch (IOException e) {
59                     throw e;
60                 }
61             }
62         }
63         return fileContent;
64     }
65 }