温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java读取txt文件并输出结果

发布时间:2020-08-20 19:29:50 来源:脚本之家 阅读:213 作者:农名工进城 栏目:编程语言

这篇文章主要介绍了java读取txt文件并输出结果,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

描述:

1.java读取指定txt文件并解析

文件格式:

java读取txt文件并输出结果

代码:

package com.thinkgem.wlw.modules.midea;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhouhe
 * @Date: 2019/6/19 8:48
 */
public class Test {
  public static void main(String[] args) {
    // 文件夹路径
    String path = "D:\\input.txt";
    try {
      List<String> scanListPath = readFile02(path);
//      System.out.println(scanListPath);
      for (int i = 0; i < scanListPath.size(); i++) {
        String mytext = scanListPath.get(i);

        //替换所有制表符
        mytext = mytext.replaceAll("\t",",");
        System.out.println(mytext);
        //每一行都转化为新的数组,根据下标去判断参数值对应的参数是什么
        String [] strArr= mytext.split(","); //注意分隔符是需要转译
        for (int m = 0; m < strArr.length; m++) {
//          System.out.println(strArr[m]);
          switch(m){
            case 0:
              System.out.println("时间:"+strArr[m]);
              break;
            case 1:
              System.out.println("甲烷:"+strArr[m]);
              break;
            case 2:
              System.out.println("总烃:"+strArr[m]);
              break;
            case 3:
              System.out.println("非甲烷总烃:"+strArr[m]);
              break;
            case 4:
              System.out.println("氨气:"+strArr[m]);
              break;
            case 5:
              System.out.println("硫化氢:"+strArr[m]);
              break;
            case 6:
              System.out.println("氧气:"+strArr[m]);
              break;
            default:
              break;
          }
        }
      }
    } catch (IOException e) {
      System.out.println("有异常,无法读取!!!");
    }
  }

  /**
   * 读取一个文本 一行一行读取
   *
   * @param path
   * @return
   * @throws IOException
   */
  public static List<String> readFile02(String path) throws IOException {
    // 使用一个字符串集合来存储文本中的路径 ,也可用String []数组
    List<String> list = new ArrayList<String>();
    FileInputStream fis = new FileInputStream(path);
    // 防止路径乱码  如果utf-8 乱码 改GBK   eclipse里创建的txt 用UTF-8,在电脑上自己创建的txt 用GBK
    InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    BufferedReader br = new BufferedReader(isr);
    String line = "";
    while ((line = br.readLine()) != null) {
      // 如果 t x t文件里的路径 不包含---字符串    这里是对里面的内容进行一个筛选
      if (line.lastIndexOf("---") < 0) {
        list.add(line);
      }
    }
    br.close();
    isr.close();
    fis.close();
    return list;
  }
}

结果:

java读取txt文件并输出结果

2.java读取指定文件夹下的所有txt文件并输出内容(我这里一个文件夹下面有 2 个txt文件):

java读取txt文件并输出结果

代码:

package com.thinkgem.wlw.modules.midea;

import java.io.*;

/**
 * @Author zhouhe
 * @Date 2019/10/10 13:10
 */
public class Test2 {


  /**新建一个类把下面代码放进去,注意要设置basePath(你要读取的文件夹),读取和写入的方法也都写好了.你可以根据自己的需求掉用就行了**/
  static String basePath="D:\\测试";
  /**
   * 查找文件夹下所有符合csv的文件
   *
   * @param dir 要查找的文件夹对象
   * */
  public static void findFile(File dir) throws IOException {
    File[] dirFiles = dir.listFiles();
    for(File temp : dirFiles){
      if(!temp.isFile()){
        findFile(temp);
      }
    //查找指定的文件
    if(temp.isFile() && temp.getAbsolutePath().endsWith(".txt") ){
        //获取文件路径,包含文件名
        String filePath = temp.getAbsolutePath();
        //获取文件名
        String fileName = temp.getName();
        System.out.println(temp.isFile() + " " + temp.getAbsolutePath());
    readFileContent(temp);
    }
    }
  }

  /**
   * @param file 要读取的文件对象
   * @return 返回文件的内容
   * */
  public static String readFileContent(File file) throws IOException{
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    StringBuffer sb = new StringBuffer();
    while(br.ready()){
//      sb.append(br.readLine());
      System.out.println(br.readLine());
    }
    System.out.println(sb.toString());
    return sb.toString();
  }

  /**
   * @param file 要写入的文件对象
   * @param content 要写入的文件内容
   * */
  public static void writeFileContent(File file,String content) throws IOException{
    FileWriter fw = new FileWriter(file);
    fw.write(content);
    fw.flush();
    fw.close();
  }

  public static void main(String[] args) {
    try {
      findFile(new File(basePath));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
  }
}

结果:

java读取txt文件并输出结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI