2 * Copyright 2021 ZTE Corporation.
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.holmes.common.utils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import java.io.BufferedReader;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
27 public class FileUtils {
29 final static private Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
30 final static private String LINE_SEPARATOR = System.getProperty ("line.separator");
32 static public String readTextFile(String path) {
34 BufferedReader br = new BufferedReader(new FileReader(path));
35 StringBuilder sb = new StringBuilder();
37 while ((line = br.readLine()) != null) {
38 sb.append(line).append(LINE_SEPARATOR);
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);