• Post Reply Bookmark Topic Watch Topic
  • New Topic

compare file names of 2 dir pleas help  RSS feed

 
Masoud shirzay
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am actually building a client server application. which compares the file names of the client directory (C:\Program Files\MS Patches)with file name of the server directory (C:\Program Files\MS Patches). "these 2 directories are hard coded you may obviously have to change it if you want to run the program".
and then prints the file names which are not on the client directory.
here is the code
client

import javax.swing.*;
import java.net.*;//this is for sockets
import java.io.*;// java reads and writes to sockets the same way it does to files

public class client {

String name = "C:/JDK";

public String getName(){

return name;

}

public static void display(String mesg) {
JOptionPane.showMessageDialog(null,mesg,"LOTTERY_DRAW",JOptionPane.INFORMATION_MESSAGE);
}
public static void main (String args[]) throws IOException {


Socket server = new Socket("Localhost",3500);//Opens the socket number 3500
InetAddress address=InetAddress.getLocalHost();//get localhost address

BufferedReader in = new BufferedReader //create a BufferedReader for reading incoming messages
(new InputStreamReader(server.getInputStream()));
PrintWriter out = new PrintWriter //create a printwriter for output stream
(server.getOutputStream(), true);

System.out.println("Connected to "+server.getInetAddress());//display the host that it is connected to
System.out.println("Connected to "+address);//print the locat host address

String mesg1 = in.readLine();//read in the message sent by the server
System.out.println(""+mesg1);//display the message received

out.println("Yes I want to update");//send to the sever

String s = in.readLine();//read in the message received from server using readLine

System.out.println("SEVER MESSAGE TO CLIENT \n"+s);//print the message received from the serve
//Start building the SEARCH program

File dir = new File("C:/JDK/MyWork/Project/clFolder");

String[] children = dir.list();
if (children == null) {
// Either dir does not exist or is not a directory
JOptionPane.showMessageDialog(null,"No such directory exist");

} else {
for (int i=0; i<children.length; i++) {
// Get filename of file or directory
String filename = children[i];
System.out.println("files are: "+i+"- "+filename);
out.println(filename);
}
}



out.close();

System.exit(0);//exit the program


}
}



server

import java.net.*;//this is for sockets
import java.io.*;// java reads and writes to sockets the same way it does to files
import java.util.*;//java utility
import javax.swing.*;

public class server {

public static void displayMessage(String mesg)
{
JOptionPane.showMessageDialog(null,mesg,"RESULT",JOptionPane.INFORMATION_MESSAGE);
}

public static void main (String args[]) throws IOException {
int port = 3500; //the port number to connect to



int k,arr=100;
String sfiles = "", cfiles="", missing="";
final String files[]=new String[arr];


ServerSocket server = new ServerSocket(port);//establish a server that monitors a port eg: 3500
InetAddress address=InetAddress.getLocalHost();//get localhost address IP and Host name

System.out.println("\nIM THE SERVER WAITING FOR CLIENT\n");//print a message

while (true) {//trow

Socket client = server.accept();//accept the connection when connected
System.out.println("Client from "+client.getInetAddress()+" connected.");

BufferedReader in = new BufferedReader//create a buffer reader for incomming messages
(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter //create printwriter for client's output stream
(client.getOutputStream(), true);

String mesg = "SERVER SAYS: Your Security updat status is unknown YOU NEED TO BE UPDATED";//message as string created
out.println(mesg);//send message to client
String message = "Message from me the SERVER\n\n";//create a message as string
message += "Your security status is unknown\n\n";
message += "Your computer is at RISK !!\n\n";
message += "TO UPDATE NOW PRESS 'OK' !!";
JOptionPane.showMessageDialog(null,message);//print message on message dialog (pop up window)

String s = in.readLine();//read in the message sent by the client
System.out.println("I AM THE SERVER I RECEIVE THE BOTTOM LINE FROM THE CLIENT\n\n"+s);//print the message received from

the client

out.println("SERVER SAYS: 'YOUR STATUS WILL BE CHECKED NEXT TIME YOU CONNECT TO THE NETWORK'");//server message to client


//read file name sent by the client display them
int n=0;

while ((files[n] = in.readLine()) != null){

//System.out.println("Client files "+files[n]);
n++;

File dir = new File("C:/JDK/MyWork/Project/SerFolder");

String[] children = dir.list();
if (children == null)
{
// Either dir does not exist or is not a directory
JOptionPane.showMessageDialog(null,"No such directory exist");

} else {
for (int j=0; j<children.length; j++) {
// Get filename of file or directory
String filename = children[j];
}}

for (k=0;k<children.length;k++)
{
if(!(files.length>0))
break;
boolean found=false;

for(int l=0;l<files.length;l++)
{
if(children[k].equals(files[l]))
{
found=true;
//System.out.println(" files "+files[l]);
//l++;
break;
}
}


if (!found)

System.out.println("missing files "+children[k]);


}


}
System.exit(0);//exit the program
}

}
}


the part I am having trouble with is:
i want it to print those files which does not exist on the client side. but it just prints the server and client files. basically problem with this part:


for (k=0;k<children.length;k++)
{
if(!(files.length>0))
break;
boolean found=false;

for(int l=0;l<files.length;l++)
{
if(children[k].equals(files[l]))
{
found=true;
//System.out.println(" files "+files[l]);
//l++;
break;
}
}


if (!found)

System.out.println("missing files "+children[k]);//should only print those files which are not on the client directory.


}

thanks


[ March 20, 2005: Message edited by: Masoud shirzay ]

[ March 21, 2005: Message edited by: Masoud shirzay ]
[ March 21, 2005: Message edited by: Masoud shirzay ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. What part are you having trouble with?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Masoud shirzay --

You keep sending me private email, and I've already asked you to keep the discussion here in this thread. Post a reply in this thread which further explains what help you need; don't just edit your older post, which just makes things more confusing. Furthermore, posting a reply will pop your thread up to the top of the forum again. Tell us what part of the task, or what part of the code, presents a problem for you.

Also, please use UBB CODE tags to format your code, so we can read it!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!