Java读取txt文件中的数据赋给String变量方法-创新互联

实例如下所示:

成都创新互联主要从事成都网站建设、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务兴化,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
public class MainActivity {
	
	private static final String fileName = "D:/Tao/MyEclipseWorkspace/resources/weather.txt";
	
	public static void main(String[] args) {
		
		//读取文件
		BufferedReader br = null;
		StringBuffer sb = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"GBK")); //这里可以控制编码
			sb = new StringBuffer();
			String line = null;
			while((line = br.readLine()) != null) {
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (Exception e) {
				e.printStackTrace();
			}	
		}
		
		String data = new String(sb); //StringBuffer ==> String
		System.out.println("数据为==> " + data);
			
	}
	
}

网页题目:Java读取txt文件中的数据赋给String变量方法-创新互联
链接地址:http://myzitong.com/article/coohdo.html