Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.security.ntlm.Server;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class ispit {
- public static void main(String[] args) throws IOException {
- TCPClient c1 = new TCPClient("C:\\Users\\Ivana\\Desktop\\os\\cita" , "C:\\Users\\Ivana\\Desktop\\os\\zapisuva");
- c1.start();
- }
- }
- class TCPClient extends Thread
- {
- File inputFile;
- File outputFile;
- BufferedWriter writer;
- Socket socket;
- public TCPClient(String folderToScan, String folderTxtOutput) throws IOException {
- inputFile = new File(folderToScan);
- outputFile = new File(folderTxtOutput + "/files.csv");
- writer = new BufferedWriter(new FileWriter(outputFile));
- socket = new Socket("localhost", 9876);
- }
- private void Rekurzija(File fol) throws IOException {
- File[] files = fol.listFiles();
- for(File f: files)
- {
- if(f.isFile())
- {
- if(f.getName().endsWith(".txt") || f.getName().endsWith(".dat") && f.length() < 512*1024)
- {
- System.out.println("Najdov txt file");
- writer.write(f.getAbsolutePath() + "," + f.length() + "\n");
- }
- }
- else
- Rekurzija(f);
- }
- }
- @Override
- public void run() {
- try {
- Rekurzija(inputFile);
- writer.close();
- System.out.println("Prakam !");
- DataOutputStream out = new DataOutputStream(socket.getOutputStream());
- out.writeLong(outputFile.length());
- out.writeLong(outputFile.lastModified());
- out.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- class TCPServer
- {
- private ServerSocket server;
- public TCPServer() throws IOException {
- this.server = new ServerSocket(9876);
- }
- public void listen() throws IOException {
- Socket client = server.accept();
- ServerWorkerThread thread = new ServerWorkerThread(client);
- thread.start();
- }
- }
- class ServerWorkerThread extends Thread{
- BufferedWriter writer;
- File folder = new File("C:\\Users\\Ivana\\Desktop\\os\\zapisuva\\data.txt");
- Socket client;
- ServerWorkerThread(Socket client) throws IOException {
- this.client = client;
- writer = new BufferedWriter(new FileWriter(folder));
- }
- @Override
- public void run() {
- try{
- BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
- String data = null;
- while((data = reader.readLine()) != null)
- {
- writer.write(client.getInetAddress() +" " + client.getPort() + " " + data);
- }
- writer.close();
- }
- catch(Exception ex){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement