6 Replies Latest reply: Jan 2, 2007 10:34 AM by 807599 RSS

    To Open File

    807599
      I've created the File class:
      String pathAndFile = new String("something correct");
      File fileThatMustBeOpened = new File (pathAndFile);
      So, how can I open a file, that's represented by fileThatMustBeOpened?
      I see here two directions: opening exe-files, and opening files, that must be opened by other programs.
        • 1. Re: To Open File
          807599
          Define what you mean by open file?

          You can read the data in the file, but I think you might be talking about running it on the OS with another application?

          What are you trying to do?
          • 2. Re: To Open File
            807599
            Yes, I talk about the second variant. The other application must open the file.

            Generally speaking, I'm writing lite File Manager. The files and directories are represented by labels. When the user click the label, the text of label helps to create the appropriate instance of File class.

            Message was edited by:
            stopper
            • 3. Re: To Open File
              doremifasollatido
              You don't need "new String" here, and you shouldn't use it here (and, for a literal string, almost never need it). Just use:
              String pathAndFile = "something correct";
              To open the file, you need the "cmd /c start" command in Windows. Read Reply #11 of the following:
              http://forum.java.sun.com/thread.jspa?forumID=31&threadID=749669

              (I didn't test it, and didn't check if the code is any good. But, it does call the correct command [assuming you are running Windows].)
              • 4. Re: To Open File
                807599
                i dont what are you trying to accomplish. but here is the code (assumming you have a .txt file already made ) and you want to make some changes.
                import java.util.Scanner;
                import java.io.*;
                
                class SquareDisk
                {
                  public static void main (String[] args) throws IOException
                  { 
                    File    file = new File("myData.txt");   // create a File object
                    Scanner scan = new Scanner( file );      // connect a Scanner to the file
                    int num, square;
                
                    num = scan.nextInt();
                    square = num * num ;   
                
                    System.out.println("The square of " + num + " is " + square);
                  }
                }
                • 5. Re: To Open File
                  807599
                  To open the file, you need the "cmd /c start" command in Windows
                  Yes, it's work. And now It even needn't to create an instance of File class, as I thought:
                  String pathAndFile = "something correct";
                  Runtime.getRuntime().exec("cmd /c start " + pathAndFile);
                  thank you!
                  • 6. Re: To Open File
                    800323
                    To open the file, you need the "cmd /c start"
                    command in Windows

                    Yes, it's work. And now It even needn't to create an
                    instance of File class, as I thought:
                    tring pathAndFile = "something correct";
                    Runtime.getRuntime().exec("cmd /c start " +
                    pathAndFile);
                    thank you!
                    You will most likely want to read the following; otherwise, you may run into some unexpected problem if the executable has a lot of output:
                    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html