Results 1 to 6 of 6
  1. #1
    edcaru is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default File input output

    I have the following code which should read binary data from a file and write it to the console as character data. It is not working because the data is written as binary data, it is not converted to char as supposed to. Can anyone help me please?

    Java Code:
    package Tutorial;
    
    /**
     *
     * @author Admin
     */
    import java.io.*;
    
    public class fileStreamTest{
    
       public static void main(String args[]){
    
       try{
          byte bWrite [] = {11,21,3,40,5};
          OutputStream os = new FileOutputStream("C:\\Temp\\test.txt");
          for(int x=0; x < bWrite.length ; x++){
             os.write( bWrite[x] ); // writes the bytes
          }
          os.close();
    
          InputStream is = new FileInputStream("C:\\Temp\\test.txt");
          int size = is.available();
          
          for(int i=0; i< size; i++){
             System.out.print((char)is.read()+ " ");       
          }
          is.close();
       }catch(IOException e){
          System.out.print("Exception");
       }
       }
       
    
    }
    Last edited by Eranga; 12-19-2010 at 05:32 AM. Reason: code tags added

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default a freebee

    The code is working perfectly.
    You have to learn more about how to present data.
    (which is probably what this assignment is about)

    I hope you do not come to rely on this forum
    to solve such simple problems for you.


    Java Code:
    /**
    *
    * @author Admin
    */
    import java.io.*;
    
    public class fileStreamTest{
    
      public static void main(String args[]){
    
        try{
          byte bWrite [] = {11,21,3,40,5};
          OutputStream os = new FileOutputStream("C:\\Documents and Settings\\Paul\\Desktop\\test.txt");
          for(int x=0; x < bWrite.length ; x++){
            os.write( bWrite[x] ); // writes the bytes
          }
          os.close();
    
          InputStream is = new FileInputStream("C:\\Documents and Settings\\Paul\\Desktop\\test.txt");
          int size = is.available();
    
          for(int i=0; i< size; i++){
            //System.out.print((char)is.read()+ " "); 
            System.out.print(Integer.toString(is.read())+" "); 
          }
          is.close();
        }catch(IOException e){
          System.out.print("Exception");
        }
      }
    }
    And please use code tags next time you post a problem on the forum.
    Last edited by paul pasciak; 12-17-2010 at 06:13 PM. Reason: To correct my indentation mistake

  3. #3
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    I have reconsidered my brief remarks on your
    posting.

    I see that this assignment offers an oppertunity
    to explore many possibles solutions to a simple
    problem.

    The following is an alternate solution to the
    problem you posted.

    I am almost certain that there is a third possibility..

    Java Code:
    /**
    *
    * @author Admin
    */
    import java.io.*;
    
    public class fileStreamTest{
    
      public static void main(String args[]){
    
        try{
          byte bWrite [] = {11,21,3,40,5};
          OutputStream os = new FileOutputStream("C:\\Documents and Settings\\Paul\\Desktop\\test.txt");
          for(int x=0; x < bWrite.length ; x++){
            os.write( bWrite[x] ); // writes the bytes
          }
          os.close();
    
          InputStream is = new FileInputStream("C:\\Documents and Settings\\Paul\\Desktop\\test.txt");
          int size = is.available();
    
          for(int i=0; i< size; i++){
            //System.out.print((char)is.read()+ " "); 
            //System.out.print(Integer.toString(is.read())+" "); 
            System.out.print((is.read() & 0xf)+"   "); 
          }
          is.close();
        }catch(IOException e){
          System.out.print("Exception");
        }
      }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    @OP, please use code tags when you are posting code segments in the forum. Unformated codes are really hard to read.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    convert the binary to decimal format after reading, chars are essentially decimal numbers...

  6. #6
    edcaru is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default File io

    Thank you everybody for your immediate help. Paul, excuse me for posting simple problems but I am trying to teach myself and have no other sources.

    My difficulty is that I am not understanding why the progrum returns byte instead of character data.

    XML Code:
    System.out.print((char)is.read()+ " ");
    the line above should cast the result of
    XML Code:
    is.read()
    as a char but it is not working in my case. The commented line underneath works but is not the original solution.

    XML Code:
    //System.out.print(new String(is.read()+ " "));
    I am running this code in NetBeans and the output i am getting is symbols, which I opened with a hex editor and found that it is the binary representation of the four numbers 11,21,3,40,5 which I included in the byte array.

    XML Code:
     byte bWrite [] = {11,21,3,40,5};
    I would appreciate if someone will clarify this for me.
    Thank you.
    Last edited by edcaru; 12-19-2010 at 06:58 PM. Reason: for got to enter comment

Similar Threads

  1. Input output without string class
    By surenq1 in forum New To Java
    Replies: 4
    Last Post: 06-13-2010, 04:18 PM
  2. Replies: 8
    Last Post: 05-12-2010, 08:19 PM
  3. help with data output, input
    By collin389 in forum New To Java
    Replies: 3
    Last Post: 11-11-2009, 06:34 PM
  4. Replies: 1
    Last Post: 08-04-2009, 01:48 PM
  5. Single XSD Creation for input/output
    By lavanya82 in forum XML
    Replies: 0
    Last Post: 03-13-2009, 01:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •