Using the Stream Classes

T

Tony_P

HI, Can somebody give me some help as it's going badly wrong for at moment

Could some gives a hint or two where, & what i am going wrong...

Tony_P :))
----------------------------------------------------------
THIS HOW I MUST DO THIS
Using the Stream Classes
--------------------------
I have got modify my SendAndClearHandler.class
Add a field of type DataInputStream, then add an OutputStream argument to
your constructor, chain the OutputStream argument to my DataOutputStream.

Then modify my actionPerformed() method so that text in the TextField is
sent to the DataOutputStream using writeUTF() method.
clear text as before, but do not append the String to the TextArea, This
will be done somewhere else.

Then I have got modify my MessageReader.class
Add a field of type DataInputStream, then add an InputStream argument to
your constructor, chain the InputStream argument to my DataInputStream.
within the Run() method, use readUTF() method of my DataInputStream field to
read in a String from the stream.
Instead of appending "Hello, Thread" to TextArea, append the String from the
call to readUTF().


Then I have got modify my ChatWindow.class
Before instantiating your MessageReader and SendAndClearHandler objects,
creata a File object associated with a file called "chat.txt".
Instantiate a FileOutputStream & FileInputStream object using you File
object.

Use the FileOutputStream object as the argument to the constructor
when instantiating your SendAndClear object

Use the FileInputStream object as the argument to the constructor
when instantiating your MessageReader object

Save, compile and run the ChatWindow program.

WHAT YOU SHOULD SEE
-------------------
Typing in the TEXTFIELD and clicking "Send" will send the String to a file.
This file is being read by a thread that places the string into your
TextArea

---------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SendAndClearHandler implements ActionListener
{
private TextArea ta2;
private TextField tf2;
// Declaring Field of type DataOutputStream
private DataOutputStream dataout;
//Adding outstr argument to constructor
public SendAndClearHandler(TextArea ta2, TextField tf2, OutputStream
outstr, FileOutputStream fileout)
{
// chainning DataOutputStream to outstr argument
this.dataout = new DataOutputStream(outstr);
this.ta2 = ta2;
this.tf2 = tf2;
}

public void actionPerformed(ActionEvent a)
{
if(a.getActionCommand() == "Send" || a.getSource() == tf2)
{
try
{
dataout.writeUTF(tf2.getText() + "\n");
dataout.close();
}catch (IOException e)
{
e.printStackTrace();
}
}
else if(a.getActionCommand() == "Clear"){ta2.setText("");
{
try{
dataout.close();
}catch(IOException e){
e.printStackTrace();
}
}
//tf2.setText();

}
else if(a.getActionCommand() == "Enter" || a.getSource() == tf2)
{
try
{

dataout.writeUTF(tf2.getText()+ "\n");
dataout.close();
}catch (IOException e)
{
e.printStackTrace();
}
}
}
}
---------------------------------------------------------
import java.awt.TextArea;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class MessageReader implements Runnable
{
private TextArea ta4;
//Declaring Field of type DataInputStream
private DataInputStream datain;
public MessageReader(TextArea ta4, InputStream instr, FileInputStream
filein)
{
// chainning DataInputStream to InputStream args...
this.datain = new DataInputStream(instr);
// creating txt area number 4
this.ta4 = ta4;
}

public void run()
{
while(true)
{
try
{
DataInputStream ta4 =
new DataInputStream(datain);
String in = ((DataInputStream) ta4).readUTF();
System.out.println(in + "Hello, Thread");
datain.readUTF();
datain.close();
}catch(IOException e)
{
e.printStackTrace();
}
this.ta4.setText(ta4.toString());
/* THIS THREAD WILL SLEEP FOR 50 SECONDS
*
* NUMBER IN MILLISECONDS >-- 5000
*/

// TRY/CATCH BLOCK HERE
try
{
Thread.sleep((long)(Math.random()*5000 + 1));
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
----------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ChatWindow extends Frame
{
private Button clear;
private Button send;
private Button quit;
private TextField tf1;
private Choice combo;
private TextArea ta1;


public ChatWindow(String title)
{
super(title);
/*
* (non-Javadoc)
*
* pan1, 1st panel
*/
Panel pan1 = new Panel();
/*
* (non-Javadoc)
*
* pan2, 2nd panel
*/
Panel pan2 = new Panel();
/*
* (non-Javadoc)
*
* This code is setting panel 2, to borderlayout manager
*/
pan2.setLayout(new BorderLayout ());
/*
* (non-Javadoc)
*
* This line of code is setting panel 1, to Gridlayout manager
*/
pan1.setLayout(new GridLayout (3,1));
/*
* (non-Javadoc)
*
* This line of code is declairing main txt field
*/
tf1 = new TextField(80);
/*
* (non-Javadoc)
*
* This line of code is declairing choice list
*/
combo = new Choice();
/*
* (non-Javadoc)
*
* This code is adding three buttons to 1st panel
*/
clear = new Button ("Clear");
pan1.add(clear,
BorderLayout.WEST);
send = new Button ("Send");
pan1.add(send,
BorderLayout.WEST);
quit = new Button ("quit");
pan1.add(quit,
BorderLayout.WEST);

/*
* (non-Javadoc)
*
* This line of code is adding choice list to 2nd panel
*/
pan2.add(combo, BorderLayout.WEST);
/*
* (non-Javadoc)
*
* This code is adding choice box options
*/
combo.addItem("brb");
combo.addItem("lol");
combo.addItem("cul8r");
/*
* (non-Javadoc)
*
* This code is adding a text field to 2nd panel
*/
pan2.add(tf1, BorderLayout.CENTER);
/*
* (non-Javadoc)
*
* This line of code is declairing text area
*/
ta1 = new TextArea();
this.add(pan1, BorderLayout.WEST);
this.add(ta1, BorderLayout.CENTER);
this.add(pan2, BorderLayout.SOUTH);

/*
* (non-Javadoc)
*
* LAB 6_2
*
* Creating an instance QuitChat
*/
QuitChat qc = new QuitChat();
/*
* (non-Javadoc)
*
* This line of code is adding Frame to ActionListener, by calling
* it this
*/
this.addWindowListener(qc);
/*
* This line of code is adding quit to ActionListener
*/
quit.addActionListener(qc);
/*
* (non-Javadoc)
*
* Creating an instance of SendAndClearHandler
* With two arguments TextArea & TextField
*/
SendAndClearHandler sch = new SendAndClearHandler(this.ta1,
this.tf1, null, null);
/*
* (non-Javadoc)
*
* Creating an instance of ChoiceHandler
* With one argument TextArea
*/
ChoiceHandler ch = new ChoiceHandler(this.ta1);
/*
* (non-Javadoc)
*
* This line of code is adding TextField to a listener,
* & then adding handler
*/
this.tf1.addActionListener(sch);

this.combo.addItemListener(ch);
/*
* (non-Javadoc)
*
* This line of code is adding send to a listener,
* & then adding handler
*/
send.addActionListener(sch);
/*
* (non-Javadoc)
*
* This line of code is adding clear to a listener,
* & then adding handler
*/
clear.addActionListener(sch);
/*
* (non-Javadoc)
*
* Local variable
*/
Button enter = send;
/*
* (non-Javadoc)
*
* This line of code is adding enter to a listener,
* & then adding handler
*/

enter.addActionListener(sch);

// Instantiating an MessageReader object
MessageReader mr = new MessageReader(ta1, null, null);
Thread t1 = new Thread(mr);

t1.start();
//...
try
{
File file = new File("chat.txt");

FileOutputStream fileout = new FileOutputStream(file);
FileInputStream filein = new FileInputStream(file);
}catch(IOException e){
e.printStackTrace();
}
}
/*
* (non-Javadoc)
*
* Declairing main
*/
public static void main(String [] args)
{
ChatWindow cw = new ChatWindow("Hello, ChatWindows");
cw.setSize(500,400);
cw.setVisible(true);
}
}
 
P

PofN

Tony_P said:
HI, Can somebody give me some help as it's going badly wrong for at moment

At the moment? Shirly. You are posting the same lab exercise a.k.a.
homework for two weeks in a row now. To multiple Java groups. You don't
even manage do disguise the text of the original homework assignment
very well. You have apparently no idea what the original code does.
Where did you copy it from?

You think you can fool the people here? The only one you are fooling is
you.

Ralph
 
T

Tony_P

not at all i didn't copy the code. i put the code in there so shut up
pal!!!, it's called java programming you know class must have main method,
and ain't trying to fool anyone just trying to sort the error in my code!!!
Yes i have an idea what code does, but yer i rubbish at following logic...

Tony_P
----------------------------------------------------------
 
T

Tony_P

For anybody who is intrested in what is happening in my code....

You send the FileOutputStream to the SendAndClearHandler and the
FileInputStream to the MessageReader.The SendAndClearHandler creates a
DataOutputStream from the FileOutputStream and the MessageReader creates a
DataInputStream from the FileInputStream.

Tony_p
---------------------------------------------------------------------------------------
 
J

James Westby

Tony_P wrote:
[snip]
public SendAndClearHandler(TextArea ta2, TextField tf2, OutputStream
outstr, FileOutputStream fileout)
{
// chainning DataOutputStream to outstr argument
this.dataout = new DataOutputStream(outstr);
this.ta2 = ta2;
this.tf2 = tf2;
} [snip]
private TextArea ta4;
//Declaring Field of type DataInputStream
private DataInputStream datain;
public MessageReader(TextArea ta4, InputStream instr, FileInputStream
filein)
{
// chainning DataInputStream to InputStream args...
this.datain = new DataInputStream(instr);
// creating txt area number 4
this.ta4 = ta4;
} [snip]
/*
* (non-Javadoc)
*
* Creating an instance of SendAndClearHandler
* With two arguments TextArea & TextField
*/
SendAndClearHandler sch = new SendAndClearHandler(this.ta1,
this.tf1, null, null); [snip]
// Instantiating an MessageReader object
MessageReader mr = new MessageReader(ta1, null, null);
Thread t1 = new Thread(mr);

t1.start();
//...
try
{
File file = new File("chat.txt");

FileOutputStream fileout = new FileOutputStream(file);
FileInputStream filein = new FileInputStream(file);
}catch(IOException e){
e.printStackTrace();
}
[snip]

You are creating instances of your classes with null arguments for the
streams, probably not a good idea if you want to use streams within the
classes. You create file streams later in your code but do not use them
anywhere.


James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top