java程序读取文件内容打印控制台

来源:互联网 发布:paypal充值 淘宝 编辑:程序博客网 时间:2024/05/16 14:37

读取文件内容打印控制台

public class test{

7 /**
8 * 读取txt文件的内容
9 * @param file 想要读取的文件对象
10 * @return 返回文件内容
11 */

12 public static String txtString(File file){
13 String result = “”;
14 try{
15 BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
16 String s = null;
17 while((s = br.readLine())!=null){//使用readLine方法,一次读一
18 result = result + “\n” +s;
19 }
20 br.close();
21 }catch(Exception e){
22 e.printStackTrace();
23 }
24 return result;
25 }
26
27 public static void main(String[] args){
28 File file = new File(“需要读取文件路径”);
29 System.out.println(txtString(file));
30 }
31 }

1 0
原创粉丝点击