5 Replies - 472 Views - Last Post: 18 August 2012 - 01:05 PM Rate Topic: -----

#1 alpha_x  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 03-May 11

How to clone a file in java

Posted 18 August 2012 - 12:09 PM

Hey all, I am attempting to write a chat client and to solve a problem so that if multiple users connect to the server, I need a way for each client to correctly reflect what the server has received. My solution albeit perhaps a bit crude is to have the server save whatever it receives to a text file like so:

public void recieve() throws IOException {
		out = new PrintWriter(clientSocket.getOutputStream(), true);
		in = new BufferedReader(new InputStreamReader(
				clientSocket.getInputStream()));

		while ((inputLine = in.readLine()) != null) {

			out.println(outputLine); // send data back to the client
			writeFile();
			System.out.println("Recieved: " + inputLine); // printout to make
															// sure data was
															// received
			if (inputLine.equals("exit"))
				break;
		}

	}

	public void writeFile() {
		try {
			// Create file
			FileWriter fstream = new FileWriter("out.txt", true);
			BufferedWriter outb = new BufferedWriter(fstream);
			outb.append(inputLine);
			// Close the output stream
			outb.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}



Then I have a method in my client class that reads what is in the file like so:

	public File current;
public void readFile() {
		BufferedReader br = null;
		try {
			String sCurrentLine;
			BufferedReader in = new BufferedReader(new FileReader(current));

			while ((sCurrentLine = in.readLine()) != null) {
				fromServer += (sCurrentLine + "\n");
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

	}



To finish this solution, I decided to create a timer in my clientGUI that constantly checks the size of the file and if the filesize has increased, it resets the JTextArea clientArea to the data in the file and this is where I have run into a bit of a snag:

Action checkServerTimer = new AbstractAction() {

		@Override
		public void actionPerformed(ActionEvent e) {
			File tempFile = (File) (myClient.current);
			long oldlength = tempFile.length();
			System.out.println("OldLength: " + oldlength);
			myClient.readFile();
			long newLength = myClient.current.length();
			System.out.println("NewLength: " + newLength);
			if (oldlength < newLength)
			clientArea.append(myClient.fromServer);
			
			System.out.println("Reached!");

		}

	};



In my client class, I update the length of the file when i re-read it, I need a way to clone the file before reading it again so that oldLength is not equal to newlength in the example above as it always is! How would I go about cloning a file or reading the length before myClient.current (which is the file) is changed after myClient.readFile()?

Sorry for the long post, Any help would be appreciated! My source is attached in a zip file.
Thanks!

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: How to clone a file in java

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1375
  • View blog
  • Posts: 3,027
  • Joined: 05-April 11

Re: How to clone a file in java

Posted 18 August 2012 - 12:18 PM

Are you not using any networking at all?
Was This Post Helpful? 0
  • +
  • -

#3 alpha_x  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 03-May 11

Re: How to clone a file in java

Posted 18 August 2012 - 12:24 PM

I am, each client sends data over a TCP connection to the server, and the server accepts these connections and handles the data. I decided to use this text file approach so that each client can see everything that the server has received from the multiple clients.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1375
  • View blog
  • Posts: 3,027
  • Joined: 05-April 11

Re: How to clone a file in java

Posted 18 August 2012 - 12:25 PM

View Postalpha_x, on 18 August 2012 - 07:24 PM, said:

I am, each client sends data over a TCP connection to the server, and the server accepts these connections and handles the data. I decided to use this text file approach so that each client can see everything that the server has received from the multiple clients.


So they can see history? Else there is no reason to store it in a file, you could just broadcast the message out to every client.
Was This Post Helpful? 1
  • +
  • -

#5 alpha_x  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 03-May 11

Re: How to clone a file in java

Posted 18 August 2012 - 12:49 PM

View PostCasiOo, on 18 August 2012 - 12:25 PM, said:

View Postalpha_x, on 18 August 2012 - 07:24 PM, said:

I am, each client sends data over a TCP connection to the server, and the server accepts these connections and handles the data. I decided to use this text file approach so that each client can see everything that the server has received from the multiple clients.


So they can see history?


Yeah, precisely so users can see old chats and I'm using files as I think using a database is a tad excessive...
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1375
  • View blog
  • Posts: 3,027
  • Joined: 05-April 11

Re: How to clone a file in java

Posted 18 August 2012 - 01:05 PM

But then why are you having your clients check a file? When a new client connects to the server, you will have the server sending the chat log to the client.

Save timestamps of log off and messages, then you wouldnt have to send all of the chat log.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1