Example usage for java.io File File

List of usage examples for java.io File File

Introduction

In this page you can find the example usage for java.io File File.

Prototype

public File(URI uri) 

Source Link

Document

Creates a new File instance by converting the given file: URI into an abstract pathname.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);// www.j a v  a 2  s .  c  om

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
        Element element = (Element) list.item(i);
        Comment comment = doc.createComment("index=" + i);

        element.getParentNode().insertBefore(comment, element.getNextSibling());
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from ww w.jav a  2s  . co m*/

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    // Find all elements with the name "entry" and append a comment
    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
        Element element = (Element) list.item(i);
        Comment comment = doc.createComment("index=" + i);
        // Add the comment after this element
        element.getParentNode().insertBefore(comment, element.getNextSibling());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    User admin = new User();
    admin.setId(new Long(1));
    User foo = new User();
    foo.setId(new Long(2));

    ObjectOutputStream oos = new ObjectOutputStream(
            new GZIPOutputStream(new FileOutputStream(new File("user.dat"))));
    oos.writeObject(admin);/*from w  ww  .  j a v  a  2s .  c o  m*/
    oos.writeObject(foo);
    oos.flush();
    oos.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Items.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    File xml = new File("input.xml");
    Items items = (Items) unmarshaller.unmarshal(xml);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(items, System.out);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from w  ww .j  a v  a 2s.com

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Element element = doc.getDocumentElement();
    Element element2 = doc.createElement("newname");
    NamedNodeMap attrs = element.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        element2.getAttributes().setNamedItem(attr2);
    }
    while (element.hasChildNodes()) {
        element2.appendChild(element.getFirstChild());
    }
    element.getParentNode().replaceChild(element2, element);
}

From source file:Main.java

public static void main(String[] args) {
    String destFile = "luci3.txt";

    try (PrintStream ps = new PrintStream(destFile)) {
        ps.println("test");
        ps.println("test1");
        ps.println("test2");
        ps.print("test3");

        // flush the print stream
        ps.flush();//from   w ww .  j av a2s . co m

        System.out.println("Text has  been  written to " + (new File(destFile).getAbsolutePath()));
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  w w w .  j a v  a 2 s.c  o m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    // Add a PI at the beginning of the document
    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction("target", "instruction");
    element.getParentNode().insertBefore(pi, element);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Document doc = null;// w  ww.j av a  2s .  c om
    String filename = "name.xml";

    Source source = new DOMSource(doc);

    File file = new File(filename);
    Result result = new StreamResult(file);

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream("filename"));
    ZipEntry zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        String entryName = zipentry.getName();
        File newFile = new File(entryName);
        String directory = newFile.getParent();
        if (directory == null) {
            if (newFile.isDirectory())
                break;
        }//from   ww  w .j  a  v a  2 s  . c o  m
        RandomAccessFile rf = new RandomAccessFile(entryName, "r");
        String line;
        if ((line = rf.readLine()) != null) {
            System.out.println(line);
        }
        rf.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();
    }
    zipinputstream.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLEventReader xmlr = xif.createXMLEventReader((new FileInputStream(new File("./file.xml"))));

    boolean inline = false;
    StringBuffer sb = new StringBuffer();
    while (xmlr.hasNext()) {
        XMLEvent event = xmlr.nextEvent();

        if (event.isStartElement()) {
            StartElement element = (StartElement) event;
            if ("data".equals(element.getName().toString().trim())) {
                inline = true;//  w  ww. j a  v  a  2s  .  c o m
            }
        }

        if (inline) {
            sb.append(xmlr.peek());
        }

        if (event.isEndElement()) {
            EndElement element = (EndElement) event;
            if ("data".equals(element.getName().toString().trim())) {
                inline = false;
                System.out.println(sb.toString());
                sb.setLength(0);
            }
        }
    }
}