Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Distance Learning Student Help with DataInputStream

Thread view: 
Yogi_Bear_79 - 04 Mar 2006 04:26 GMT
I am taking a distance learning class on JAVA. I am stuck with the
following.  My code appears to work correclty, until it comes to the
DataInputStream. I am unable to figure out how to print the stream to my
console. I am reading a simple txt file of words (one per line). The Problem
could also be in the DataOutputStream, but I think I got that part correct.
Any help would be most appriciated.

import java.util.*;

import java.io.*;

public class PipeDemo{

public static void main(String args[]){

try{

PipedOutputStream out = new PipedOutputStream();

PipedInputStream in = new PipedInputStream(out);

Producer prod = new Producer(out);

Consumer cons = new Consumer(in);

prod.start();

cons.start();

}

catch (IOException e){System.out.println("Error: " + e);}

}

}

class Producer extends Thread{

private DataOutputStream out;

public Producer(OutputStream os){

out = new DataOutputStream(os);

}

public void run(){

try{

FileInputStream inputFile = new FileInputStream( "words.txt" );

InputStreamReader in = new InputStreamReader( inputFile );

BufferedReader br = new BufferedReader(in);

String input;

while ((input = br.readLine()) != null){

out.writeChars(input);

}

br.close();

}

catch(Exception e){System.out.println("Error: " + e);}

}

}

class Consumer extends Thread{

private DataInputStream in;

public Consumer(InputStream is)

{

in = new DataInputStream(is);

}

public void run(){

System.out.println(in.readLine);

}

}
Rhino - 04 Mar 2006 04:52 GMT
>I am taking a distance learning class on JAVA. I am stuck with the
>following.  My code appears to work correclty, until it comes to the
[quoted text clipped - 90 lines]
>
> }

Have you looked at the I/O chapter in the Java Tutorial? It might answer
your questions. The URL is
http://java.sun.com/docs/books/tutorial/essential/io/index.html.

--
Rhino
Roedy Green - 04 Mar 2006 10:17 GMT
>The Problem
>could also be in the DataOutputStream, but I think I got that part correct.
>Any help would be most appriciated.

DataOutputStreams write binary, incomprehensible to humans, but
beloved of computers.  If you want to make something for humans see
http://mindprod.com/applets/fileio.html

Tell it you want to write to the console and it will generate you
sample code.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.