Home > Archive > Java Help > July 2004 > human non readable suite
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
human non readable suite
|
|
| Québec 2004-07-23, 3:58 pm |
| Hi again,
The following will print what I want to screen bu not to a file. See the
commented section.
The file is created but remains empty.
Jean
----
import java.io.*;
import com.bruceeckel.tools.*;
public class ByteTest {
public static void main(String[] args) {
try {
// 1. Buffered input file
DataInputStream in =
new DataInputStream(
new BufferedInputStream(
new FileInputStream(args[0])));
String s, s2 = new String();
while ((s = in.readLine()) != null)
s2 += s + "\n";
in.close();
// 3. Formatted memory input
DataInputStream in3 = null;
in3 = new DataInputStream(
new StringBufferInputStream(s2));
//(char)in3.readByte()
while (true)
System.out.print(in3.readByte());
/*
byte bite = 1;
PrintStream out1 =
new PrintStream(
new BufferedOutputStream(
new FileOutputStream("ByteTest.txt")));
while ((bite = in3.readByte()) != -1)
out1.println(bite);
out1.close(); // finalize() not reliable!
*/
} catch (EOFException e) {
System.out.println(
"End of stream encountered");
} catch (FileNotFoundException e) {
System.out.println(
"File Not Found:" + args[0]);
} catch (IOException e) {
System.out.println("IO Exception");
}
}
}
| |
| Roedy Green 2004-07-23, 3:58 pm |
| On Fri, 23 Jul 2004 09:38:08 -0400, "Québec" <Once@WasEno.ugh> wrote
or quoted :
> PrintStream out1 =
> new PrintStream(
> new BufferedOutputStream(
> new FileOutputStream("ByteTest.txt")));
Nowadays you normally use PrintWriters not PrintStreams.
See http://mindprod.com/jgloss/fileio.html
for sample code to use one.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
| |
| Québec 2004-07-23, 3:58 pm |
| Iwent ther I dont understand.
I get this at the console from
....
System.out.print(in3.readByte());
.....
8410111511632111102321111171166897116977
010510810110100840101011501160320111
0102
0320111011701160680970116097070010501080
101010010
If I copy/paste in Data3.txt
....
new FileInputStream(args[0])));
....
byte bite = 1;
PrintWriter out1 =
new PrintWriter(
new BufferedOutputStream(
new FileOutputStream("ByteTest.txt")));
while ((bite = in3.readByte()) != -1)
out1.println(bite);
out1.close();
..................
Same empty file created.
| |
| Québec 2004-07-23, 3:58 pm |
| I forgot in previous post to write I casted to a char
out1.print((char)bite);
| |
|
|
| Québec 2004-07-23, 8:56 pm |
| Ok.
This is what I am trying to do.
Cant we convert one to the other and vice-versa?
Read a text file 1 and write it to a binary file 2
Read the written binary file 2 and write it to a text file 3.
I am expecting file 3 being an exact copy of file 1.
Jean
"Roedy Green" <look-on@mindprod.com.invalid> a écrit dans le message de
news:5ho2g019e76pdplnbjl25a3fi0ceq1vs0b@
4ax.com...
> On Fri, 23 Jul 2004 14:30:51 -0400, "Québec" <Once@WasEno.ugh> wrote
> or quoted :
>
>
> I think your basic problem is you don't understand the difference
> between human readable and binary formats. Human-readable ones have
> only 8-bit printable characters. Binary ones are not intended to be
> looked at by humans.
>
> See http://mindprod.com/jgloss/printlnformat.html
> http://mindprod.com/jgloss/binaryformats.html
> http://mindprod.com/jgloss/prinable.html
>
>
> You are mixing the two.
> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
| |
| Roedy Green 2004-07-23, 8:56 pm |
| On Fri, 23 Jul 2004 17:44:49 -0400, "Québec" <Once@WasEno.ugh> wrote
or quoted :
>Cant we convert one to the other and vice-versa?
>Read a text file 1 and write it to a binary file 2
>Read the written binary file 2 and write it to a text file 3.
>I am expecting file 3 being an exact copy of file 1.
Any time you use text files there is a translation, and some
characters can be lost.
If you want perfection stay away from text files, or always write them
in UTF-8.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
| |
| Will Hartung 2004-07-24, 3:56 am |
| "Québec" <Once@WasEno.ugh> wrote in message
news:5GfMc.66550$gG4.919477@wagner.videotron.net...
> Ok.
> This is what I am trying to do.
>
> Cant we convert one to the other and vice-versa?
> Read a text file 1 and write it to a binary file 2
> Read the written binary file 2 and write it to a text file 3.
> I am expecting file 3 being an exact copy of file 1.
Yes, of course.
You can do anything you want to do.
But there are some disctinctly lacking details. Notably, WHY do you want to
perform this conversion? What's the goal? Just because something is "in
text" doesn't mean it is necessarily useful to a human being.
To be pedantic, first off a Text file IS a binary file. It's the tool that
treats it differently, the file simply doesn't care, but I'm sure this isn't
what you want.
Read a text file, say a piece of java source code, and run it through a
java.util.zip.GZIPOutputStream, and you will end up with a Binary,
essentially human unreadable file. Read it back through a GZIPInputStream to
get the original. By fluke luck, the output turns out to be smaller to boot.
Dump it to the console or view it in an editor, and you get a bunch of funny
symbols and beeps. Of course, anybody's grandmother could convert the file
into readable text with a file rename and a double click.
This meets your "specifications" perfectly, but is it what you want?
Does your binary file have a particular format that something else may be
interested in?
Any of the several base64 decoders will read base64 encoded text files and
create binary files, and vice-a-versa. Is that what you want? Of course I
don't think anyone would argue that a base64 encoded file is "human
readable", though it is most certainly a text file.
Perhaps you're looking to ENCRYPT the text? Which takes a normal text file
and turns it into illegible garbage through the use of a provided key.
Provide the key to a decoder and you'll get the original file back. Most
grandmothers (as well as others) would have difficulty recovering this kind
of file as long as the key is kept secure. There you need to look at the
JCA/JCE stuff. That is not an area for the faint of heart.
Is THAT what you want to do?
Regards,
Will Hartung
(willh@msoft.com)
|
|
|
|
|