Updating a Jar file with updates in input Excel - java

I need to create a Jar file which will read an excel and display as output, the existing data and the updated data.
This file needs to keep on running and displaying the Excel data as output. Any update that has been done on the Excel recently needs to be reflected in the output, along with the previous data.
I know how to create a Jar file, i am also able to read an excel file using Apache POI.
I just need an idea regarding how during every run, if the Excel is updated, that updated values can be displayed.
Do we need to implement threading,synchronization? If so, then how?

Synchronization does only work inside of your Java process. Assuming that an external process creates/updates the Excel file therefore synchronization will not help you.
The best chance you have is to listen for file-system changes of the Excel file (see WatchService class) and access the file after it has been changed.
For avoiding (or better minimize) file access conflicts I would open the file, copy the data to memory and then directly close the file.
Alternatively you could copy the file and then operate on the copied file. In both cases conflicts can still occur if the program writing the Excel file tries to perform changes while you are accessing the file.
Potential errors are errors because of blocked file or inconsistent data.

Related

Java Servelet 3.0 File Upload to input stream - without intermediate folders or files being created

I dont know how to do this, or whether is possible or wise, so any form of answer that points me to a library, example or reasoning will be helpful.
I need to upload and process some Java XML files (actually, XSLT files - XML Excel files).
I dont want to store the file on the server and then invoke processing on it. Instead, I want to stream the file in, and process it as a stream.
I also want to be able to process multipart file uploads, but still process that as an input stream.
I am expressly trying to avoid creating a file on disk for this.

inserting existing PDF into one being created, iText 2

I have a web application using iText v2.1.7 to create PDFs; before anyone tries to move me to a different library, let me point out that, like most programmers, I don't choose the libraries my company uses for things, or I certainly would not use this one.
I have code that generates these PDFs; now I am to add code that takes the contents of an existing PDF and inserts it into the PDF I'm creating.
I've found examples of how to do this, but they all use files. Except for the one I'm reading, I don't have files; I'm in a web application where I don't have easy access to a place to write a file.
Can't I open the existing PDF and somehow insert its entire content into the document I'm creating, without having to write to a file?
After I do this, I will have more content to add to the document, either from another file, dynamically created content, or both, so it isn't a simple merge of my content with one existing file. I also haven't created the existing file as its own entity, to be merged with another file, though I suppose I can do that IF it's necessary.
But I was hoping there was a way (or were ways) to do this without having to reorganize my existing code. It's possible the answer is implied in one of these examples, but they don't explain the concepts behind things, so I don't know where I can put input Streams instead of file input streams, output streams instead of file output streams, etc.

Modifying an Existing PDF without creating an new pdf file

Using iText, I am wanting to open a PDF file, add some more pages with text to it, and then close it. I have found some questions like this on here, but all require creating a new PDF file. Is there any way to read in the pdf file and modify it and then overwrite the original?
Of course you can create a new pdf file, and afterwards overwriting the old file with the new one.
Commons Apache File Util
forceDelete(oldPdf)
moveFile(newPdf, oldPdf)
Of course, you can always overwrite a file (if it is not locked by the OS) by writing to the whole content to the FileOutputStream. You cannot partially write to part of a file unless it is to append data at the end of file. This is limited by the operating system itself so there is nothing you can do.

How to close a excel if it is opening when accessing using JAVA and APACHE-POI 3.6?`

We are developing application in JAVA AND using APACHE POI 3.6 for accessing Ms-Excel files. Suppose,my Excel file is opening, when running my program, then it raising the error and program terminated.So, First i want to check that whether my excel is opened or not.If it is opening, then i want to close my excel file and then open and read that file...
POI does not run Excel at all. It is independent code that opens your excel file and interprets the contents. There is no straightforward way for you java code to communicate with Excel and check to see if it is running. You can look into Java/COM bridges for complex mechanisms for trying to coordinate.
I think the best you're going to be able to do from within Java is to check if someone has the file locked for editing using the canWrite() method of the File class.
You're not going to be able to force the user to close the excel file though. If the application has a GUI you could post a message to the user requesting they close the file manually.

Read a file with java while it is saved manually

I have a question concerning java and file input/output.
for an specific task, i have to transfer a file (excel to be precise) while it's opened.
imagine following scenario:
An excel file is opened and used by one user. From time to time the file is saved manually by the user. Now i want to write a java programm which reads the file and transfer it over an socket every 30 sec. No problem so far. My question: what happens if the user saves the document in the exact moment my program wants to read the file. Could this cause any troubles?
Don't know if it matters, but im using an BufferedInputStream to read the file.
My question: what happens if the user saves the document in the exact moment my program wants to read the file. Could this cause any troubles?
Yes.
One or more of the following things could happen depending on your platform, and the way that the Excel file is saved.
If Excel uses locking, then either Excel or the program trying to read the file could get an error saying that the file is in use.
If Excel does a rewrite in place and doesn't lock the file, then the program trying to read the file could see a truncated Excel file.
If Excel writes a new file and renames it, the program trying to read the file could see a state
where the file apparently does not exist.
It could work.
In short, the program doing the reading needs to very defensive ...
Don't know if it matters, but im using an BufferedInputStream to read the file.
That's irrelevant I think.
AFaik, the behaviour will depend on your underlying filesystem / operating system. A unix system typically keep an "un-named" copy of the file being read and starts creating a new file for the "being written" new copy, using inode trickery. An old Windows system would likely reply that the file cannot be written to because it is locked. I don't know about modern Windows systems.
what you can do i think is to alway check the state of the file before you do anything about it. like what have been said in some earlier posts, it all depends on the underlying platform, and you should employ a lot of defensive programming...

Categories

Resources