i am making a client server program in which client request a filename to server and server search that file and then send that file to client if found. In my case server is my computer. So, is there any method using which i can search that file in my whole computer in less time.
Asked
Active
Viewed 164 times
-3
-
possible duplicate of http://stackoverflow.com/questions/15624226/java-search-for-files-in-a-directory – Anarki Apr 03 '15 at 10:16
-
But i dont want to enter the directory name – rocky Apr 03 '15 at 10:18
-
So set your root directory as a default. – pomkine Apr 03 '15 at 10:20
-
Can you show us what did you try ? – Anarki Apr 03 '15 at 10:20
-
You could use a FileVisitor http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html – Tesseract Apr 03 '15 at 10:22
1 Answers
0
this is example for TCP Server in java:
public class TCPServer {
public static void main(String argv[]) {
String clientSentence;
//String capitalizedSentence;
ServerSocket welcomeSocket = null;
int port = 0;
try {
//port = Integer.valueOf(argv[0]);
port = 6868;
} catch (ArrayIndexOutOfBoundsException aio) {
System.out.println("Insert port");
System.exit(1);
} catch (NumberFormatException nfe) {
System.out.println("Argument must be a number");
System.exit(1);
}
try {
welcomeSocket = new ServerSocket(port);
System.out.println("started TCP listening on " + String.valueOf(port));
} catch (IOException ioe) {
System.out.println("Could not listen on TCP port = " + String.valueOf(port));
ioe.printStackTrace();
}
try {
if (welcomeSocket != null) {
while (true) {
Socket connectionSocket = welcomeSocket.accept();
System.out.println("connected-----------------------");
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
while ((clientSentence = inFromClient.readLine()) != null) {
System.out.println("File name: " + clientSentence);
if (clientSentence.equals("filename")) {
outToClient.writeBytes("this file exists\n");
}
}
inFromClient.close();
connectionSocket.close();
System.out.println("Disconnected-------------------");
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
welcomeSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
this is client for sending filename and listening server reply.For example file path which you found by file name:
public class TCPRequester {
public static String sendRequestToServer(String serverIp, int serverPort, String command) throws Exception {
StringBuilder result = new StringBuilder();
// Create input and output streams to read from and write to the server
Socket socket = new Socket(serverIp, serverPort);// Connect to the server
PrintStream out = new PrintStream(socket.getOutputStream()); // Create output streams to write to the server
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//Create input streams to read from the server
out.write(command.getBytes());
// Read data from the server until we finish reading the document
String line;
while ((line = in.readLine()) != null) {
result.append(line);
System.out.println( line );
}
System.out.println("finished response");
// Close our streams
in.close();
out.close();
socket.close();
return result.toString();
}
public static void main(String[] args) {
try {
System.out.println(sendRequestToServer("localhost", 6868, "filename\n"));
} catch (Exception ex) {
Logger.getLogger(TCPRequester.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

Sarkhan
- 1,281
- 1
- 11
- 33