2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.utilities;
24 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.nio.file.Files;
32 * The Class TextFileUtils is class that provides useful functions for handling text files. Functions to read and wrtie
33 * text files to strings and strings are provided.
35 * @author Liam Fallon (liam.fallon@ericsson.com)
37 public abstract class TextFileUtils {
38 private static final int READER_CHAR_BUFFER_SIZE_4096 = 4096;
40 private TextFileUtils() {
41 // This class cannot be initialized
45 * Method to return the contents of a text file as a string.
47 * @param textFilePath The path to the file as a string
48 * @return A string containing the contents of the file
49 * @throws IOException on errors reading text from the file
51 public static String getTextFileAsString(final String textFilePath) throws IOException {
52 final File textFile = new File(textFilePath);
53 return new String(Files.readAllBytes(textFile.toPath()));
57 * Method to write contents of a string to a text file.
59 * @param outString The string to write
60 * @param textFilePath The path to the file as a string
61 * @throws IOException on errors reading text from the file
63 public static void putStringAsTextFile(final String outString, final String textFilePath) throws IOException {
64 final File textFile = new File(textFilePath);
65 if (!textFile.getParentFile().exists()) {
66 textFile.getParentFile().mkdirs();
69 putStringAsFile(outString, textFile);
73 * Method to write contents of a string to a text file.
75 * @param outString The string to write
76 * @param textFile The file to write the string to
77 * @throws IOException on errors reading text from the file
79 public static void putStringAsFile(final String outString, final File textFile) throws IOException {
80 Files.write(textFile.toPath(), outString.getBytes());
84 * Method to return the contents of a text steam as a string.
86 * @param textStream The stream
87 * @return A string containing the output of the stream as text
88 * @throws IOException on errors reading text from the file
90 public static String getStreamAsString(final InputStream textStream) throws IOException {
91 return getReaderAsString(new BufferedReader(new InputStreamReader(textStream)));
95 * Method to return the contents of a reader steam as a string. This closes the reader after use
97 * @param textReader The reader
98 * @return A string containing the output of the reader as text
99 * @throws IOException on errors reading text from the file
101 public static String getReaderAsString(final BufferedReader textReader) throws IOException {
103 final StringBuilder builder = new StringBuilder();
105 final char[] chars = new char[READER_CHAR_BUFFER_SIZE_4096];
107 charsRead = textReader.read(chars, 0, chars.length);
109 builder.append(chars, 0, charsRead);
112 while (charsRead > 0);
113 return builder.toString();