import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
public class ReaderFileUtils {
public static InputStream getFile(String file)
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream is = loader.getResourceAsStream(file);
return is;
}
public static String getString(String file) throws IOException
{
StringBuilder sb = new StringBuilder();
InputStream is = getFile(file);
List<String> ls = IOUtils.readLines(is);
for(String str : ls)
sb.append(str);
return sb.toString();
}
public static String getString(String...strs) throws IOException
{
StringBuilder sb = new StringBuilder();
InputStream is = getFile(strs[0]);
List<String> ls = IOUtils.readLines(is,strs[1]);
for(String str : ls)
sb.append(str);
return sb.toString();
}
public static List<String> getFile(File file,String encoding) throws IOException
{
List<String> ls = FileUtils.readLines(file, encoding);
return ls;
}
public static LineIterator getFile(File file,String encoding,String flag) throws IOException
{
LineIterator li = FileUtils.lineIterator(file, encoding);
return li;
}
public static Map getResourceBundleProperties(String baseName)
{
Map<String,String> map = new HashMap<String,String>();
ResourceBundle rb = ResourceBundle.getBundle(baseName);
for(Iterator it = rb.keySet().iterator();it.hasNext();){
String key = (String) it.next();
String value = rb.getString(key);
System.out.println(key + " = "+value);
map.put(key, value);
}
return map;
}
public static Map getResourceBundleEnum(String baseName)
{
Map<String,String> map = new HashMap<String,String>();
ResourceBundle rb = ResourceBundle.getBundle(baseName);
Enumeration en = rb.getKeys();
while(en.hasMoreElements()){
String key = (String) en.nextElement();
String value = rb.getString(key);
System.out.println(key + " = "+value);
map.put(key, value);
}
return map;
}
public static Map getResourceBundleInputStream(String baseName) throws IOException
{
Map<String,String> map = new HashMap<String,String>();
ResourceBundle rb = new PropertyResourceBundle(getFile(baseName));
for(Iterator it = rb.keySet().iterator();it.hasNext();){
String key = (String) it.next();
String value = rb.getString(key);
System.out.println(key + " = "+value);
map.put(key, value);
}
return map;
}
}