JAVA 文件解析

IOtest java文件解析

Java 菜鸡学习的文件解析的过程。

WIN+ALT 查询不全调用的函数;

  • 读取文件在控制台输出,即读取文件进入内存
    大致思路是:
    读取文件路径
    获取文件
    创建字符集
    创建一个使用输入缓冲区的缓冲字符输入流
    逐行读取并输出

代码块

实现如下:

package io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class iostest {



    public static void main(String[] args){
        String pathname = "D:\\MINI--JAVA\\test.txt";
        File file = new  File (pathname);

        if(file.exists()){
            System.out.println("exists");
            InputStreamReader inputStreamReader=null;
            try {
                inputStreamReader  = new InputStreamReader(new FileInputStream(file),"utf-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String LineTxt = null;
                while((LineTxt=bufferedReader.readLine()) != null){
                    System.out.println(LineTxt);

                }


            } catch (UnsupportedEncodingException | FileNotFoundException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }

        }
        else {
            System.err.println("no exists");
        }
    }
}

在读入后可以进行相应操作,比如分割,并只显示想要的部分:
用split切分一下(切分时注意用“.”或“|”等转义字符作为分隔的话,必须加上\,即String.split(“\.”),才能正确的分隔开,不能用String.split(“.”);)
在循环里增加:

String a[]=LineTxt.split(" ");
System.out.println(a[0]);

显示切分的第一组

关于切分,这位写的我觉得挺清楚:
https://blog.csdn.net/qq_27093465/article/details/54910323

猜你喜欢

转载自blog.csdn.net/Mick_JJ/article/details/81297517