Open a file using java......... how?

hi all
I have a prob with my code
the problem is by using the FileChooser like
file = fileChooser.getSelectedFile();
I have the file name but how can i open that file e.g. if file is word document then that file should open in word , if that file is HTML file then open in IE , and other files as well.
is there any way to open a file
plz help me
Thanx
Regards
Satinderjit

veer ji, try this code on your pc if you can. Click the print button and see that happens. thanx yuvraj:
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
//import com.sun.java.swing.*;
import javax.swing.*;
public class ComponentPrinterFrame
extends JFrame
implements Printable {
public static void main(String[] args) {
ComponentPrinterFrame cpf = new ComponentPrinterFrame();
cpf.setVisible(true);
public ComponentPrinterFrame() {
super("ComponentPrinterFrame v1.0");
createUI();
protected void createUI() {
JPanel panel = new JPanel();
JButton printButton = new JButton("Print");
panel.add(printButton);
panel.add(new JList(new Object[] { "One", "Two", "Three" }));
panel.add(new JButton("Push me"));
panel.add(new JCheckBox("Chess", true));
panel.add(new JComboBox(new Object[] { "Eins", "Zwei", "Drei" }));
printButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(ComponentPrinterFrame.this);
if (pj.printDialog()) {
try { pj.print(); }
catch (PrinterException pe) {
System.out.println(pe);
setContentPane(panel);
setSize(400, 400);
// Center.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
int x = (screenSize.width - frameSize.width) / 2;
int y = (screenSize.height - frameSize.height) / 2;
setLocation(x, y);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
public int print(Graphics g, PageFormat pf, int pageIndex) {
if (pageIndex != 0) return NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)g;
g2.translate(pf.getImageableX(), pf.getImageableY());
getContentPane().paint(g2);
return PAGE_EXISTS;

Similar Messages

  • How to open/read file using Java in Unix?

    Hi Friends,
    Can you please help me out how to open/read file using java in unix os? I have create one text file in "/home/test.txt" in unix environment. How to open the same file & read using java code?
    - Hiren Modi

    http://java.sun.com/docs/books/tutorial/essential/io/index.html

  • How can we open any file using JAVA...

    Hi all
    i trying to make code in that code i choose a file using a filechooser then put that file name in FILE object like (File file = fileChooser.getSelectedFile();)
    but the thing is how can i open that file for example if that file is HTML file then opens in IE or if that file is MS Word document that open in Word, like that
    is there any suggestions
    Thanx
    Regards
    Satinderjit

    start is a windows command-line utility.
    start foo.doc will start Word (or Star Office or Word Perfect or whatever is registered for the .doc extension),
    start bar.htmnl will start your registered browser etc.

  • Error trying to open a file using java.io.* in an Applet

    I have a file called wordlist.txt and I am using java.io.* to open it. When I try it
    in Applet Viewer it runs without a hitch. But when I try to run it in a
    browser as an Applet, it errors out saying:
    com.ms.security.SecurityExceptionEx[package3/SpellCheck.setWordList]: cannot access file wordlist.txt
    I have tried adding this file to my Project and accessing it that way but I still get
    the same message. If anyone can help with this, I sure would appreciate it.
    My code is as follows:
    String inWord = new String("zzz");
    try {
    File myWordList = new File("wordlist.txt");
    BufferedReader inWordList = new BufferedReader(
    new FileReader(myWordList));
    inWord = inWordList.readLine();
    while (inWord != null) {
    wordList.addElement(inWord);
    inWord = inWordList.readLine();
    } // end while
    inWordList.close();
    } // end try
    catch (Exception e1) {
    // Pr is equivalent System.out.println();
    Pr("Error Reading this line " + '\n' + inWord + '\n' + e1);
    } // end catch
    } // end setWordList()

    Applets are generally prevented from reading as well as writing files, but appletviewer can load files from the hard drive, so, that is the cause of the behaviour you have observed. To know more about enabling applets load files, you have to sign your applets,
    learn more at
    http://java.sun.com/sfaq/#prevent

  • Excel hyperlink is opening pdf file using reader, how can I make it use Acrobat professional?

    I created a hyerlink in excel to a pdf file. The file automatically opens up using  adobe reader, I want it to open using Acrobat professional. How do I set it up. Thanks

    Open Acrobat, go to Edit - Preferences - General and click the Select
    Default PDF Handler button there.

  • Open  a file using Java

    Write a java program to open a text file. Check if the file already exists. If the file already exists then append data to it. Otherwise program should write 10 lines of text data in the file.

    i was told that all i need to do is
    BufferedReader in = new BufferedReader(new FileReader(filename));
    in.readLine()
    then just write to it
    but i am not sure on how to do that.
    If anyony can help me i would appreciate it..
    thanks

  • Downloading images and audio file using java

    Dear All
    I have a directory on my webserver which contains images,audio files and many other data files.
    I want to download all the file but the problem is when i download image and save it, it is not viewable. I dont want to use Image class as i may have to downlaod audio files too. Please guis which stream shouldi open to download any kind of content using same code.
    Thanks in advance. And if u have some example please let me know
    Regards
    Jafery

    downloading images and audio file using java -------------------------------
    how to write it back to dataoutputstremI don't understand what you are talking about.
    Server: DataOutputStream -> Client: DataInputStream -- the two is a pair.

  • How to Write in bold to a file using java I/O classes?

    Hi,
    Using I/O classes I want to prepare a .doc file in which some text will be there. I want that text to be formatted. Like some text I want to be bold. Some text to be italic. How can I write bold/Italic text to the file using java I/O classes.
    Thanks
    Prashant

    By .doc file, I'm assuming you mean an MS Word document, yes? (fyi, Word Perfect also used the .doc extension)
    The .doc format is proprietary to Microsoft and isn't documented by them. In order to output a file in .doc format you'd have to understand that format correctly, otherwise MS Word will spit out it's tongue at you, call you names, and maybe send the Microsoft Police to "audit" your PC Software Licensing.
    Fortunately for you there is an open source project to demangle the microsoft file formats. See http://jakarta.apache.org/poi/ and especially pay attention to the HDF project (Horrible Document Format).
    - David

  • Opening a template indd file using java API

    How do I open a template indd file using java API and use it for laying out graphics and text ?
    Thanks in advance

    Sample code:
    VariableType vtDocument = myApp.open(VariableTypeUtils.createFile("c:\\myfile.indd"));
    myDocument = DocumentHelper.narrow(vtDocument.asObject());
    Thanks
    -arun

  • Using Java, How can I Update, Add, Delete nodes in XML Files.

    Hi,
    I want to store the student record (like Name, Age, school name, total mark etc.,) as nodes in the XMLfile. Also I should able to Update, Add, Delete any nodes (student record) in the XML file. How can I achieve this...using Java
    I am able to read the content of the xml file using xml-parser. But my problem is
    updating the xml file.
    pls suggest some solutions or links with " example source code"
    Thanks :-)

    There are 2 kinds of XML parsers : SAX and DOM. DOM seems to suit your need. You can use JAXP APIs to add, delete or change nodes or attributes.
    http://java.sun.com/webservices/jaxp/dist/1.1/docs/tutorial/TOC.html provides contents that would satisfy most of the needs.
    To save a DOM modified XML file use java IO APIs to write to the same file from which it was read using a Document object ( doc.getNodeValue() ).

  • How to read pdf files using java.io package classes

    Dear All,
    I have a certain requirement that i should read and write PDF files at runtime. With normal java file IO reading is not working. Can any one suggest me how to proceed probably with sample code block
    Thanks in advance.

    hi I also have the pbm. to read pdf file using JAVA
    can any body help meWhy is it so difficult to read the thread you posted in? They say: java.io is pointless, use iText. So why don't you?
    or also I want to read a binary encoded data into
    ascii,
    can anybody give me a hint how to do it.Depends on what you mean with "binary encoding". ASCII's binary encoding, too, basically.

  • How to print a text file using Java

    How can I print a text file using Java without converting the output to an image format. Is there anyway I can send the characters in the text file as it is for a print job? I did get a listing doing this ... but that converted the text to an image format before printing....
    THanks,.

    Hi I had to write a print api from scratch, and I did not convert the output to image. Go and read up on the following code. I know there is a Tutorial on Sun about the differant sections of the snippet.
    private void printReport()
         Frame tempFrame = new Frame(getName());
         PrintJob printerJob = Toolkit.getDefaultToolkit().getPrintJob(tempFrame, "Liesltext", null);
         Graphics g = printerJob.getGraphics();
                    //I wrote the method below for calculations
         printBasics(g);
         g.dispose();
         printerJob.end();
    }This alone wont print it you have to do all the calculations in the printBasics method. And as I said I wrote this from scratch and all I did was research first the tutorial and the white papers
    Ciao

  • How to read the data from Excel file and Store in XML file using java

    Hi All,
    I got a problem with Excel file.
    My problem is how to read the data from Excel file and Store in XML file using java excel api.
    For getting the data from Excel file what are all the steps i need to follow to get the correct result.
    Any body can send me the code (with java code ,Excel sheet) to this mail id : [email protected]
    Thanks & Regards,
    Sreenu,
    [email protected],
    india,

    If you want someone to do your work, please have the courtesy to provide payment.
    http://www.rentacoder.com

  • How to create a table in the file using java code.?

    HI,
    I should export the data from the view objects to a word document. I have done that but I should
    display the data in the form of a table.
    Kindly come up with the necessary information on how to create a table in the file using java.
    Thanks,
    Phani

    Hi, Thank you for responding to my query.
    The below are the details of my code.
    DCBindingContainer dcBindings =
    (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
    DCIteratorBinding StudentDetailsContent =
    (DCIteratorBinding)dcBindings.get("StudentView1Iterator");
    OutputStreamWriter w = new OutputStreamWriter(outputStream, "UTF-8");
    Row currentRow =
    StudentDetailsContent.getRowSetIterator().first();
    Object a[]= currentRow.getAttributeValues();
    int i;
    for(i=0 ;i<=a.length;i++){
    w.write(a.toString());
    w.write(" ");
    w.flush();
    I am usning this coding to achieve the task of exporting data to file.
    I need to display this information in the table that is where I need help from you people.
    Thanks,

  • I am using I Photo 9.2.3 with OS 10.6.8 When I upgraded to current I photo some of my Iphoto books i put together disappeared.  Cant find them anywhere, including in the time machine...I photo cant open older files...How can a find  th book entitled "Mom"

    I am using I Photo 9.2.3 with OS 10.6.8 When I upgraded to current I photo some of my Iphoto books i put together disappeared.  Cant find them anywhere, including in the time machine...I photo cant open older files...How can a find  the book entitled "Mom"  ?  Does Apple keep ordered books on file?

    Apple keeps books for a short time (used to be 40 days. I've not seen a recent time frame)
    What version did you have?
    Books do nor exist except in the iPhoto database so you can not "find" a book vista TM. You restore a previous library (be sure NOT to overwrite your current library) and switch to it
    LN

Maybe you are looking for

  • Please Help - ApplicationManager 2.0 won't install - Mac Intel OSX 10.6.8

    I've Adobe Master Suite 5.0 demo on my hard disk and would like to deinstall it the correct way. Unfortunately each and every time I attempt to deinstall any program, I receive a dialog box informing me that the Application Manager is either not pres

  • APEX Error: Current version of data in database has changed since user init

    Hi: APEX 4.1 I have a page with 2 regions. The first region is built with custom SQL using the APEX APIs. I have a process the can successfully update records. I built the second region with the tabular form wizard. This created the multi row update

  • Aperture stretches or distorts some photos?

    Hi guys! New to the forums, new to Aperture! Had a quick search and found a kind of answer, but am hoping for something better! Have recently made the transition from iPhoto to Aperture and am enjoying it very much. However am experiencing what seems

  • Compile error in adjacency matrix

    Hello, I'm getting a strange compile error in my fileStream constructor also I'm having trouble accessing nodes. Any advice is much much appreciated and will be rewarded as such cheers mport java.io.*; import java.util.*; class AdjMatrix      private

  • Slowing down a widget

    Hello everyone I have this widget code (<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><head><title></title></head><body><iframe name="XXAnimation" src="http://homepage.mac.com/name/file/X