• Post Reply Bookmark Topic Watch Topic
  • New Topic

FilterInputStream  RSS feed

 
John Dou
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to write a FilterInputStream that reads a file and replaces all the exclamation marks (!) to periods(.). please help me.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, first post! Welcome to the ranch. You will want to change your display name before the sherrifs stumble across it. They're very nice, but pretty stern about made up sounding names. It doesn't have to be your real name, just not an obvious fake.

Have you read the JavaDoc on FilterInputStream? Looks like it overrides all the methods of InputStream to just read from its input and pass the results along. You need to override some of the methods again to do your filtering.

For example, it says read() just calls in.read() and returns the result. You want to call in.read(), see if it's a ! character and maybe change it to a period. You may have to override the other read array methods, too.

At the ranch we like to give broad hints for answers so you get the full thrill of the hunt for the real deal. Lemme know if this was too vague and you need more specific help. We try to keep learning fun!
 
John Dou
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please explain me a little bit in detail..
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm, let's try again:

[Stan]: You will want to change your display name before the sherrifs stumble across it. They're very nice, but pretty stern about made up sounding names. It doesn't have to be your real name, just not an obvious fake.

"NewJava Learner" is an example of what we consider an obvious fake. No real person in the history of the world has ever (or will ever) have that as their real name. Please see our display name policy. Thank you.

As for the rest: can you give us some idea which parts you're having trouble with? For example: have you read the JavaDon on FilterInputStream? Do you have any idea abou thow it might apply to your problem? Have you used any types of streams before? Do you know how to override a method? It's hard for our posters to provide useful feedback without some idea which things you already understand, and which you're having trouble with. I hope that helps...
 
John Dou
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the following program.

import java.io.*;


public class ChangeChar extends FilterInputStream
{
private int matchChar;
private int replaceWith;

public ChangeChar(InputStream input, int char1, int char2)
{
super(input);
matchChar = char1;
replaceWith = char2;
}



public static void main(String args[])throws Exception
{
int match = '!';
int replace = '.';
System.out.println(match+" "+replace);

ChangeChar c = new ChangeChar((new FileInputStream("Test.txt")),match,replace);

FilterOutputStream fileOutput = new FilterOutputStream(new FileOutputStream("Test.txt"));

int temp;

while(( temp = c.read() ) != -1)
{
if (temp == 33)
temp = 46;
fileOutput.write(temp);
}


c.close();
fileOutput.close();

}
}

but its erasing all the contents in the text file. why is that?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Documentation doesn't come right out and say it, but if you read the doc for the two-argument constructor for FileOutputStream:

public FileOutputStream(String name, boolean append) throws FileNotFoundException

Creates an output file stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.


perhaps you can infer something about the constructor which omits the boolean argument. . .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!