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 5 of 5

Thread: i/o classes question

  1. #1
    Junior Member p0oint's Avatar
    Join Date
    Jun 2010
    Location
    Macedonia
    Posts
    10
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default i/o classes question

    /*
     
    package IOStreamDemo;
    import java.io.*;
     
    public class IOStreamDemo{
     
     
        public static void main(String[] args) throws IOException {
            // TODO code application logic here
            try {
    DataOutputStream out2 =
    new DataOutputStream(
    new BufferedOutputStream(
    new FileOutputStream("Data.txt")));
    out2.writeDouble(3.14159);
    out2.writeChars("That was pi\n");
    out2.writeBytes("That was pi\n");
    out2.close();
    FileInputStream in4=new FileInputStream("Data.txt");
    DataInputStream in5 =new DataInputStream(
    new BufferedInputStream(
    new FileInputStream("Data.txt")));
    BufferedReader in5br =
    new BufferedReader(
    new InputStreamReader(in5)); //[B]What if I use i4 in constructor????[/B]
    // Must use DataInputStream for data:
    System.out.println(in5.readDouble());
    // Can now use the "proper" readLine():
    System.out.println(in5br.readLine());
    // But the line comes out funny.
    // The one created with writeBytes is OK:
    System.out.println(in5br.readLine());
    } catch(EOFException e) {
    System.err.println("End of stream");
    }
     
        }
    }

    I get different outputs, the first one is:

    3.14159
    T h a t w a s p i
    That was pi.

    the second output with i4 is:

    3.14159
    @	!щр†n T h a t w a s p i
    That was pi.


    What is the difference?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,621
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: i/o classes question

    Where/what is i4?

    What is in the input file?

    Please put your posted code in code tags. Info here: Java Forums - BB Code List

  3. #3
    Junior Member p0oint's Avatar
    Join Date
    Jun 2010
    Location
    Macedonia
    Posts
    10
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: i/o classes question

    Hello!

    FileInputStream in4=new FileInputStream("Data.txt"); //before bolded part


    I put my code in [cоde][/cоde] таг.

    The input file is "Data.txt"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,621
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: i/o classes question

    What is in the input file?
    The name of the input file is not really relevant.

    Can you change your program so it reads and prints with one class and then reads and prints with another class all in one program and print out headers describing which classes were used for each reading and output?

  5. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: i/o classes question

    I put my code in [cоde][/cоde] таг.
    What's up with the random Cyrillic, are you using a translator?

Similar Threads

  1. noob question on abstract classes
    By joshred in forum Object Oriented Programming
    Replies: 1
    Last Post: September 15th, 2010, 06:27 PM
  2. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM
  3. A Question Regarding Abstract Classes & Packages
    By Kerubu in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 12:27 PM
  4. connecting two classes?
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 1st, 2009, 03:15 PM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM

Tags for this Thread