软件编程
位置:首页>> 软件编程>> java编程>> Java读取txt文件中的数据赋给String变量方法

Java读取txt文件中的数据赋给String变量方法

作者:牧_风  发布时间:2022-08-04 22:32:19 

标签:Java,txt,String

实例如下所示:


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);

}

}

来源:https://blog.csdn.net/hbtj_1216/article/details/52036885

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com