java读取本地文件

╰+哭是因爲堅強的太久メ 2022-02-05 12:15 436阅读 0赞
  1. public class Fozu {
  2. public static void main(String[] args) {
  3. readFileByLines("D:\\work\\cmp\\sq-server\\cmp\\src\\main\\resources\\banner.txt");
  4. }
  5. public static void readFileByLines(String fileName) {
  6. File file = new File(fileName);
  7. BufferedReader reader = null;
  8. try {
  9. reader = new BufferedReader(new FileReader(file));
  10. String tempString = null;
  11. int line = 1;
  12. // 一次读一行,读入null时文件结束
  13. while ((tempString = reader.readLine()) != null) {
  14. Thread.sleep(500);
  15. System.out.println(tempString);
  16. line++;
  17. }
  18. reader.close();
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. } finally {
  22. if (reader != null) {
  23. try {
  24. reader.close();
  25. } catch (IOException e1) {
  26. e1.printStackTrace();
  27. }
  28. }
  29. }
  30. }
  31. }

发表评论

表情:
评论列表 (有 0 条评论,436人围观)

还没有评论,来说两句吧...

相关阅读

    相关 py读取本地文件

    在Python中,可以使用内置的`open()`函数来读取本地文件。以下是一个基本的示例,演示如何打开并读取一个文本文件: 使用 'r' 参数表示我们想要读取文件

    相关 iOS 读取本地Json文件

    之前写过类似的方法 今天写这个 的目的是 应对开发过程中面对 服务端数据刚定下模型 但是接口不通 的情况下 不耽误客户端开发进度 + (id)getJsonDataJ