zoukankan      html  css  js  c++  java
  • java 文件读取汇总

    概要说明:这里汇总了线上常用到的各种按行读取方法

    1、BufferedReader

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
     
    public class ReadFileLineByLineUsingBufferedReader {
     
        public static void main(String[] args) {
            BufferedReader reader;
            try {
                reader = new BufferedReader(new FileReader("/Users/test/Downloads/myfile.txt"));
                String line = reader.readLine();
                while (line != null) {
                    System.out.println(line);
                    // read next line
                    line = reader.readLine();
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    2、Scanner

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
    public class ReadFileLineByLineUsingScanner {
     
        public static void main(String[] args) {
            try {
                Scanner scanner = new Scanner(new File("/Users/test/Downloads/myfile.txt"));
                while (scanner.hasNextLine()) {
                    System.out.println(scanner.nextLine());
                }
                scanner.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    3、Files

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.List;
     
    public class ReadFileLineByLineUsingFiles {
     
        public static void main(String[] args) {
            try {
                List<String> allLines = Files.readAllLines(Paths.get("/Users/test/Downloads/myfile.txt"));
                for (String line : allLines) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    4、RandomAccessFile

    import java.io.IOException;
    import java.io.RandomAccessFile;
     
    public class ReadFileLineByLineUsingRandomAccessFile {
     
        public static void main(String[] args) {
            try {
                RandomAccessFile file = new RandomAccessFile("/Users/test/Downloads/myfile.txt", "r");
                String str;
                while ((str = file.readLine()) != null) {
                    System.out.println(str);
                }
                file.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    元素水平居中方式
    JQuery 之事件中的 ----- hover 与 onmouseover 、onmouseout 联系
    JQuery 获取指定url对应的html内容
    BOM 窗体相关属性以及页面可见区域的获取方式
    使用supervisor提高nodejs调试效率
    Jquery on() 动态绑定事件
    局部打印插件 jquery.PrintArea.js
    深入理解BFC和Margin Collapse
    Sublime Text 2 安装Package Control和插件的两种方法
    001 数据库基本概念和操作
  • 原文地址:https://www.cnblogs.com/liang1101/p/14788078.html
Copyright © 2011-2022 走看看