I have file reader object in Java.
When we load the file in, is entire file loaded or only file pointer will be loaded like in C. Because I have application (producer) wherein it keeps on writing to the file and other application (consumer) will start to read the file after few minutes, I want to read all file data which will be written by the application producer.
I tried to search but could not get the answer.
Best and canonical approach would be to use Java NIO memory mapped files. Use MappedByteBuffer.force() on each write to insure the consumer process flushes the dirty page files. Consumers (in other process if necessary) can map the same file in read mode.
A FileReader works a lot like a FileInputStream, which in turn works a lot like its C analogue -- when you first open a file, very little data will actually be loaded immediately. The file's contents generally aren't read in til you first read some of it, at which point the runtime reads enough to fill the request, or the buffer if you're using a BufferedReader (possibly plus enough to fill up a block, depending on the OS and JVM).
When i say it "works like its C analogue", though, i mean it. A FileReader is opened for reading, and a FileWriter is opened for writing. There could be issues with having a file open for reading and writing at the same time. Windows, in particular, isn't too fond of it. If you care about portability, you may want to find another way -- alphazero's answer sounds promising, but i've never tried it.
I don't understand what you mean when you wrote, "when we load the file in". Sample code would be helpful here. Please see How to Ask.
If you've called anything that sounds like "read" on your Reader then you've got data in memory. If this is text data (assuming you're using FileReader) then you're better off using a BufferedReader. Not only is it buffered, it's more convenient for text files.
If I have the answer all wrong then please post some code so I know what you're talking about. And welcome!
Related
I am writing an applet that I eventually want to put online so that my friends/family can use it. I have the applet running now locally, but in order to work properly it needs to read a .ser file in when the applet opens, and update that same file when the applet closes. The file is quite large (~180 MB), though I am working on paring it down.
What would be the fastest/most effective way to read/write this file in java? There is a lot of information out there on this and I have never done anything like it before, so it's a bit overwhelming. The class HTTPURLConnection seems like an option to read it, but not write it. Any free web hosting that I have seen will not allow a file that big to be uploaded.
The size of the file should hopefully go down substantially, it is a list of 2.8 million musical artists, many of which I'm sure nobody using the program will ever encounter, but if this program is to be effective, many artists will have to be stored, so the problem most likely remains the same.
Thanks in advance for any help
It sounds like it would be wise to keep this large data and the processing of it on your server instead of making the applet operate on it. That's because you would avoid each user downloading a large file and processing it. If you had a server side piece that the applet could call to get useful information from, then only your server would have to load it, write it, and process it. You could implement a Java servlet, or a PHP program to respond to http requests from your applet in a format that suits the data. I'm assuming that your server can handle either servlets or custom PHP (most can).
There's a file I wanted to get into, but whenever I try to open it I get the message "The process cannot access the file because it is being used by another process".
Well, I want in! So, how can i do it?
I've been brainstorming a few ways to try, I'm hoping to get some input on other ways, or if my ideas wouldn't work for some reason that is not apparent to me.
Idea 1 The folder knows where the file is, it just won't open it. What if I create a program to read from the memory address of the file, copy it, then rebuild it somewhere else? I'm not sure if this has hope, because it relies on the file being the issue.
Idea 2 How does my process know that another process is using the file? If it's checking against all the other processes, maybe I can also figure out which process is using that file and pause it or end it.
Either of these ideas will probably take me weeks. Is anyone more creative and can think of another way; or more knowledgeable and eliminate an impractical idea?
In Windows, applications are allowed to obtain exclusive locks on files. When the process opens the file, one thing you specify is who else can access it while your process does (those are the .NET methods, but equivalents exist in other languages). Excel, for example, is notorious for getting an exclusive lock when you open a file. The way around it is usually to find the offending process and kill it to break the lock. Unlocker is the app that I'm most familiar with to accomplish this. If the process is a System process, however, you may not be able to kill it. You'd have to reboot to reset the lock.
Reading directly from another process's memory is unlikely to be reliable. The application may not have an in-memory copy, may not have a complete in memory copy, may not have a consistent in memory copy, and may not have an in memory copy that matches what's on disk (If they're editing the document, for example).
Your process knows that the file is locked because when it tries to open the file, it does so by asking the operating system for access to the file. The operating system responds saying, "Request denied. Another process has this file open and locked." The OS doesn't tell your process what process has the file open because trying to open a file doesn't include asking for who already has it open. Your process must ask the right question to get the answer you're looking for.
Windows makes you specify a sharing modes when opening a file. The sharing mode may prevent the file from being read, written, or deleted while you have it open. If you want to allow simultaneous read access you should include FILE_SHARE_READ in the dwShareMode parameter when you call CreateFile (http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx).
In other words, if you want to enable concurrent access to an open file you must modify the way the file is opened in the first place.
The portable standard libraries in C and Java don't offer a way to set the sharing mode when opening a file, but their usual implementations on windows set the sharing mode to READ+WRITE.
I'm trying to use Java WatchEvent ENTRY_MODIFY to check if a file is being access (ie: read, copied to clipboard). However from the documentation and a small test case I've made, that event isn't being fired. It's only fired when the file is changed.
Am I doing something wrong? If so, how can I monitor a file on the filesystem?
This isn't directly built into java. Your best bet is to jump into a native OS solution. This can be tedious if you want to support multiple systems though.
If you can get away with supporting windows take a look at THIS LINK . Scroll down to the bottom and look at similar apps. You would be interested in any app that contains a command line interface. What you will need to do is install one of the software and then kick off a process using Runtime.exec. You could potentially just use a direct dll, but I'm not qualified to tell you which dll will give you that information or if it even exists. It might be something you want to look into though if you do not want a 3rd party dependency.
You will read the results of the process that hooks into the windows dll's and will tell you if the file is currently open (See this link for more details). Your application will have to pull data (consistently asking the Application if the file is open). It is not ideal, but probably it is a potential solution.
Answering from your definition of file being accessed (copied and being read), however for file alteration there are several existing API available. Here is an example given to monitor file alteration.
To check file is copied to clipboard, you can use Clipboard#hasFiles() method when content of clipboard modified. If it returns true than file is copied to clipboard.
To check file is being read currently, you can check if the file is locked or not using implementation of FileLock abstract class. It has acquiredBy() method which returns the channel currently holding the lock on file.
you can try other libraries to accomplish that task, for example http://jnotify.sourceforge.net/
or http://java.dzone.com/announcements/new-java-library-monitor-file the latter specifically stands: File Access Monitoring- You will be able to receive notifications about events when access or modification date is changed.
I am using a EMC Documentum content management system. I am trying to automate some file importing (moving files to CMS repository) with the help of their own API. (Dont panic by this next sentence, just read it, since its EMCs' own API that you may be unaware of) For that, I have to first create an object of type IDfFile and then pass that object to IDfImportNode.add() which performs importing.
What I want to do is to fetch the file at runtime from one server and immediately perform import operation. I want to do all this in-memory, without saving fetched files on the disk - since those files are confidential.
However the problem is that IdfFile(string) takes absolute path of the file to be imported. So the file has to exist on the disk physically, which will eventually leave traces of files on disk even after I delete files after import. I was guessing if it can take Stream object, but there is no such overload.
So I want to know if I can encrypt files before saving to disk or any other way out of it. Or that I request EMC people to provide suitable API method.
If the files are truly confidential, and you really want to go through so much trouble to keep them that way, you should have encrypted them in the first place.
The files leaving traces on disk should be the least of your worries, one would need physical access to the disk to perform deep sleuthing on it to find out what the files were. Much more realistic problems are attackers gaining access to the server and reading the files like that.
But to still answer your question:
Encryption could be a solution to a lot of things, but always consider if it's worth it.
You could always ask the "EMC people" for a solution to this, of course, especially if you have commercial support
for leaving traces of the file on the disk, it's easy: encrypt the disk. Take some solution like Truecrypt, encrypt the whole disk, mount it, run your program. Everything that is saved in the disk will be encrypted, but it will be transparent to your program (it will only read and write to a device, and won't have to bother if the file is stored encrypted or not).
The problems are:
if someone has access to your computer while it is doing that, he can read the files
if your computer is hacked somehow and can be accessed from the outside, he can read the files
The way to do this is to use the IDfSysObject.setContent() method. It is going to be more code, because you can't use the Import operation conveniences, but it should allow you to save a stream. Maybe something like this (but you would already have the stream from somewhere):
File pdfInput = new File("C:\\Rupinder\\MyFile.txt");
byte[] outBytes = new byte[(int)pdfInput.length()];
FileInputStream fileInputStream = new FileInputStream(pdfInput);
fileInputStream.read(outBytes);
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(outBytes);
IDfSysObject sysObj = (IDfSysObject)session.newObject("c_pdf");
sysObj.setObjectName("testDoc");
sysObj.setContentType("crtext");
sysObj.setTitle("import operation");
sysObj.link("/Temp/Source Folder");
sysObj.setContent(out);
sysObj.save();
source: https://community.emc.com/message/98225
I'm trying to edit configuration file in Java. What I really need to do is to change single line, so reading the whole file and writing it back would be waste of time, since configuration file can be big.
Is there a more efficient way to do this? Except reading in/editing/writing out file. I thouhgt of converting entire file to string, replacing the line I want and writting it back.
I don't know how efficient would that be, can someone give me some other suggestions or the one I mentioned are ok, execution time is important.
I would recommend to use the Preferences API instead. Then on the Windows platform your preferences is stored in the registry. On other platforms the corresponding way to save application preferences is used. See also Preferences API Overview.
How big of a configuration file are we talking here? 1k lines? 10k? 1m lines? If the line you want to edit is the last line, just seek to the start of the line, truncate the file there and write the new one. If it's not... you will need to read it whole and write it again.
Oh, and the 2 options you mention are actually the same (read/edit/write).
On the third hand, I think it's irrelevant (unless you have weird constraints, like a flash storage device which takes too long to write, and has limited write cycles), given the sizes of most config files.