My application saves excel file data in a database (mongodb) using java.
On user click my application will do
First create an excel file on local filesystem for instance C:\ali.xlsx and place data on excel file .
Open excel file C:\ali.xlsx using java.awt.Desktop class open method for the cross platform perspective.
When user close file C:\ali.xlsx gets its close event so that i will get file updated value and save it to the database.
Delete the file C:\ali.xlsx from the local filesystem.
My Question :
How to implement the third bullet point. In short : how to get close event of any file using java.
If anyone have another approach to implement this functionality please tell me also that's why I write the application flow .
I don't know any straightforward way, the uphill (:-)) way would be:
watch file system with WatchService, (tutorial)
in case of modification of the Excel file check if it's open in processes list and if not - follow with next step.
If I understand correctly you have an Excel sheet and you want to save this data to mongodb every time it gets updated.
There is no trigger available for what you ask. Instead you can keep reading the file at regular intervals and check the "lastModified" attribute. Or as you are deleting the file every time it means if it exists you have an update.
Related
I am currently using Apache POI to enter data into Excel file. The only problem is I can not keep the file open if I have to append data to the same file. Are there any specific sample codes which would allow me to do so?
My basic requirement is to fetch Runtime data from a place (this I am able to do) and add it to the Excel sheet while the file is still open.
Any suggestions?
I don't think that this is possible without a C# addon or some kind of macro. You could write a simple C# addon for Excel that connects to your java programm and recieves the realtime data. The addon will write it to the spreadsheat then.
Let me first describe where I am. For readability, that will take three paragraphs.
Original Problem: I am working on a php website, one function of which is to read data from a Word file users upload and then insert the entry into some database. Note: the server is running windows (devil windows!).
My Solution: Write a java program to do the word file reading and database inserting stuff, using java Apache POI library. Then execute the java program in one bat file. Then new problem occurs...
New Problem: How can we make a specific bat file run on a specific file system event? In my case, the event will be file creation in one assigned folder.
Wish I have made myself totally understood. Waiting for your help. Any suggestion is appreciated.
If I were you, I would implement some kind of scheduled task(http://technet.microsoft.com/en-us/library/cc772785%28WS.10%29.aspx) on the server that runs the Java app you have for adding the files. Depending on your traffic, you could set it to run every 5 minutes, or every 5 hours, or whatever.
That Java app would then do this:
Look at that directory
If it sees some file(s), loads the file(s) into the database
When it is finished loading the files, it would delete them from the directory (so it doesn't add them the next time the task executes).
At the same time, I'm surprised you're not implementing something with PHP through the web app. If your website is already in php, wouldn't it make sense to keep this application logic as part of the PHP stuff as well? To each their own.
If you went the php route, a super simple approach would be this:
have some page submit_doc.php with a form where users can upload a doc
when they hit the submit button, some other php file would execute, say upload_doc.php
upload_doc.php would handle connecting to the db and adding the file to the db
I'm automating a scenario in which I need excel file to be updated with current date on daily basis( using java or any macro).For this to update on daily basis,I need to open and close the file so that it can save the current date.I'm using a continuous integration system where in, automation gets triggered at 5am.Before it triggers,I need to open the file and close.
I tried open and close using JXL.But, i'm not sure of reference issues which might get disturbed.
Can this be somehow automated?
Thanks in advance.
You can call a java function that returns the current date and write it to the excel file using jxl.
To make it automated visit this http://www.advancedinstaller.com/java.html. Set this to auto start service. I guess that will solve your problem.
I have a web application in GWT and a complementary desktop client also written in Java (so the same solution basically applies to both). In my program users can attach files, then download them later or do whatever. These files are stored as blobs and can be in just about any format. Many of the users that use Excel and Word want to be able to open the file, make changes, then have those changes stored back in the attached file. In other words, need an inline editing of attachments.
Any ideas on how to make this happen? Should I have an 'edit' mode that keeps a file handler while the file is open, and then store that File handler? Some way keeping track of whether the file is changing, or not?
Sorry about the late response. Amol >> I have that going. I want to save directly back to a blob as if it were a filehandle. Thought that was clear in my question.
I have decided that this is almost impossible with a web application without writing some kind of client interface for each and every potential file type - word, excel, pdf, graphics, etc...
I have this recurrent Java JAR program tasks that tries to modify a file every 60seconds.
Problem is that if user is viewing the file than Java program will not be able to modify the file. I get the typical IOException.
Anyone knows if there is a way in Java to modify a file currently in use? Or anyone knows what would be the best way to solve this problem?
I was thinking of using the File canRead(), canWrite() methods to check if file is in use. If file is in use then I'm thinking of making a backup copy of data that could not be written. Then after 60 seconds add some logic to check if backup file is empty or not. If backup file is not empty then add its contents to main file. If empty then just add new data to main file. Of course, the first thing I will always do is check if file is in use.
Thanks for all your ideas.
I was thinking of using the File
canRead(), canWrite() methods to check
if file is in use.
Not a good idea - you'll run into race conditions e.g. when your code has used those check methods, received true return values, but then the file is locked by a different application (possibly the user) just before you open it for writing.
Instead, try to get a FileLock on the file and use the "backup file" when that fails.
You can hold a lock on the file. This should guarantee you are able to write on the file.
See here on how to use the FileLock class.
If the user is viewing the file you should still be able to read it. In this case, make an exact copy of the file, and make changes to the new file.
Then after the next 60 seconds you can either:
1) Check if the file is being viewed and if not, delete it and replace it with the earlier file, then directly update this file.
2) If it is being viewed, continue making changes to the copy of the file.
EDIT: As Michael mentioned, when working with the main file, get a lock on it first.