Open text file directly upon execution
Author |
Message |
mirus17
|
Posted: Tue May 15, 2007 11:20 pm Post subject: Open text file directly upon execution |
|
|
Hi, i have a project that writes and opens text files to a table. However, i am using a third party software to create an awesome installer with my programm, which has an option of associating specific file formats with my java program. But when i click on the file to open with my application, it just opens a new window, without loading the data inside the file into the textfields. Is there any way to load the file contents directily into the tables upon file execution like it happens for example in excell or word documents? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rdrake
|
Posted: Wed May 16, 2007 12:58 am Post subject: RE:Open text file directly upon execution |
|
|
Most documents you create with an office suite are actually zip files containing things like settings, data, images, etc.. When you double click on said documents, your program would need to be passed along the location of the file. It would unzip and load all the files from this file and act according (be it setting settings, populating a spreadsheet with data, or whatever is necessary). |
|
|
|
|
|
Luny
|
Posted: Wed May 16, 2007 6:10 am Post subject: RE:Open text file directly upon execution |
|
|
When you are associating your program with a file. My understanding is that its passes your program with a command line args. So in your program's main method, have it so a user can call your program along with an argument.
code: |
java YourProgram.java fileToLoad.txt
|
Check to see how many arguments the user passed and load them accordingly. |
|
|
|
|
|
mirus17
|
Posted: Wed May 16, 2007 6:22 pm Post subject: Re: Open text file directly upon execution |
|
|
sory, but i dont find this information too helpfull, maybe an example would help. From what i see, the filename would always be different, it is the content that matters. when i click any text file and open it with my program, i d like the data to be loaded instantly to the specified tables, thats pretty much all, and by the way, my .java program is an executable now (remember i said above that i use a third party software to convert java file into an exectuable). Still waiting for an answer on this one, thanks in advance. |
|
|
|
|
|
rdrake
|
Posted: Wed May 16, 2007 6:46 pm Post subject: Re: Open text file directly upon execution |
|
|
You're sitting at your desktop. You find the file you want to edit, and double click on it. What happens next?
The location of that file you just opened is passed to the program editing that file. You want to load the file passed to the program.
So, now that you have the location of the file, you have to load the contents of said file (this is your department, unless you post some code we can't really help you with this).
The user can edit their file and do whatever, once finished and they click save, you would want to save the file to the same location the file information came from initially.
Essentially what I'm trying to say is this: The files you double click on contain only data, nothing else. When you double click on these files, they launch the main program which also gets the location of the file. The main program uses this file location to load the file contents. |
|
|
|
|
|
mirus17
|
Posted: Wed May 16, 2007 6:50 pm Post subject: Re: Open text file directly upon execution |
|
|
rdrake @ Wed May 16, 2007 6:46 pm wrote:
The location of that file you just opened is passed to the program editing that file. - how do i manage to do that? |
|
|
|
|
|
rdrake
|
Posted: Wed May 16, 2007 7:15 pm Post subject: Re: Open text file directly upon execution |
|
|
mirus17 @ Wed May 16, 2007 6:50 pm wrote: rdrake @ Wed May 16, 2007 6:46 pm wrote:
The location of that file you just opened is passed to the program editing that file. - how do i manage to do that? Your main entry point into the application should be passed a listing of arguments. Most "Hello world" Java examples should tell you how to do this, I myself do not know Java. This information should automatically be passed, all you need to do is parse it. |
|
|
|
|
|
mirus17
|
Posted: Wed May 16, 2007 7:36 pm Post subject: Re: Open text file directly upon execution |
|
|
could anyone give me an example with using buffered reader - i just need to know how to manage to extract the location of the file being executed. i dont know how can it be automaticly accessable, when you read from a buffered reader, you need to specify the location of the file and its filename inside the code, but the location and filename are always different |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Luny
|
Posted: Wed May 16, 2007 7:42 pm Post subject: Re: Open text file directly upon execution |
|
|
mirus17, When you associate a file with a program, it passes it through main's arguments. That string array in the main() method's parameter actually means something.
code: |
...rest of code
public static void main(String arg[]) throws Exception
{
String locToFile = arg[0];
File newFile = new File(locToFile); //throws an exception
//you can figure the rest out
try {
BufferedReader in = new BufferedReader(new FileReader(locToFile));
String str;
while ((str = in.readLine()) != null) {
readAndProcess(str);
}
in.close();
} catch (IOException e) {
}
}
|
This is what rdrake was saying. Through the main() method, the location of the file was passed and its up to the program how it is handled. If you don't find this solution understandable or useful. I think you need to take some lessons on how the main method works before associating files to your program. Look at my example! The location could be ANYWHERE AND DIFFERENT. |
|
|
|
|
|
|
|