2 * Copyright (c) 2018 ZTE Corporation.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the Apache License, Version 2.0
5 * and the Eclipse Public License v1.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * ZTE - initial API and implementation and/or initial documentation
13 package org.onap.sdc.workflowdesigner.utils;
15 import java.io.BufferedReader;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.io.OutputStream;
22 import java.io.Reader;
23 import java.io.Writer;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26 import java.util.ArrayList;
27 import java.util.List;
29 import org.apache.commons.io.IOUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * common utility class.
37 public class FileCommonUtils {
38 private static final Logger logger = LoggerFactory.getLogger(FileCommonUtils.class);
43 public static void closeInputStream(InputStream ins) {
47 } catch (IOException e) {
48 logger.info("Close InputStream failed.", e);
58 public static void closeOutputStream(OutputStream os) {
62 } catch (IOException e) {
63 logger.info("Close OutputStream failed.", e);
72 public static void closeReader(Reader reader) {
76 } catch (IOException e) {
77 logger.info("Close Reader failed.", e);
87 public static void closeWriter(Writer writer) {
91 } catch (IOException e) {
92 logger.info("Close Writer failed.", e);
102 * @throws IOException
104 public static String[] readLines(InputStream ins) throws IOException {
105 InputStreamReader insReader = new InputStreamReader(ins);
106 BufferedReader reader = new BufferedReader(insReader);
108 List<String> lineList = new ArrayList<>();
111 while ((line = reader.readLine()) != null) {
116 closeReader(insReader);
119 return lineList.toArray(new String[0]);
127 * @throws IOException
129 public static String readString(InputStream ins) throws IOException {
130 return IOUtils.toString(ins, "UTF-8");
138 * @throws IOException
140 public static String readString(String filePath) throws IOException {
141 InputStream ins = null;
143 ins = Files.newInputStream(Paths.get(filePath));
144 return readString(ins);
146 closeInputStream(ins);
157 * @throws IOException
159 public static String saveFile(InputStream ins, String path, String fileName) throws IOException {
160 File tmpPath = new File(path);
161 if (!tmpPath.exists()) {
165 File file = new File(path + File.separator + fileName);
166 OutputStream os = null;
169 byte[] bytes = new byte[1024];
170 os = new FileOutputStream(file, false);
171 while ((read = ins.read(bytes)) != -1) {
172 os.write(bytes, 0, read);
175 return file.getAbsolutePath();
177 closeOutputStream(os);
187 * @throws IOException
189 public static void writetoAbsoluteFile(String path, String fileName, String content)
191 writetoAbsoluteFile(path, fileName, content, FileCommonConstants.DEFAULT_CHARSET_NAME);
201 * @throws IOException
203 public static void writetoAbsoluteFile(String path, String fileName, String content,
204 String charsetName) throws IOException {
205 write(path, fileName, content, charsetName);
213 * @throws IOException
215 public static void write(String fileName, String s) throws IOException {
216 write(".", fileName, s, FileCommonConstants.DEFAULT_CHARSET_NAME);
227 * @throws IOException
229 public static void write(String path, String fileName, String s, String charsetName)
231 File tmpPath = new File(path);
232 if (!tmpPath.exists()) {
236 String absolutePath = path + File.separator + fileName;
237 FileOutputStream out = null;
239 out = new FileOutputStream(absolutePath);
240 out.write(s.getBytes(charsetName));
243 closeOutputStream(out);
253 * @throws IOException
255 public static void write(String fileName, String s, String charsetName) throws IOException {
256 write(".", fileName, s, charsetName);
264 * @throws IOException
266 public static void write(String fileName, String[] ss) throws IOException {
267 write(fileName, ss, FileCommonConstants.DEFAULT_CHARSET_NAME);
276 * @throws IOException
278 public static void write(String fileName, String[] ss, String charsetName) throws IOException {
279 StringBuilder sb = new StringBuilder();
280 for (int i = 0; i < ss.length; i++) {
281 sb.append(ss[i]).append(System.lineSeparator());
284 write(fileName, sb.toString(), charsetName);