forked from srizzo/code2code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.java
More file actions
103 lines (70 loc) · 2.23 KB
/
FileUtils.java
File metadata and controls
103 lines (70 loc) · 2.23 KB
1
package code2code.utils;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.LinkedHashMap;import java.util.Map;import java.util.Map.Entry;import org.eclipse.core.resources.IContainer;import org.eclipse.core.resources.IFile;import org.eclipse.core.resources.IFolder;import org.eclipse.core.runtime.CoreException;public final class FileUtils { public static String read(InputStream inputStream) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); try{ StringBuilder result = new StringBuilder(); String line = reader.readLine(); while (line != null) { result.append(line); line = reader.readLine(); if(line != null){ result.append(System.getProperty("line.separator")); } } return result.toString(); }finally{ reader.close(); } } public static Map<String, String> propertiesInputStreamAsMap(InputStream propertiesInputStream) throws Exception { try { Properties paramsProperties = new Properties(); paramsProperties.load(propertiesInputStream); Map<String, String> params = new LinkedHashMap<String, String>(); for (Entry<Object, Object> entry : paramsProperties.entrySet()) { params.put((String) entry.getKey(), (String) entry.getValue()); } return params; } finally { propertiesInputStream.close(); } } public static void createFolderWithParents(IFolder folder) throws CoreException{ if(!folder.getParent().exists()){ IFolder parent = (IFolder) folder.getParent(); createFolderWithParents(parent); } if (!folder.exists()) { folder.create(false, true, null); } } public static void createParentFolders(IFile file) throws Exception { IContainer parent = file.getParent(); if(parent instanceof IFolder){ IFolder parentFolder = (IFolder) file.getParent(); if(!parentFolder.exists()){ FileUtils.createFolderWithParents(parentFolder); } } } public static Properties inputStreamAsProperties(InputStream inputStream) throws Exception { try { Properties properties = new Properties(); properties.load(inputStream); return properties; } finally { inputStream.close(); } }}