Added some tools for DCAE CBS removal
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / FileUtils.java
1 /**
2  * Copyright 2021 ZTE Corporation.
3  * <p>
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  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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
17 package org.onap.holmes.common.utils;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import java.io.BufferedReader;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
26
27 public class FileUtils {
28
29     final static private Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
30
31     static public String readTextFile(String path) {
32         try {
33             BufferedReader br = new BufferedReader(new FileReader(path));
34             StringBuilder sb = new StringBuilder();
35             String line;
36             while ((line = br.readLine()) != null) {
37                 sb.append(line);
38             }
39             return sb.toString();
40         } catch (FileNotFoundException e) {
41             LOGGER.warn("No file found: {}", path);
42         } catch (IOException e) {
43             LOGGER.error(String.format("Failed to read file contents from '%s'.", path), e);
44         } catch (Exception e) {
45             LOGGER.warn("Unknown exception occurred!", e);
46         }
47         return null;
48     }
49 }