zoukankan      html  css  js  c++  java
  • 获取指定位置的内容的util(heml等)

    package fs;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.util.Properties;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class IOReader {
    	private static final Logger log = LoggerFactory.getLogger(IOReader.class);
    	/**
    	 * 功能:Java读取txt文件的内容 步骤:1:先获得文件句柄 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
    	 * 3:读取到输入流后,需要读取生成字节流 4:一行一行的输出。readline()。 备注:需要考虑的是异常情况
    	 * 
    	 * @param filePath
    	 */
    	public static String readTxtFile(String filePath) {
    		StringBuffer fileInfo = new StringBuffer();
    		try {
    			String encoding = "UTF-8";
    			File file = new File(filePath);
    			if (file.isFile() && file.exists()) { // 判断文件是否存在
    				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
    				BufferedReader bufferedReader = new BufferedReader(read);
    				String lineTxt = null;
    				while ((lineTxt = bufferedReader.readLine()) != null) {
    					fileInfo.append(lineTxt);
    					// System.out.println(lineTxt);
    				}
    				read.close();
    			} else {
    				System.out.println("找不到指定的文件");
    			}
    		} catch (Exception e) {
    			System.out.println("读取文件内容出错");
    			e.printStackTrace();
    		}
    		return fileInfo.toString();
    
    	}
    
    	/**
    	 * 在maven项目中直接获取配置文件当前path然后并读取内容
    	 * 
    	 * @param fileName
    	 * @return
    	 */
    	public static String getDefaultPath_ReadLine(Class c, String fileName) {
    		String line = "";
    		ClassLoader classLoader = c.getClassLoader();
    		URL resource = classLoader.getResource(fileName);
    		String path = resource.getPath();
    		line = readTxtFile(path);
    		return line;
    	}
    
    	/**
    	 * 读取项目中的配置文件
    	 * 
    	 * @param fileName
    	 *            文件名
    	 * @param key
    	 *            对应参数key
    	 * @return
    	 */
    	public static String getPropertiesKeysValue(String fileName, String key) {
    		String value = "";
    		InputStream is = IOReader.class.getClassLoader().getResourceAsStream(fileName);
    		// 获取文件的位置
    		String filePath = IOReader.class.getClassLoader().getResource(fileName).getFile();
    		BufferedReader br = new BufferedReader(new InputStreamReader(is));
    		Properties props = new Properties();
    		try {
    			props.load(br);
    			value = props.getProperty(key);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return value;
    
    	}
    
    	public static void main(String[] args) {
    		String msg = getPropertiesKeysValue("redis.properties", "redis.host");
    		System.out.println(msg);
    	}
    }
    

      

  • 相关阅读:
    算法竞赛入门经典训练指南——UVA 11300 preading the Wealth
    hiho一下 第148周
    ajax总结及案例
    Spring事务
    Struts2拦截器介绍
    Struts2的拦截器----Dog实例
    Struts2文件的下载
    Struts2文件的上传
    Struts2类型转换
    Struts2 属性驱动、模型驱动、异常机制
  • 原文地址:https://www.cnblogs.com/yongyao/p/6799339.html
Copyright © 2011-2022 走看看