zoukankan      html  css  js  c++  java
  • 把文件里的数据(文本文件)读成字符串

    代码如下:

    //这个是用Scanner读文件,但是会有异常

    import java.io.*;
    import java.util.*;

    public class Test {
        public static void main(String[] args) throws Exception {
            File file = new File("Test.java");
            Scanner scanner = new Scanner(file);
            String line = null;
           
            while (scanner.hasNextLine()) {
                line = scanner.nextLine();
                System.out.println(line);
            }
            scanner.close();
        }
    }
     

    //这个是用reader的,这个不会出现异常

    import java.io.*;
    import java.util.*;

    public class Test {
        public static void main(String[] args) throws Exception {
            File file = new File("Test.java");
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;
           
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        }
    }

  • 相关阅读:
    day22-20180522笔记
    day20-20180517笔记
    day19-20180515笔记
    day18-20180513笔记
    day17-20180510笔记
    day16-20180508笔记
    Python之初识面向对象
    Python之常用模块(2)
    Python之常用模块(1)
    Python之模块与包(下)
  • 原文地址:https://www.cnblogs.com/yanzi629/p/1882930.html
Copyright © 2011-2022 走看看