Java file to InputStream and the new document from InputStream read and write files operation instance code

Source: Internet
Author: User
A. http://www.java2s.com/Code/Java/XML/NewDocumentFromInputStream.htm


New Document from InputStream

    

Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.StringReader;
Import Java.io.StringWriter;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;

Import Java.util.Iterator;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.transform.Transformer;
Import javax.xml.transform.TransformerException;
Import Javax.xml.transform.TransformerFactory;
Import Javax.xml.transform.dom.DOMSource;

Import Javax.xml.transform.stream.StreamResult;
Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import Org.xml.sax.InputSource;

Import org.xml.sax.SAXException;
 /******************************************************************************* * Copyright (C) 2007 Google Inc. * Licensed under the Apache License, Version 2.0 (the "License")); The May isn't * use this file except in compliance with the License. You may obtain a copy of * the License in * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by app 
 Licable or agreed to in writing, software * Distributed under the License be distributed on ' as is ' basis, without * Warranties or CONDITIONS of any KIND, either express OR implied.
 The * License for the specific language governing permissions and limitations under.
 //** * Various XML utilities.  * * @author simonjsmith, Ksim * @version 1.1-ksim-march 6th, 2007-added functions regarding streaming * @version 1.2-ksim-march 10th, 2007-added functions regarding DOM * manipulation/public class Utils {public
    Static Document Newdocumentfrominputstream (InputStream in) {documentbuilderfactory factory = null;
    Documentbuilder builder = null; Document RET = null;
      try {factory = Documentbuilderfactory.newinstance ();
    Builder = Factory.newdocumentbuilder ();
    catch (Parserconfigurationexception e) {e.printstacktrace ();
    try {ret = Builder.parse (new InputSource (in));
    catch (Saxexception e) {e.printstacktrace ();
    catch (IOException e) {e.printstacktrace ();
  return ret;
 }

}

B. http://www.zuidaima.com/share/1758442014903296.htm

Java reads and writes file operation instance code through InputStream

1. File to InputStream

1 File File = new file ("File.xml");
2 InputStream InputStream = new FileInputStream (file);

2.InputStream to File

01 InputStream InputStream = new FileInputStream ("File.xml");
02
03 OutputStream outputstream = new FileOutputStream ("File-new.xml");
04
05 Int

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.