栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java读取txt文件并输出结果

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java读取txt文件并输出结果

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

描述:

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

文件格式:

代码:

package com.thinkgem.wlw.modules.midea;

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


public class Test {
  public static void main(String[] args) {
    // 文件夹路径
    String path = "D:\input.txt";
    try {
      List 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("有异常,无法读取!!!");
    }
  }

  
  public static List readFile02(String path) throws IOException {
    // 使用一个字符串集合来存储文本中的路径 ,也可用String []数组
    List list = new ArrayList();
    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;
  }
}

结果:

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

代码:

package com.thinkgem.wlw.modules.midea;

import java.io.*;


public class Test2 {


  
  static String basePath="D:\测试";
  
  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);
    }
    }
  }

  
  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();
  }

  
  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();
    }
  }
}

结果:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/136058.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号