网络文本的获取常用方法?
网络文本的获取常用方法
1。 [代码]test。properties 1 name=root 2 password=123456 2。 [文件] ReadProperties。java ~ 2KB 下载(63) view sourceprint? 01 import java。
io。InputStream; 02 import java。util。Enumeration; 03 import java。util。List; 04 import java。util。Properties; 05 import java。
util。ResourceBundle; 06 07 import org。junit。Test; 08 09 /** 10 * 获取*。properties配置文件中的内容 ,常见的两种方法: 11 * 12 * @author 冰雨凌風 13 * 14 */ 15 public class ReadProperties { 16 // 方法一 17 @Test 18 public void One() { 19 // 获得资源包 20 ResourceBundle bundle = ResourceBundle。
getBundle("test"); 21 // 通过资源包拿到所有的名称 22 Enumeration allName = bundle。getKeys(); 23 // 遍历 24 while (allName。
hasMoreElements()) { 25 // 获取每一个名称 26 String name = (String) allName。nextElement(); 27 // 利用已得到的名称通过资源包获得相应的值 28 String value = bundle。
getString(name); 29 System。out。println(name + "=" + value); 30 } 31 } 32 33 // 方法二 34 @Test 35 public void Two() throws Exception { 36 // 获得类加载器,然后把文件作为一个流获取 37 InputStream in = ReadProperties。
class。getClassLoader() 38 。getResourceAsStream("test。properties"); 39 // 创建Properties实例 40 Properties prop = new Properties(); 41 // 将Properties和流关联 42 prop。
load(in); 43 // 获取所有的名称 44 Enumeration allName = prop。propertyNames(); 45 // 遍历 46 while (allName。
hasMoreElements()) { 47 // 获得每一个名称 48 String name = (String) allName。nextElement(); 49 // 利用已得到的名称通过Properties对象获得相应的值 50 String value = (String) prop。
get(name); 51 System。out。println(name + "=" + value); 52 } 53 // 关闭资源 54 in。close(); 55 } 56 }。
答:“记事本”允许您以多种不同的格式创建或打开文件:ANSI、Unicode、big-endian Unicode 或 UTF-8。这些格式允许您使用具有不同字符集...详情>>