如何从主函数中编写BufferedWriter

时间:2016-07-19 05:09:57

标签: java bufferedwriter

现在我使用sax解析器来分析xml文件(以下示例代码来自tutorialspoint)。我想在调用字符函数时使用BufferedWriter将所有内容输出到txt文件,而我却无法做到这一点。如果我在characters函数中写这个BufferedWriter,它只会输出最后一个字符串,因为它只会记录最后一个调用。如果我在main函数中编写这个BufferedWriter,因为它们是两个类,UserHandler无法识别在main函数(dblpSax类)中创建的BufferedWriter。我也试过做一个类实例,而我仍然遇到麻烦。你能给我一个方法吗?感谢。

我想使用的Bufferwriter如下:

File writename = new File("output.txt");
writename.createNewFile(); 
BufferedWriter out = new BufferedWriter(new FileWriter(writename));



    public class dblpSax{
   public static void main(String[] args){

      try { 
         File inputFile = new File("student.txt");
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();
         UserHandler userhandler = new UserHandler();
         saxParser.parse(inputFile, userhandler);     
      } catch (Exception e) {
         e.printStackTrace();
      }
   }   
}


class UserHandler extends DefaultHandler {

   boolean bFirstName = false;
   boolean bLastName = false;
   boolean bNickName = false;
   boolean bMarks = false;


   public void startElement(String uri, 
      String localName, String qName, Attributes attributes)
         throws SAXException {
      if (qName.equalsIgnoreCase("student")) {
         String rollNo = attributes.getValue("rollno");
         System.out.println("Roll No : " + rollNo);
      } else if (qName.equalsIgnoreCase("firstname")) {
         bFirstName = true;
      } else if (qName.equalsIgnoreCase("lastname")) {
         bLastName = true;
      } else if (qName.equalsIgnoreCase("nickname")) {
         bNickName = true;
      }
      else if (qName.equalsIgnoreCase("marks")) {
         bMarks = true;
      }
   }


   public void endElement(String uri, 
      String localName, String qName) throws SAXException {
      if (qName.equalsIgnoreCase("student")) {
         System.out.println("End Element :" + qName);
      }
   }


   public void characters(char ch[], 
      int start, int length) throws SAXException {
      if (bFirstName) {
         System.out.println("First Name: " 
         + new String(ch, start, length));
         bFirstName = false;
      } else if (bLastName) {
         System.out.println("Last Name: " 
         + new String(ch, start, length));
         bLastName = false;
      } else if (bNickName) {
         System.out.println("Nick Name: " 
         + new String(ch, start, length));
         bNickName = false;
      } else if (bMarks) {
         System.out.println("Marks: " 
         + new String(ch, start, length));
         bMarks = false;
      }
   }
}

1 个答案:

答案 0 :(得分:0)

你需要一个与解析相同级别的BufferedWriter,最好在DefaultHandler之外进行自定义。

        try (BufferedWriter out = new BufferedWriter(....)) {
            UserHandler userhandler = new UserHandler(out);
            saxParser.parse(inputFile, userhandler);     
        }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }   
}

类UserHandler扩展DefaultHandler {     私人决赛BufferedWriter出局; //或PrintWriter

UserHandler(BufferedWriter out) {
    this.out = out;
}

然后一个人就可以了。到处都写。