Java读取txt文件中的数据赋给String变量方法
程序员文章站
2023-12-21 11:06:16
实例如下所示:
public class mainactivity {
private static final string filename = "...
实例如下所示:
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变量方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。