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...
Related
I am developing a Java application through which I need to read the log files present on a server and perform operations depending on the content of the logs.
Files range from 3GB up to 9GB.
Here on stack I have already read the discussion about reading large files with java, I am attaching the link:
Java reading large file discussion
In the discussion, the files are read locally,
in my case i have to retrieve and read the file on the server, is there an efficient way to achieve this?
I would like to avoid having to download files given their size.
I had thought about using URL Reader to retrieve the files, but I have doubts about the speed of execution.
The files I need to recover are under the path C:\production\LOG\file.log
Do you have any suggestions or advice?
I have a file which I need to upload to a service, and parse into relevant data. The parser and the uploader both require an InputStream. Ought I to open the file twice? I could save the file to a String, but having many of these files in memory is concerning.
EDIT: Thought I should make it clear that the parsing and uploading are entirely separate processes.
Since you are parsing it already it would be most efficient to load the file into a string. Parse it into indexes to the string, you will save memory and can just upload the string whenever you want to. This would be the most effective way, with memory but maybe not processing time.
A reply to one of the comments above.
Separate processes does not mean different threads or processes, just they do not need each other to operate.
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.
Basically, I want to be able execute a byte stream as a file, without writing said bytes to a file.
I want to do this in a windows environment.
You could say I want a to create a file, copy the stream to the file, and open the file as an executable, but I want the file to have no physical manifestation on the disk.
Thanks
This is not possible to do using the standard Java API. You will have to rely on some OS specific native implementations, either that do the whole thing for you, or that allow you to create some sort of RAM-disk on which you can place your temporary data and execute it.
Possibly related:
write file in memory with java.nio?
Supposing you bytestream is the one of a standard exe, and you don't want to call a whole virtualisation environnement, the short answer is no because internal adresses in the code wouldn't map.
Here's a more detailled answer :
process.start() embedded exe without extracting to file first c#
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.