Updated slf4j to 2.0.9
[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     final static private String LINE_SEPARATOR = System.getProperty ("line.separator");
31
32     static public String readTextFile(String path) {
33         try {
34             BufferedReader br = new BufferedReader(new FileReader(path));
35             StringBuilder sb = new StringBuilder();
36             String line;
37             while ((line = br.readLine()) != null) {
38                 sb.append(line).append(LINE_SEPARATOR);
39             }
40             return sb.substring(0, sb.length() - 1);
41         } catch (FileNotFoundException e) {
42             LOGGER.warn("No file found: {}", path);
43         } catch (IOException e) {
44             LOGGER.error(String.format("Failed to read file contents from '%s'.", path), e);
45         } catch (Exception e) {
46             LOGGER.warn("Unknown exception occurred!", e);
47         }
48         return null;
49     }
50 }