d1f73cf4587d21acf96f818472d6f6529fec66f8
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / java / org / openo / msb / wrapper / util / FileUtil.java
1 /**\r
2  * Copyright 2016 ZTE, Inc. and others.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.openo.msb.wrapper.util;\r
18 \r
19 import java.io.BufferedReader;\r
20 import java.io.File;\r
21 import java.io.FileInputStream;\r
22 import java.io.FileNotFoundException;\r
23 import java.io.IOException;\r
24 import java.io.InputStreamReader;\r
25 \r
26 public final class FileUtil {\r
27 \r
28     /**\r
29      * Read all the files under a folder\r
30      */\r
31     public static File[] readFileFolder(String filepath) throws FileNotFoundException, IOException {\r
32         File file = new File(filepath);\r
33         if (file.isDirectory()) {\r
34             File[] filelist = file.listFiles();\r
35             return filelist;\r
36         }\r
37        \r
38         return null;\r
39     }\r
40 \r
41     public static String readFile(String Path) throws IOException{\r
42         BufferedReader reader = null;\r
43         String fileContent = "";\r
44         try {\r
45             FileInputStream fileInputStream = new FileInputStream(Path);\r
46             InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");\r
47             reader = new BufferedReader(inputStreamReader);\r
48             String tempString = null;\r
49             while ((tempString = reader.readLine()) != null) {\r
50                 fileContent += tempString;\r
51             }\r
52             reader.close();\r
53         } catch (IOException e) {\r
54             throw e;\r
55         } finally {\r
56             if (reader != null) {\r
57                 try {\r
58                     reader.close();\r
59                 } catch (IOException e) {\r
60                     throw e;\r
61                 }\r
62             }\r
63         }\r
64         return fileContent;\r
65     }\r
66 }\r