Hi Cecil
When reading or writing from or to a file, you typically use several
layers, each one with its role. For e.g.:
...
java.io.InputStream file = new java.io.FileInputStream( "myfile.txt" );
java.io.InputStream buff = new java.io.BufferedStream( file );
java.io.InputStream in = new java.io.MyFilter( buff ); // extends
java.io.FilterInputStream
byte[] myBuffer = new byte[256];
int len = in.read( myBuffer );
String s = new String( myBuff, 0, len );
...
Each layer has its own role. In this example:
-- The "FileInputStream" knows how to read from a file and exposes its
content byte by byte using "read" (allowing to read one byte or a full
buffer directly from the file)
-- The "BufferedInputStream" always reads a full buffer from a
InputStream (either a FileInputStream or a pipe or other), then allows
to access to the data either byte by byte or by smaller buffers
(improving performances). The BufferedInputStream extends
FilterInputStream too
-- Supplementary filters can do supplementary actions, such as detecting
any "\r\n" sequence in the input stream and returning through its "read"
methods only "\n"
As at any step there is a InputStream that can be used as argument for
the constructor of another InputStream, you can build a chain as long as
you wish (there is no particular reason to build a long chain, it's just
that you can do it if you need).
The output works alike:
...
java.io.OutputStream outFile = new java.io.FileOutputStream(
"myoutfile.txt" );
java.io.OutputStream out = new java.io.MyOutFilter( outFile );
String s = "You say high\nI say low\nYou say why\nAnd I say I don't
know\n";
byte[] myBuffer = s.getBytes();
out.write( myBuffer );
out.flush();
...
Where the filter, for e.g., detects any '\n' and writes further "\r\n"
(you understood, my filters convert from the Windows text file style to
Unix one and vice versa). There is no need for a "buffered" output
stream, as the output is automatically buffered (or not), as imposed by
the system APIs (for e.g., file writing is probably always buffered).
Now, the idea of the exercise is
-- Either to read from a FilterInputStream that converts to upper cases:
o Override the "read()", "read( byte[] b )" and "read( byte[] b, int
off, int len )" methods
o For each read byte (or int), cast it to a character then
Character.toUpperCase(c), then cast it back to int or byte
-- Or write to a FilterOutputStream that converts to upper cases:
o Override the "write( int c )", "write( byte[] b )", "write( byte[]
b, int off, int len )"
o Do the same to convert to upper case
I actually did the two of them for fun.
Hope it helps
mihai
Cecil H a écrit :
I'm probably making this way more complicated than it has to be. So if
someone can maybe lend a hand..... I've been thinking about this for
awhile now. I have accomplished converting to file name to all
uppercase (Granted it was appended by some object data).
The homework states:
write ChangeToUpperCaseInputStream class, which extends
FilterInputStream.
Write ChangeToUpperCaseOutputStream class, which extends
FilterOutputStream.
Use either ChangeToUpperCaseInputStream class or
ChangeToUpperCaseOutputStream class to convert the characters read to
upper case.
Is the best way to accomplish this by:
1. Read the .txt file into a FileInputStream
2. convert the FileInputStream to a string
3. us the toUpperCase() method on the String
4. then convert to the string to a FileOutputStream
5. Finally write the FileOutputStream to a file
Or
1. Read the .txt file into a FileInputStream
2. Create an InputStreamReader
3. Since this returns an integer value of the character. Change the
value of the character to match its uppercase integer value
4. Then Using the InputStreamWriter object, write the value back out
the Stream.
The FilterInputStream, FilterOutputStream extend InputStream and
OutStream overriding all the methods. But where is the additional
functionality? I understand I/O is a very important concept to grasp.
So if anyone could help me understand a better, and mabe point me in
the write direction.
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en