Converting any file into binary in java - java

Anyone know how can I read any file in binary using Java? I want to be able to read any image, document, pdf etc as a stream of binary digits. Thanks

Sounds like you want FileInputStream! You may find the Basic I/O tutorial useful too.

use FileInputStream
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

Related

What file extensions can be created/written with Java's Writer write() method?

I have a question about Java streams but I can't seem to find the answer in the documentation: What file formats or extensions can be written with java.io FileWriter inherited method write()? Does it depend on the platform?
I've tested writing some files like txt, doc, html, rtf, xml, sql & csv alright so far, but I'm wondering what else can be written and what cannot.
Thank you very much for your help!
FileWriter writes text, so that's the only restriction.
FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.
You can, of course, write files with any extension you like, or no extension at all. They just might not be valid files for the format.

what are prepackaged character stream classes?

While reading Java Tutorials, the topic Basic I/o says, use InputStreamReader and OutputStreamWriter when there are no prepackaged character stream classes.
1)What are Pepackaged character stream classes?
Does it mean, a file already has some text!
The term is quite vague and doesn't really seem to be defined anywhere, so good question.
As best I understand it it means things like FileInputStream, FileOutputStream, ByteArrayOutputStream, etc. Classes that have wrapped up a particular kind of stream for you and provide the functionality required to work with it.
Note that most of these streams are working with characters not bytes, and that is generally what you want in Java for dealing with String data in files. On the other hand though if you are reading a pure binary source then the data will come in as bytes and you can then use InputStreamReader to convert those bytes to characters.
So a prepackaged stream reader is one that already provides you the data pre-packaged in the form that you want it.
I believe it to mean classes which inherit Reader or Writer. Such classes "wrap" byte streams so as to convert them automatically to character streams. Example: FileReader, FileWriter; they can read text from files directly.
If no such classes exist for your particular stream needs but you know what you get out of it/put into it is text, then you must use these two wrapper classes.
Classical example: HTML. It is text, but what you get from sockets is byte streams; if you want to read it as HTML, use a Reader (with the correct encoding!) over the socket stream (but of course, many APIs today don't require you to do that).

Convert XML to binary in Java

I want to convert XML into binary data in Java? What is the fastest and easiest way to do this?
Is there any internal Java method that I can use?
If you want to just compress the xml then you can read it and use either GZIPOutputStream or ZipOutputstream as described here.

Java 6 File Input Output Stream (same file)

I searched and looked at multiple questions like this, but my question is really different than anything I found. I've looked at Java Docs.
How do I get the equivalent of this c file open:
stream1 = fopen (out_file, "r+b");
Once I've done a partial read from the file, the first write makes the next read return EOF no matter how many bytes were in the file.
Essentially I want a file I/O stream that doesn't do that. The whole purpose of what I'm trying to do is to replace the bytes in an existing file in the current file. I don't want to do it in a copy or make a copy before I do the Read->Write.
You can use a RandomAccessFile.
As Perception mentions, you can use a RandomAccessFile. Also, in some situations, a FileChannel may work better. I've used these to handle binary file data with great success.
EDIT: you can get a FileChannel from the RandomAccessFile object using getChannel.

Java NIO - How to efficiently parse a file containing both ascii and binary data?

I have some data files looking something like this:
text
header
"lots of binary data hear"
/header
more text
header
"more binary data"
/header
....
Most of the files are around 1-5MB in size. It's very unlikely that I will have to deal with any files larger than approximately 30MB.
I'm fairly new to Java NIO and the API looks a bit like a jungle to me. Could anyone give me any pointers to how I should go about parsing a file like this?
Would it be possible to have multiple threads consuming data from different parts of the file? The file will just be open for reading.
Redesign the file. That's a terrible design.
Question is how would you know if you're reading text or binary data. If there is a clear demarcation of the text and binary regions (like a marker, or a defined block size), then I suspect Preon would be able to help you out. Preon does have support for reading both text and binary data in a useful way. And since I'm pretty sure your binary data represents something else, you might also be able to decode the binary bits into a more useful data structure than just an array.
you can FileChannel.map(), and read it like an array.

Categories

Resources