实例如下所示:
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变量方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
下一篇 :
Golang中切片的用法与本质详解
-
道屹道:Android应用手机APP软件在中国为何如此受欢迎呢?
2026-05-15栏目: 教程
-
“自我造血”搞科研,这家省级研究院在莞揭牌,采用“平台+公司”新方式
2026-05-15栏目: 教程
-
获授权可在线查客户健康信息?上海市健康保险交易平台今上线
2026-05-15栏目: 教程
-
国家级科研成果!用机器人造飞机成为可能!
2026-05-15栏目: 教程
-
一头多用,协作式应用或成为下一个市场爆发点
2026-05-15栏目: 教程
