java 读取文件

本文介绍了一个Java实用工具类,该类提供了一系列方法用于文件读取和资源加载。包括从类路径获取文件输入流、读取字符串内容、读取文件列表等,并展示了如何使用这些方法来处理不同类型的文件和编码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值