This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Hi...how to print to a file...

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I currently have some code that reads an xml file and prints it out to the dos prompt screen. However, can someone tell me how I can print the whole output to a file? I know how to simply print something to a file, but I don't know how to get this xml to a file. **I have pasted code below, thanks

Thanks so much,
Carmen
********** code***********
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;



public class JDomPractice {
public static void main(String[] args) {
// Assume filename argument
String filename = args[0];
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object

try {
// Build the document with SAX and Xerces, no validation
SAXBuilder builder = new SAXBuilder();
// Create the document
Document doc = builder.build(new File(filename));

// Output the document, use standard formatter
XMLOutputter fmt = new XMLOutputter();
fmt.output(doc, System.out);
/*
out = new FileOutputStream("myfile.txt");
// Connect print stream to the output stream
p = new PrintStream( out );
p.println ("This is written to a file");
p.close();
*/
} catch (Exception e) {
e.printStackTrace();
}
}
}
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carmen,

I don't know too much about the JDOM APIs you are using - but I would guess to output the xml document to a file, could you do something like:

fmt.output(doc, p) ?

System.out is a reference to a PrintStream object, and so is your "p" reference. The only difference is that System.out is directing your ouput to the console and p would write the output to the file myfile.txt.

If that doesn't work, you need to study the javadocs that come with the API.

Anyway try it and see how you go.

Regards,
JD
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic