To Open File

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.

[433 byte] By [stoppera] at [2007-11-26 13:55:48]
# 1
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?
zadoka at 2007-7-8 1:35:01 > top of Java-index,Java Essentials,New To Java...
# 2

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

stoppera at 2007-7-8 1:35:01 > top of Java-index,Java Essentials,New To Java...
# 3

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].)

doremifasollatidoa at 2007-7-8 1:35:01 > top of Java-index,Java Essentials,New To Java...
# 4

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

{

Filefile = 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);

}

}

fastmikea at 2007-7-8 1:35:01 > top of Java-index,Java Essentials,New To Java...
# 5

>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!

stoppera at 2007-7-8 1:35:01 > top of Java-index,Java Essentials,New To Java...
# 6

> >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

jbisha at 2007-7-8 1:35:01 > top of Java-index,Java Essentials,New To Java...