1

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 .

Ali AKhtar
  • 161
  • 2
  • 12
  • 1
    It's actually the close event of the _file-processor_, not the file. – Mordechai Jul 29 '15 at 18:26
  • Do you mean close the file from within the Java application? Why having the excell file anyway, if it's just a temporary 'while running' thing? – Stultuske Jul 29 '15 at 18:27
  • I don't know, what you want to archieve, but.... what about writing the excel file programmatically with Apache POI? Or not using an excel file at all? What are your use cases? – bobbel Jul 29 '15 at 18:28
  • @Stultuske while java application is running get the close file event of a native desktop. Ofcourse it's a temporary file I also use File.createTempFile but the same problem not get user updated value. – Ali AKhtar Jul 29 '15 at 18:45
  • @bobbel I am creating a filesystem above local filesystem having the functionality to save files like filesystem do . But for initial purpose my filesystem is saving excel file .on click I open excel file on a temporary file in Windows OS path and open it using Desktop class java. User can work like we work on excel sheet in our local filesystem .I want to watch this temporary file close event so that I get excel temporary file data and save it to the database – Ali AKhtar Jul 29 '15 at 18:54
  • @MouseEvent is there any api of java to get the `file-processor` events?? – Ali AKhtar Jul 29 '15 at 18:56

2 Answers2

2

I don't know any straightforward way, the uphill (:-)) way would be:

Community
  • 1
  • 1
MirMasej
  • 1,592
  • 12
  • 17
  • WatchService - thanks for bringing this up. I didn't know about this before. – bobbel Jul 29 '15 at 19:19
  • Can it suffers a lot if user opens lets suppose 100 excel file for the reading purpose ? Not update any thing – Ali AKhtar Jul 29 '15 at 19:27
  • From performance view - no, opening 100 files would be an issue here, if anything. However if you want to react on Excel file being closed regardless changed or not - it won't be possible with approach I proposed. – MirMasej Jul 30 '15 at 06:21
0

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.

Jason D
  • 8,023
  • 10
  • 33
  • 39
Sudheer
  • 131
  • 1
  • 1
  • 10
  • Delete the file when user close the file? It can be for instance 100 files user open ,then i have to check all files value at regular interval . How this will affect the performance?? – Ali AKhtar Jul 29 '15 at 19:18