Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, i need some help because i dont know much about java (either about english) and i have to make a program to compress files with a new algorithm i am developing. The problem is that i dont know how to read the binary code of the files. I dont know if it is using DatainputStream and readBytes or something like that... but i want the bit information not the byte one (i dont know if itīs clear enought). The second problem is i dont know to put the results again in a file.
If you have any idea please tell me.
thanks.

Is there a reason that you are doing this in Java rather than C or C++??
My experience is that working at the bit/byte level in Java is not terribly easy. Not that I have done it, but experienced Java programmers I know had lots of trouble doing this type of stuff that I could do in C very quickly and easily.
Maybe it's easy and these guys just didn't know the right classes to use.

I don't know how to do it in C++, but Don ain't kidding when he says it's no picnic to do it in Java. To get input from a file you would probably use something like:
FileReader fr=new FileReader ("c:\test.txt");Now that you have a FileReader, you can read one character at a time:
int i;
i=(int)fr.read();Now you can get a String of binary characters from this int:
String s=Integer.toBinaryString (i);
You can now test each character of the String to find out if it is a one or a zeroif (s.charAt (i)==1)
The outputing to a file is similar to the reading, just use the FileWrite class.
I got all this from Java's documentation on their website:
http://java.sun.com/j2se/1.4.1/docs/api/The above method is crap when it comes to efficiency (of course, if you're doing it in Java in the first place you can't possibly be too worried about efficiency:-)
So anybody else have a more efficient solution? Good luck,-SN

Hi - I admit Java file I/O can seem like dark magic to someone not familiar with the language and supplied classes.
I think you want to use the Stream classes, not the Reader/Writer classes (you are reading binary data I assume, not just text characters).
I hope the following will get you started, there is lots of exception/error checking it could do but does not:
import java.io.*;
public class fileread
{
public static void main(String[] args)
{
new fileread().go(args);
}
private void go(String[] args) {
String inF = "infile.txt";
String oF = "outfile.txt";
FileInputStream ifs = null;
FileOutputStream ofs = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
byte[] inbuffer = new byte[4096];
byte[] outbuffer = new byte[4096];
try
{
ifs = new FileInputStream(inF);
bis = new BufferedInputStream(ifs, 4096);
ofs = new FileOutputStream(oF,false);
bos = new BufferedOutputStream(ofs, 4096);
int lr = 0;
while( (lr = bis.read(inbuffer, 0, 4096)) != -1) {
// Compress data to 'outbuffer'
System.arraycopy(inbuffer, 0, outbuffer, 0, lr);
bos.write(outbuffer, 0, lr);
}
bis.close();
bos.close();
}
catch(Throwable t)
{
}
}
}HTH, Guy

thanks guy! iīll try this code.
you were right i want to use the Stream classes because as i said i want to make a to compress all kind of files not only text ones.. .
Also if you (or somebody else) Know something about coding i will hear any idea.
I making a compressor based in the classical LEMPEL-ZIV coding. So if someone knows were can i find some ideas to improve it tell me.
Thank in advance.
thanks again guy
thanks SN.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |