opening text file in my textapp - java

I am new in java swing .For practice, I've created text app which simply allow the user to open,read and write text files and also have some editing functions.
I've packaged my .class and other required files into jar file and created .exe file from this jar file.
When user opens text file(.txt,.rtf e.t.c) from the app(by clicking Open file toolbar on my app ) it works fine and display the content of file in JTextpane.
But when user opens text file outside my app (by clicking open with and set my app to open particular text file) to display the content of file on my app,my app just getting opened but not displaying the content of the file in JTextPane .
Can anyone suggest me the way How can this be done?

When you Open With... a file, that file's location is passed to the program as the first argument. So in your public static void main(String... args) procedure, you could add handling of a first argument that is a file location and open that file as you would otherwise from the GUI.

Related

i am not able to view my downloaded file in downloads folder using selenium java

[Screenshot displaying the downloaded file in a window[][1]1i am not able to access my downloaded file which i opened in a new small window popup using selenium java and also i am not able to find it in downloads folder.
When i click on a download button-->the file gets downloaded with an icon of word in a new small popup browser window-->but it does not move into the downloads folder. Testcase passes but i am not able to validate.In that browser window i am not able to find any elements....only .... tags are displayed.. While doing it in manual way i am able to view it in downloads folder.
Kindly advise me what should be done.
you are coding in java language,
so, use method exists() which will return boolean value.
You must know your Downloaded file name along with exact download path,
String FileName = "YOUR_FILE_NAME";
File tmpDir = new File("C:\Users\a.k\Downloads\"+FileName);
if(tmpDir.exists())
System.out.println("File Downloaded");
System.out.println("File Not Downloaded");

Deleting a file, opended by an external application, after closing it

I'm creating a pdf file in my java program.
After having it created to a specified Path, I'm opening it with the user's standard application.
File myFile = new File("F:/test.pdf");
Desktop.getDesktop().open(myFile);
My standard application for pdf files is for example Adobe Reader - so Adobe Reader opens up and displays the file. - So far so good.
Is there any way to delete this "test.pdf" after I close the file/my Adobe Reader?
Check the following link:
Java: Check if file is already open
Run an infinite loop after you open the loop, as mentioned in the above thread, verify and close the file accordingly.
Thanks,
JK
You can create the file in the temp directory, so you will not have to worry about removing it.
To create a file in the temp directory you should use the File.createTempFile method.

How to read text files without defining the location?

I'm starting to build a JFrame application to work with File Handling. What I'm trying to get done from the application is that
it reads the contents of all the texts files in a particular location and merges the contents & creates one single text file.
The main property this application should have is that it should not have the navigate-to-location feature. Suppose if I paste this application in location C:\Users\Desktop\application.exe, the application must search the location for all the text files (i.e. on Desktop) & merge them into one single text file.
I've observed this in patch tools to patch softwares, they never ask for location for the software's_launcher.exe, they just tell us to paste the patch in the directory where the launcher belongs.
How do they do it? How can I do the same for my own application?
"./" is to specify current directory.
if you use
File f1 = new File("./");
then f1 is reference of current directory.
if your application is at C:\Users\Desktop\application.exe place then all files & folder at C:\Users\Desktop can access by "./" string

How to permit the user to navigate into the File System?

I'm making a project that opens a file and reads/writes data.
I'm doing this using the code below:
File file = new File(Environment.getExternalStorageDirectory().getPath()
+ "/Project/Application/" + fileName);
It's working. The problem is I only can get this file if its path is the specific 'Project/Application' directory.
How can I allow the user to navigate into the device file system and find the file himself?
Android does not have a file chooser by itself. The only file list that Android can show natively is a ringtone list.
Here you have some solutions:
Load the file list into an array and show a dialog showing the file list, as you can read here: Choose File Dialog. Using this method you can detect when the user clicks an item that is a folder and then show a new dialog with the files inside that folder, so the user can navigate easily.
Here is a good answer to your question that has a built-in file chooser just in case that the user does not have one installed: Android file chooser
And also here you have a good library: https://code.google.com/p/android-filechooser/

use html file from jar in java

I want to add html files to jar files and access them through jar in the windows application
project how can i access them
I am using the following code .
try{
File f1= new File(getClass.getResource("/path is the src folder/));
}
but it is showing no suitable constructor found
File won't take a URL as reference. Embedded resources like this aren't actually files, they are InputStreams to the resource.
Depending on how you're showing them will determine what you need to do.
For example, JEditorPane takes a URL via its setPage method
UPDATED
You should be able to load the pages directly into the editor pane using something like...
// editor pane is a reference to a JEditorPane
editorPane.setPage(getClass.getResource("/path is the src folder/"));

Categories

Resources