Computing.Net > Forums > Programming > Need help with JAVA

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Need help with JAVA

Reply to Message Icon

Name: David
Date: November 17, 2002 at 09:33:30 Pacific
OS: win98
CPU/Ram: 750 128
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: November 17, 2002 at 09:38:31 Pacific
Reply:

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.



0

Response Number 2
Name: David
Date: November 17, 2002 at 09:52:33 Pacific
Reply:

The program must be done in Java.


0

Response Number 3
Name: SN
Date: November 17, 2002 at 14:18:36 Pacific
Reply:

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 zero

if (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


0

Response Number 4
Name: Guy
Date: November 17, 2002 at 15:05:08 Pacific
Reply:

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



0

Response Number 5
Name: David
Date: November 18, 2002 at 10:44:25 Pacific
Reply:

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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Need help with JAVA

Need help with WinXP batch script www.computing.net/answers/programming/need-help-with-winxp-batch-script/13165.html

Need help with an SQL join query www.computing.net/answers/programming/need-help-with-an-sql-join-query/13358.html

I need help in Java www.computing.net/answers/programming/i-need-help-in-java/7027.html