aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I need help" Watch "I need help" New topic
Author

I need help

Sreeraj Harilal
Ranch Hand

Joined: May 09, 2006
Posts: 45
import java.util.*;
import java.io.*;
class Mes
{
public static void main(String[] args)
{
String s="";
Reader r = new Reader(s);
Thread tr = new Thread(r);
tr.start();
Writer w = new Writer(s);
Thread tw = new Thread(w);
tw.start();
}
}

class Reader implements Runnable
{
String msg;
Reader(String s)
{
msg = s;
}
public void run()
{
while (true)
{
synchronized(msg)
{
if (msg.equals(""))
{
try
{
msg.wait();
}
catch (Exception e)
{
e.printStackTrace();
}

}
//System.out.println(msg);
try
{
BufferedWriter br = new BufferedWriter(new FileWriter("messages.txt"));
br.write(msg);
br.flush();
br.close();
msg = "";
msg.notify();
}
catch (Exception e)
{
}

}
}
}
};

class Writer implements Runnable
{
String msg;
Writer(String s)
{
msg = s;
}
public void run()
{
while (true)
{
synchronized(msg)
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
msg = br.readLine();
if (!msg.equals(""))
{
msg.notify();
msg.wait();
}
}
catch (Exception e)
{
}
}
}
}
};


In the xecution i type 2,3 lines in the command line but file "message.txt" is still empty. Why?


SCJP 5.0<br />SCWCD 1.4<br />SCBCD 1.3<br />SCEA part 1
Sreeraj Harilal
Ranch Hand

Joined: May 09, 2006
Posts: 45
I changed the code. Now it work properly.javascript: x()
jumpingjoy

import java.util.*;
import java.io.*;
class Mes
jumpingjoy
{
public static void main(String[] args)
{
StringBuffer s = new StringBuffer("");
Reader r = new Reader(s);
Thread tr = new Thread(r);
tr.start();
Writer w = new Writer(s);
Thread tw = new Thread(w);
tw.start();
}
}

class Reader implements Runnable
{
StringBuffer msg;
Reader(StringBuffer s)
{
msg = s;
}
public void run()
{
while (true)
{
synchronized(msg)
{
if (msg.length()==0)
{
try
{
msg.wait();
}
catch (Exception e)
{
e.printStackTrace();
}

}
//System.out.println(msg);
try
{
BufferedWriter br = new BufferedWriter(new FileWriter("messages.txt",true));
br.write(msg.toString());
br.write("\n");
br.flush();
br.close();
msg.delete(0,msg.length());
msg.notify();
}
catch (Exception e)
{
}

}
}
}
};

class Writer implements Runnable
{
StringBuffer msg;
Writer(StringBuffer s)
{
msg = s;
}
public void run()
{
while (true)
{
synchronized(msg)
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
s=br.readLine();
msg.append(s);
if (!msg.equals(""))
{
msg.notify();
msg.wait();
}
}
catch (Exception e)
{
}
}
}
}
};
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I need help