Results 1 to 9 of 9
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default I/O How to open a file

    I'm not sure if this is something that I can do with Java. I know that I can open a webpage, or read/write from a text file, but is there a way to simply open a text file using java. Basically simulate someone double clicking a text file and having it open.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    What do you mean by "open". Using the classes in the IO package you can read the contents of a file. You can then display the contents on a JTextArea. Is this what you mean? Are you trying to write your own text editor?

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    Not sure I understand your question but I will make a guess that you are interested in: How to Integrate with the Desktop Class (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    I am referring to simply opening a file. For instance, if you had a file on your desktop called results.txt, and you double click it, it opens.
    Basically within my program, I am creating and writing to a text file. Once this is done, I would like to open the file so the user can see the results, without having to navigate to the file and manually open it.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You keep saying "open" but it really isn't very clear what you want to do. If you mean start another application to display the file then it all depends upon what the file is. If it is a plain text file then you can open it with Notepad, Wordpad, Word or any other text editor. If this is what you really what then research using Runtime.exec and make sure you Google for the pitfalls associated with this.

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    not sure what I'm not explaining. Just want to open it, have it be presented to the user, so they can see the text file, as if I double-clicked the .txt file and had it open up.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I would like to open the file so the user can see the results, without having to navigate to the file and manually open it
    Possibly you are looking for JFileChooser which lets the user actually choose the file they want to open. Or possibly you want to merely create a File instance (no navigation needed).

    Either way, once you have the file use Desktop or JTextArea etc to taste. The basic difference between these is that in the first you let the OS (desktop etc) decide what "open" means and in the second your code implements whatever it is that you intend "open" to mean.
    Last edited by pbrockway2; 07-18-2011 at 03:16 AM.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I give up!

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    I think the Desktop class described in post#3 would do what is wanted. It will open the default application for the text file as if it had been double clicked.

Similar Threads

  1. Open File xls
    By karno in forum NetBeans
    Replies: 4
    Last Post: 03-21-2010, 07:16 AM
  2. Replies: 0
    Last Post: 03-05-2010, 04:21 AM
  3. Different Files Open in File -> Open Workspace
    By mgm2010 in forum JCreator
    Replies: 0
    Last Post: 04-11-2009, 02:14 PM
  4. Open File
    By JavaLerner in forum SWT / JFace
    Replies: 5
    Last Post: 07-12-2008, 06:41 PM
  5. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •