I use java.io.RandomAccessFile to open a file and write few entries in the same at random locations. When I read the file back, is it guaranteed that unwritten bytes in that file (that may be in between two locations which were written earlier) are returned as 0?
Related
I have written some data in hdfs, but i want that to be without the first two bytes that the writeUTF() method writes. I want to copy this first two byte free hdfs file to local file and do some analysis on it.
if (fs.exists(filenamePath)) {
// remove the file first
//fs.delete(filenamePath);
out = fs.append(filenamePath);
}
// create if file doesnt exists
else{
out = fs.create(filenamePath);
}
out.writeUTF(getFeaturesString(searchCriteriaList,fileNameData));
out.close();
The data written is as follows
0aEX Series ex4200-24f....
I want only
EX Series ex4200-24f
I write all the data to hdfs file and then I am copying the file into local to do some analysis. Is there an alternative method to accomplish this..
how to ignore first two bytes hdfs writeUTF() and writeChars()?
You've just answered your own question. Use writeChars().
writeUTF() is only useful when somebody is going to be calling readUTF() to read it. It uses a modified character set and a length-word that is only understood by readUTF().
There's no particular reason to use DataOutputStream here either. If the data is all text, use a BufferedWriter.
I'm writing a program which takes in a byte array of potentially millions of bytes, reads each one from a ByteArrayInputStream, and if the byte is not "printable" (ascii 32-126), that byte is encoded in a certain way and written to a ByteArrayOutputStream instance; if the byte is "printable" it is directly written to that same ByteArrayOutputStream instance.
So from a broader view I am taking in a byte array, and getting back a similar byte array except certain characters have been encoded.
My question is: would it be faster to write my data out to a file or to continuously be writing to this OutputStream?
It will be faster to write the data to your output stream. Writing to a file will involve disk access, which is slower than access to the RAM where the byte array inside the ByteArrayOutputStream lives.
However, if you eventually want to write your byte array out to some other place (say a file) then the intermediate step of the ByteArrayOutputStream is unnecessary and you should just write straight to the end destination e.g. FileOutputStream.
I have a binary file that contains big endian data. I am using this code to read it in
FileChannel fileInputChannel = new FileInputStream(fileInput).getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect((int)fileInputChannel.size());
while (bb.remaining() > 0)
fileInputChannel.read(bb);
fileInputChannel.close();
bb.flip();
I have to do something identical for zip files. In other words decompress the file from a zip file and order it. I understand I can read it in via ZipInputStream but then I have to provide the coding for the "endianness". With ByteBuffer you can use ByteOrder.
Is there an NIO alternative for zip files ?
If you have your ZipInputStream, just use Channels.newChannel to convert it to a Channel then proceed as you wish. But you should keep in mind that it might be possible that a ZipInputStream can’t predict its uncompressed size so you might have to guess the appropriate buffer size and possibly re-allocate a bigger buffer when needed. And, since the underlying API uses byte arrays, there is no benefit in using direct ByteBuffers in the case of ZipInputStream, i.e. I recommend using ByteBuffer.allocate instead of ByteBuffer.allocateDirect for this use case.
By the way you can replace while(bb.remaining() > 0) with while(bb.hasRemaining()). And since Java 7 you can use FileChannel.open to open a FileChannel without the detour via FileInputStream.
How do I read the last n number of bytes from a file, without using RandomAccessFile.
The last 6 bytes in my files contain crucial information when writing the files back. I need to write my original files, and then append the last 6 bytes elsewhere.
Any guidance? Thanks
You have to do it by using RandomAccessFile.Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system.
RandomAccessFile randomAccessFile = new RandomAccessFile(your_file, "r");
randomAccessFile.seek(your_file.length() - n);
randomAccessFile.read(byteArray, 0, n);
You could implement an OutputStream that "decorates" your current stream by extending FilterOutputStream to preserves the last six bytes written. When writing is complete, query your custom decorator for the last six bytes.
The implementation could use a simple ring buffer that records all single-byte writes, or up to the last six bytes of each block write.
try this
FileInputStream fis = new FileInputStream(file);
fis.getChannel().position(fis.getChannel().size() - 6);
byte[] a= new byte[6];
fis.read(a);
I've created a file using java.io.File, FileInputStream & FileOutputStream. Suppose that I want to change the value of some bytes in the file (for instance from the byte 15 to 35) without changing the size of the file. I've tried creating a RandomAccessFile object and then use RandomAccessFile.seek to move to byte number 15, writing my new bytes and then closing the file. The file has changed its size. What's wrong with this approach, and how can this be done successfully?
Are you sure you are writing a byte to the RandomAccessFile? If you are calling the method:
file.write(35);
Then it is actually writing 35 as an int which is 4 bytes. If you want to write a single byte try:
file.writeByte(35);