zoukankan      html  css  js  c++  java
  • 按行读入xml文件,删除不需要的行 -Java

    删除挺麻烦的,这里其实只是把需要的行存到arraylist中再存到另一个文件中

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;



    public class domainRealse {
    public static void main(String[] args) throws IOException {
    readFileByLines("E:\wewew.xml");
    }

    private static void readFileByLines(String fileName) {
    File file = new File(fileName);
    BufferedReader reader = null;
    ArrayList<String> arrStr = new ArrayList<String>();
    ArrayList<String> errStr = new ArrayList<String>();
    try {
    System.out.println("以行读文件内容,一次读一整行:");
    reader = new BufferedReader(new FileReader(file));
    String tempString = null;// 每次读入都存在一个temp中
    int line = 1;
    while ((tempString = reader.readLine()) != null) {
    if (-1 != tempString.indexOf("zhaoyueplc")) {

    //如果此行有“zhaoyueplc”的字样,将其存入errStr
    System.err.println("line " + line + ": " + tempString);
    errStr.add(tempString);
    } else {

    //没有的话,存到arrStr
    arrStr.add(tempString);
    }
    line++;
    }
    reader.close();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (reader != null) {
    try {
    reader.close();
    } catch (IOException e1) {
    }
    }
    }
    BufferedWriter output = null;
    try {

    // arrStr写到result.xml
    output = new BufferedWriter(new FileWriter("E:\result.xml"));
    for (String str : arrStr) {
    output.write(str + " ");
    }
    output.close();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (output != null) {
    try {
    output.close();
    } catch (IOException e1) {
    }
    }
    }
    BufferedWriter output1 = null;
    try {

    //errStr写到”deletedItem中“
    output1 = new BufferedWriter(new FileWriter("E:\deletedItem.txt"));
    for (String str : errStr) {
    output1.write(str + " ");
    }
    output1.close();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (output1 != null) {
    try {
    output1.close();
    } catch (IOException e1) {
    }
    }
    }
    }
    }

  • 相关阅读:
    Hadoop使用实例
    hdfs操作命令
    Hadoop安装与HDFS体系结构
    Linux和MySQL的安装与基本操作
    Hadoop演进与Hadoop生态
    作业一
    Shader笔记——9.简单灰色效果+遮罩
    iOS——xcodeproj脚本
    iOS——KSAdSDK
    UnityPlugins——4.SignInWithApple
  • 原文地址:https://www.cnblogs.com/zhaoyueplc/p/3336268.html
Copyright © 2011-2022 走看看