Hello,
I am having a problem writing to a file on a client/server application. I can send the data from a JTextArea to the server, it appears to read the text, but I cannot seem to get the server to write the data to the .txt file. Can anyone help? Here is the server code:
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
public class Server extends JFrame
{
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
private int counter = 1;
// set up GUI
public Server()
{
super( "Server" );
displayArea = new JTextArea();
add(new JScrollPane( displayArea ), BorderLayout.CENTER );
setSize( 400, 200 );
setVisible( true );
}
// run server
public void runServer()
{
// receive and process connections
try
{
server = new ServerSocket( 12345, 100 );
while ( true )
{
try
{
// wait for connection
waitForConnection();
getStreams();
processConnection();
}
catch ( EOFException eofException )
{
displayMessage( "\nServer terminated connection" );
}
finally
{
closeConnection();
counter++;
}
}
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}
// wait for connection, display connection info
private void waitForConnection() throws IOException
{
displayMessage( "Waiting for connection\n" );
connection = server.accept();
displayMessage( "Connection " + counter + " received from: " +
connection.getInetAddress().getHostName() );
}
private void getStreams() throws IOException
{
// output stream
output = new ObjectOutputStream( connection.getOutputStream() );
// flush output buffer
output.flush();
// input stream
input = new ObjectInputStream( connection.getInputStream() );
displayMessage( "\nI/O streams OK\n" );
}
private void processConnection() throws IOException
{
String message = "Connection successful";
String command;
String filename;
sendData( message );
// set text field editable
setTextFieldEditable( true );
do
{
try
{
message = ( String ) input.readObject();
command = message.split( "" )[0];
filename = message.split( "" )[1];
displayMessage( "\n" + message );
if (command.equals( "open" ) )
{
getFile( filename );
} else if ( command.equals("save") )
{
saveFile( filename );
}
}
catch ( ClassNotFoundException classNotFoundException )
{
displayMessage( "\nUnknown object type received" );
}
} while ( !message.equals( "CLIENT>>> TERMINATE" ) );
}
private void closeConnection()
{
displayMessage( "\nTerminating connection\n" );
// set text field not editable
setTextFieldEditable( false );
try
{
output.close();
input.close();
connection.close();
}
catch ( IOException ioException )
{
ioException.printStackTrace();
}
}
private void sendData( String message )
{
try
{
output.writeObject( "SERVER>>> " + message );
// flush output
output.flush();
displayMessage( "\nSERVER>>> " + message );
}
catch (IOException ioException)
{
displayArea.append( "\nError writing object" );
}
}
// display message
private void displayMessage( final String messageToDisplay )
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
displayArea.append( messageToDisplay );
}
}
);
}
private void setTextFieldEditable( final boolean editable )
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
enterField.setEditable( editable );
}
}
);
}
// get file
public void getFile( final String location ) throws IOException
{
BufferedReader read = null;
String record = "";
try {
// specify file
File m = new File( location );
FileInputStream finput = new FileInputStream( m );
BufferedInputStream binput = new BufferedInputStream( finput );
read = new BufferedReader(new InputStreamReader( binput ) );
output.writeObject( "SERVER>>> Display requested file." );
output.writeObject( "SERVER>>> File: " + location + "\n" );
output.flush();
while ( (record = read.readLine() ) != null )
{
output.writeObject( record + "\r\n" );
}
output.flush();
read.close();
}
catch (IOException ioException)
{
displayArea.append( "\nSERVER>>> Error retrieving requested file: "
+ location + "\n" );
output.writeObject( "\nSERVER>>> Error retrieving requested file: "
+ location + "\n" );
output.flush();
JOptionPane.showMessageDialog( this,
"Error retrieving requested file", "Bad file name.",
JOptionPane.ERROR_MESSAGE );
}
finally
{
if ( read != null )
{
try
{
read.close();
} catch ( IOException ioe )
{
}
}
}
}
private void saveFile( String filename )
{
String message = "";
try
{
BufferedWriter bw = new BufferedWriter( new FileWriter( filename ) );
do
{
try
{
message = ( String ) input.readObject();
if ( !message.equals( "EOF" ) )
{
bw.newLine();
bw.write( message );
bw.newLine();
}
bw.close();
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
while ( !message.equals( "EOF" ) );
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
}
Thank you.
Hello, Allen.
You close your BufferedWriter bw in the cycle (when you are writing data into the file). So in the best case you'll have an empty file ... in worst - an exception.
Just take it out of the cycle.