java读取并解析linux上E文件

E文件格式:

<! System=OMS Version=1.0 Code=GB2312 Data=1.0 !>
<XLJH>
@ 序号 名称 ...
# 1   杨小猪 ...
</XLJH>


/**
* 读文件
* eFileName : 读取文件的文件名
* node : E文件里的节点名称
*/
public static List<List<String>> readEFile(String eFileName, String node){
    String path = "/path..." + "/"+ eFileName;
    try {
        int startIndex = 0;
        int thisIndex = 0;
        int endIndex = 0;
        boolean flag = false;
        //文件内容的字符集 UTF8
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path),"UTF8"));
        String line = reader.readLine();
        List<List<String>> listDatas = new ArrayList<List<String>>();
        while ((line = reader.readLine()) != null && flag == false) {
            thisIndex++;
            if(line.startsWith("<"+node)){
                startIndex = thisIndex;
            }
            else if(line.startsWith("</"+node)){
                endIndex = thisIndex;
                flag = true;
            }
            else if(startIndex != 0){
                String[] split = line.split("\\s+");
                List<String> lineDatas = new ArrayList<String>(Arrays.asList(split));
                lineDatas.remove(0);//
                listDatas.add(lineDatas);
            }
        }
        System.err.println(node+"节点标签在第"+startIndex+"-"+endIndex);
        reader.close();
        return listDatas;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
/**
 * 写文件
 */
public static void writeTxt(String txtInfo,String fileName) throws IOException {
    String filePath = "/path" + "/"+fileName;
    File file = new File(filePath);
    if (!file.exists()) {
        file.getParentFile().mkdirs();
    }
    file.createNewFile();
    // write 解决中文乱码问题
    // FileWriter fw = new FileWriter(file, true);
    // 写入文件的字符集 GBK 看需求而设定
    OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(file), "GBK");
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(txtInfo);
    bw.flush();
    bw.close();
    fw.close();
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值