FileInputStream without Creating an Actual File using Java - java

I was wondering if there was any way to create a FileInputStream object from just a file object without creating an actual file on the file system? What I am attempting to do is create a file object with some information, and then upload that file somewhere else. I have no need for it to be on the local file system. I know that I could just create a temp folder and then delete it afterwards, but was wondering if it was possible to not do it that way?

What I am attempting to do is create a file object with some
information, and then upload that file somewhere else
In that case you should not work with any file-related classes at all. Instead, crate a byte array, which you can tread as an InputStream via ByteArrayInputStream.

You are probably looking for a ByteArrayInputStream or something similar.
A file input stream reads from a file on disk, that is its purpose. By the way, a File object in Java does not really represent a file, but rather the path pointing to a (potential) file on disk.

Try creating a memory stream, your file is stored in the memory instead of the file system

Related

When does BufferedWriter create file?

Using BufferedWriter.write() when is a file created?
I know from the docs that when the buffer is filled it will flush to file, does this mean that:
every-time the buffer is filled an incomplete file will appear on my file system?
or that the file is only created when the BufferedWriter is closed?
My concern is that I am writing files to a directory using a BufferedWriter and another process is polling the directory for new files and reading them. I do not want an incomplete file to be created and be read by the other process.
Using BufferedWriter.write() when is a file created?
Never. BufferedWriter itself just writes to another Writer. Now if you're using a FileOutputStream or a FileWriter (where the first would probably be wrapped in an OutputStreamWriter) the file is created (or opened for write if it already exists) when you construct the object, i.e. before you've actually written any data.
My concern is that I am writing files to a directory using a BufferedWriter and another process is polling the directory for new files and reading them. I do not want an incomplete file to be created and be read by the other process.
One typical way of handling this is to write to a staging area and then rename the file into the correct place, which is usually an atomic operation. Or even write the file into the correct directory, but with a file extension which the polling process won't spot - and then rename the file to the final filename afterwards.
BufferedWriter doesn't create a file as Jon Skeet said. And you cannot guarantee that another process won't read an incomplete file when it is being written to disk. But there are two things you can do:
Lock the file so that the other process cannot read it before writing is complete. There are several questions concerning file locking in Java on this site (search for "[java] lock file").
Create the file with another filename (ie. use an extension that is not being looked for by the other process) and rename it when writing is finished.

Save an object and resources to the same file in java

im developing a program in which I need to make a save file that contains a serialized object with all the settings and some images that the user added to the program, i tried adding the images as imageicons to the object so they would get serialized with it but it turned out to be very inefficient regarding size, then, i tried serializing the object through a objectoutputstream and then in the same file serializing the image with imageoutputstream, but when i deserialize the image it is corrupt, the object does deserialize correctly tho, so how can i make a save file that contains a serialized object and some resourses in the same file?? (like a project save file) thanks in advance
You can implement some class which handles packaging system. Firstly serializing the Object with ObjectOutputStream and then save the resource next to it (can be in some folder when theres more resources) and pack it into ZIP archive (Java got API for this). The extension can be something else than ".zip"...Microsoft docx format or OpenOffice odt format is also a ZIP archive which contains many XML files... :)

Execute a bytestream as a process in Java without writing to file

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#

Java zip append line

Is it possible to efficiently append a line into a zip or gzip file?
I'm storing equity market data directly into the file system and I have around 40 different files which are being updated every 5ms.
Whats the best way of doing this?
Use a database, not a zip file.
Database is recommended.
If you really want to use plain text file, put them directly on the file system (and if you are using Linux, choose a proper file system for it).
If you do want to use plain text file and put the text files in a zip file, check zip file system below:
java.nio.file.FileSystems:
http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider.html
The zip file system provider introduced in the Java SE 7 release is an implementation of a custom file system provider. The zip file system provider treats a zip or JAR file as a file system and provides the ability to manipulate the contents of the file. The zip file system provider creates multiple file systems — one file system for each zip or JAR file.
TrueZip
http://truezip.java.net/
TrueZIP is a Java based plug-in framework for virtual file systems (VFS) which provides transparent access to archive files as if they were just plain directories
And remember: use memory to cache, reduce disk operations, and make the writing non-blocking.
Complete cycle of editing zip file (i mean read-modify-close) will produce too much overhead. I think it is better to accumulate changes in memory and modify target file at some reasonable rate (i.e. every 5 seconds or even more).
One approach could be like compressing data sent over a socket and flushing compressed blocks to disk from time to time.
You can use a ZipOutputStream's write method to write at a given offset.
String filepath = new String("/tmp/updated.txt")
FileOutputStream fout = new FileOutputStream("/tmp/example.zip");
ZipOutputStream zout = new ZipOutputStream(fout);
byte[] file = IOUtils.toByteArray(mWriter.toString());
short yourOffset = 0;
ZipEntry ze = new ZipEntry(filepath);
try {
zout.putNextEntry(ze);
zout.write(file, yourOffset, file.length);
zout.closeEntry();
} catch(Exception e) {
e.printStackTrace();
}
If you convert your file to a byte array using Apache commons IOUtils (import org.apache.commons.io.IOUtils) you can rewrite and replace the zip entry by calling write at your the offset where the line you want to edit begins. In this case it writes the entire file, from 0 to file.length. You can replace the file in the zip by creating a ZipEntry with a path to the updated file on your drive.

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.

Categories

Resources