Im just new in java and im trying to make a program that would copy a picture from a specified folder and paste it also to a specified folder.
i know how to select the picture using the filechooser and my problem is how to copy it.

i send u two code which are act as server and client just change ip address in code . first start server code then start client code.

At first client ask u the source path and destination path

Server code:-

import java.io.*;
import java.net.*;
import java.net.URL;
import java.sql.*;
 import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.io.File;
import java.io.FilenameFilter;
import java.io.FileFilter;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;


public class Server2{


    int abc =0;
    public static void main(String args[])
    {
     char port = 1088;
     Server2 server = new Server2( port );
     server.startServer();
    }

    ServerSocket echoServer = null;
    Socket clientSocket = null;
    int numConnections = 0;
    int port;

    public Server2( int port )
     {
      this.port = port;
     }

    public void stopServer()
    {
    System.out.println( "Server cleaning up." );
    System.exit(0);
    }

    public void startServer() {

        try
        {
          echoServer = new ServerSocket(port);
        }
        catch (IOException e)
        {
         System.out.println(e);
        }

        System.out.println( "Server is started and is waiting for connections." );
        //System.out.println( "With multi-threading, multiple connections are allowed." );
        //System.out.println( "Any client can send -1 to stop the server." );


      while ( true )
       {
           try {
                clientSocket = echoServer.accept();
                numConnections ++;
                Server2Connection oneconnection = new Server2Connection(clientSocket, numConnections, this);
                new Thread(oneconnection).start();
            }
         catch (IOException e) {
           System.out.println(e);
         }
       }
    }
}


  class Server2Connection implements Runnable
{
    static int k=0;
    static int indentLevel = -1;
    PrintWriter os = null;
    BufferedReader is = null;
    Socket clientSocket;
    int id;
    Server2 server;
     int abc=0;

    public void finds(String s1,Socket clientSocket)
    {

       this.clientSocket=clientSocket;
       File folder = new File(s1);
       File[] listOfFiles = folder.listFiles();

      try
     {

       for (int i = 0; i < listOfFiles.length; i++)
       {
          if (listOfFiles[i].isFile())
          {


             //System.out.println("File " + listOfFiles[i].getName());
             String filename = listOfFiles[i].getName().toString();
             System.out.println("tatal name is *******************"+filename);

                     int dotpos = filename.lastIndexOf("."); //
                     if(dotpos>0)
              {


                     String extension = filename.substring(dotpos);
                     if(extension.equals(".jpg")||extension.equals(".bmp") ||extension.equals(".BMP"))
                     {
                       k++;
                       System.out.println("VALUE OF K IS INCREAMENTED HERE" +k);
                       System.out.println("File " + listOfFiles[i].getName());
                       System.out.println("This is IMAGE file");
                       System.out.println();
                       System.out.println();
                        FileInputStream fis = new FileInputStream(listOfFiles[i].getPath().toString());
                        byte[] buffer = new byte[fis.available()];
                        fis.read(buffer);
                        ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream()) ;
                        oos.writeObject(buffer);
                      //  oos.flush();
                       // oss.flush();

                     }
              }

           }
           else if (listOfFiles[i].isDirectory())
          {
           System.out.println(" THIS IS Directory AND IT'S Contents ARE  " + listOfFiles[i].getName());
           finds(listOfFiles[i].getPath(),clientSocket);
          }
       }
     }catch(Exception Ex)
       {
         System.out.println(Ex);
        }
    }









   public void listPath(File path,Socket clientSocket)
    {
      try
      {
        System.out.println(":"+path);
        File files[];
        indentLevel++;
        this.clientSocket=clientSocket;
        files = path.listFiles();
         int ig=0;
        Arrays.sort(files);
        for (int i = 0, n = files.length; i < n; i++)
         {
               for (int indent = 0; indent < indentLevel; indent++)
               {
                    System.out.print(" Strted ");
                    String filename = files[i].toString();
                    String extension;
                    try
                {
                       int dotPos = filename.lastIndexOf(".");
                       extension = filename.substring(dotPos);
                       System.out.println("Extension is" +extension);
                       //if (extension.equals(".jpeg") || extension.equals(".jpg") || extension.equals(".gif") || extension.equals(".png") ||
                         //   extension.equals(".BMP") || extension.equals(".ico") || extension.equals(".xpm") || extension.equals(".bmp"))
                           if(extension.equals(".txt"))//||extension.equals(".JPEG")||extension.equals(".bmp")||extension.equals(".BMP")||extension.equals(".jpg"))
                           {
                            ig++;
                              System.out.println("image file are:"+filename);
                              FileInputStream fis = new FileInputStream(filename);
                              byte[] buffer = new byte[fis.available()];
                              fis.read(buffer);
                              ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream()) ;
                              oos.writeObject(buffer);
                              oos.flush();
                              System.out.println(ig+"TH image are transfersd") ;
                              abc++ ;
                              System.out.println("This is"+extension+ "file");
                              System.out.println(files[i].toString());
                            }

                 }catch
                 (Exception ex)
                   {
                    System.out.println(ex);
                   }
               }
              System.out.println(files[i].toString());
              if (files[i].isDirectory())
              {
               listPath(files[i],clientSocket);
              }
         }
         indentLevel--;
       } catch(Exception ex)
       {
                    System.out.println(ex);
       }
     }





    public Server2Connection(Socket clientSocket, int id, Server2 server)
     {
       this.clientSocket = clientSocket;
       this.id = id;
       this.server = server;
       System.out.println( "Connection " + id + " established with: " + clientSocket );
          try {

             os = new PrintWriter(new OutputStreamWriter (clientSocket.getOutputStream()));
             is = new BufferedReader(new InputStreamReader(
                                        clientSocket.getInputStream()));
            } catch (IOException e)
              {
                System.out.println(e);
              }
     }

    public void run() {
        String line = null;
        String line2= null;
      try
       {
             boolean serverStop = false;
            //********************************8 modifications*****************************8//

             do{

                    InputStream inputstream = clientSocket.getInputStream();
/*  20*/            OutputStream outputstream = clientSocket.getOutputStream();
/*  21*/            System.out.println(" testing");
/*  23*/            DataInputStream datainputstream = new DataInputStream(inputstream);
/*  24*/            DataOutputStream dataoutputstream = new DataOutputStream(outputstream);
/*  25*/            System.out.println(" testing2");
/*  26*/            Object obj = null;
/*  31*/            // do
                      // {
/*  31*/                String s45 = datainputstream.readUTF();
               String s = "C:"  ;
                        System.out.println((new StringBuilder()).append("Source path is:- ").append(s45).toString());
                        if(s.equals("0000") )
                        {
                         System.exit(0);
                        }

                       // String s2 = datainputstream.readUTF();
                        //System.out.println((new StringBuilder()).append("Destination  path is:- ").append(s2).toString());

                     //   doSimpleFileListing(s, clientSocket);
                         //if (args.length == 0)
                         //{
                       //   doFileFilterListing(s, null, clientSocket);
                          //listPath(new File(s),clientSocket);
                          finds( s, clientSocket);
                         //doFileFilterDirectoryListing(s, clientSocket);
                         //System.out.println("Total  inage files are:"+ls.pinku);
                         System.out.println("Waiting for the next employee name...");
/*  76*/                 System.out.println();
                         System.out.println("data is send by server");
                         if ( s.equals(" 111 ")) {
                          serverStop = true;
                          break;
                         }
                        if (s.equals(" 0 "))  break;

                    // } while (true);





                System.out.println(+abc+"Image(s) are send  for connection:" +id);

                os.flush();

                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy   HH:mm:ss");
                Date date = new Date();
                System.out.println("   Date******Time");
                System.out.println(""+ dateFormat.format(date));

            }while(true);

                System.out.println( "Connection " + id + " closed." );
                is.close();
                os.close();
                clientSocket.close();
               if ( serverStop ) server.stopServer();
       }
        catch (IOException e) {
         System.out.println(e);
        }
    }
 }






Client code:-


// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: [url]http://www.geocities.com/kpdus/jad.html[/url]
// Decompiler options: packimports(5) braces fieldsfirst noctor nonlb space lnc
// Source File Name:   Client.java
 //press 000 to close client and 0000 to close server
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class Client {


            public static void main(String args[])
       {
/*   5*/        char c = '\u1088';
/*   6*/        String s = "192.168.10.56";// PUT HERE UR SYSTEM IP    ADDRESS
/*   9*/        try {
/*   9*/            InetAddress inetaddress = InetAddress.getByName(s);
/*  10*/            System.out.println((new StringBuilder()).append("Any of you heard of a socket with IP address ").append(s).append(" and port ").append(c).append("?").toString());
/*  11*/            Socket socket = new Socket(inetaddress, c);
/*  12*/            System.out.println(" I just get connected to Server..........");
/*  14*/            InputStream inputstream = socket.getInputStream();
/*  15*/            OutputStream outputstream = socket.getOutputStream();
/*  17*/            DataInputStream datainputstream = new DataInputStream(inputstream);
/*  18*/            DataOutputStream dataoutputstream = new DataOutputStream(outputstream);
/*  20*/            BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
/*  21*/            Object obj = null;
                    int a=0;
                    int fl= 1;
//                    String ans = null;
                 while(true)
                {
/*  22*/            System.out.println("Type the source address and PRESS enter. Will send it to the server .");
/*  23*/            //System.out.println();
/*  25*/
/*  25*/                String s1 = bufferedreader.readLine();
/*  26*/                System.out.println("Sending source name to the server...");
                        if(s1.equals("000"))
                        {
                        System.out.println("Good Bye......");
                        System.exit(0);
                        }
/*  27*/                dataoutputstream.writeUTF(s1);
/*  28*/                dataoutputstream.flush();
                        if(s1.equals("0000"))
                        {
                        System.out.println("Server is down");
                        System.exit(0);
                        }


                        System.out.println("Enter the destination address");
                        String s2 = bufferedreader.readLine();
                        String newDir = new String(s2);
                        boolean success = (new File(newDir)).mkdir();

                        String s3="\\image.jpg";
                        System.out.println("Value of s3 is" +s3);
                        String yo = new String(s2+s3);
                        System.out.println("Value of yo is" +yo);
                        System.out.println("Sending destination path  to the server...");
/*  27*/                dataoutputstream.writeUTF(s2);
/*  28*/                dataoutputstream.flush();

                        int i=0;


                     while (fl == 1)
                     {

                         // here we find out the  .  into destination path and chang the name by i
                            String  aOldPattern = ".";
                            String aNewPattern  = i+".";
                            String s5 = yo.replace(aOldPattern, aNewPattern);
                            System.out.println(" image"+i+  "  will be at:   " +s5);
                            i = i+1;
                            ObjectInputStream objectinputstream = new ObjectInputStream(socket.getInputStream());


                            if(socket.getInputStream() == null)
                            {
                             fl = 0;
                             //break;
                            } else{
/*  30*/                    byte abyte0[] = (byte[])(byte[])objectinputstream.readObject();
/*  31*/                    FileOutputStream fileoutputstream = new FileOutputStream(s5);
/*  32*/                    fileoutputstream.write(abyte0);
/*  33*/                    fileoutputstream.flush();
                            fileoutputstream.flush();
                            System.out.println(" image"+i+" is copied ");
                            }

/*  35*/                }
                    System.out.println("All images are copied");
                    System.out.println("You want to exit, press y");
                    String ans = bufferedreader.readLine();
                    if(ans.equals( " y "))
                    {
                        System.exit(0);
                    }
                }
/*  40*/
            } catch (Exception exception) {
/*  41*/            exception.printStackTrace();
                }
       }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.