Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: BufferReader and BufferWritter

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default BufferReader and BufferWritter

    I got an home assignment, that is composed by writting a class where i must have 3 methods:

    >static void removeLetters( BufferedReader orders, BufferedReader in, BufferedWriter out ) throws IOException;
    >static void capitalize( BufferedReader orders, BufferedReader in, BufferedWriter out ) throws IOException;
    >static void histogram( BufferedReader in, BufferedWriter out ) throws IOException;

    I started by searching the API to learn the methods i could use, ok... then i realized how can i do this?

    I thought on puting the buffers into strings and then do what i need to them.

    ok, i will use "(char)in.read()" for example, but i dont know the length of the buffer, cause i need this to do a "for" or a "do{...}while" to know how many char's i must put in the string.


    I would like to know how can i know the length, or if u know a easier way to do this, that a "Java begginer" can do.

    Thanks for your time
    Last edited by Nhedro; February 14th, 2012 at 09:21 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: BufferReader and BufferWritter

    I would like to know how can i know the length
    The length of what? What is the source of the bytes you are reading?

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: BufferReader and BufferWritter

    the source is a .txt that was given to me, but it mut read any Buffer given; i must build those methods, anid would like to know if there is a way to know the length of the BufferReader, for example:
    	public static void removeLetters( BufferedReader orders, BufferedReader in, BufferedWriter out ) throws IOException{
     
    		for(int i = 0; i<=*;i++){
    			StringBuilder ord1 = sb.append((char)orders.read());
    		}
    		for(int j = 0; j<=*;j++){
    			StringBuilder in1 = sb.append((char)in.read());
    		}
    //              *     the length of the BufferReader orders, something like orders.length
    //              **   the length of the BufferReader in, something like in.length
          }
    this is my idea, but if anyone have other idea, i would like to discuss which one is better, and learn some more.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: BufferReader and BufferWritter

    If you are reading from a file, you can use the File class to get the size of the file. If you create a File object for the file you can use one of its methods to get the file's size.

    If you use a class like StringBuilder to read the bytes into, you don't need to worry about the number of bytes. The class wil take care of that for you. Don't use a for loop for reading, use a while loop and test the value returned by the read method. It will return a special code when the bytes have all been read. When you get that code, exit the while loop.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Nhedro (February 14th, 2012)

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: BufferReader and BufferWritter

    Thanks Norm, i think i understood, i will try it .

  7. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: BufferReader and BufferWritter

    I've done it if someone is curious i post the code
    static void removeLetters( BufferedReader orders, BufferedReader in, BufferedWriter out ) throws IOException;{
     
     
                 String s;
                 while((s=orders.readLine())!=null)[
     
                 //use the String s to whatever u nead, which contains the current line of bufferreader orders;         
     
                 ]
    }

    Once again, thanks Norm
    Last edited by Nhedro; February 14th, 2012 at 09:32 AM.

Similar Threads

  1. [SOLVED] difference between fileReader and bufferReader
    By shadihrr in forum Java Theory & Questions
    Replies: 6
    Last Post: June 8th, 2010, 01:26 AM