Research breakthrough possible @S-Logix pro@slogix.in

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to copy a file from one location to another location in java?

Description

This program shows how to copy a file from one location to another location using java code. Any source file can be chosen from JFileChooser and target location to copy the file can also be chosen. Two code executing in a same port is required for file copying. File is read as byte array and written into OutputStream. File content is read as bytes from InputStream and stored in a file with the same name as source file.

Sample Code
  • Filename: FileCopy.java

import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
class FileCopy {
JPanel panel;
JFrame jf;
JLabel label1,label2;
JButton Browse,Copy;
JTextField textfield1,textfield2,textfield3;
JPasswordField passwordfield;
String inputFile;
String filename,location;
public FileCopy() {
initComponents();
handlingEvents();
}
public void initComponents() {
jf=new javax.swing.JFrame();
jf.setTitle("File Source");
jf.setLayout(null);
jf.setSize(800,500);
jf.show();
jf.setVisible(true);
JScrollPane scrollBar=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jf.add(scrollBar);
label1=new javax.swing.JLabel("Source File");
label1.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
label1.setBounds(50,80,200,40);
jf.add(label1);
textfield1=new javax.swing.JTextField();
textfield1.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textfield1.setBounds(250,80,200,30);
jf.add(textfield1);
Browse=new javax.swing.JButton("Browse");
Browse.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Browse.setBounds(480,80,140,30);
jf.add(Browse);

Copy=new javax.swing.JButton("Copy");
Copy.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Copy.setBounds(300,180,140,30);
jf.add(Copy);
}
public void handlingEvents() {
Browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
inputFile=fc.getSelectedFile().getPath();
filename=fc.getSelectedFile().getName();
textfield1.setText(inputFile);
}
}
});
Copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
send();
}
});
}
public void send() {
Vector vec = new Vector();
try {
ServerSocket ss = new ServerSocket(2000);
System.out.println("Server is waiting");
Socket sa = ss.accept();
OutputStream os = sa.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
vec.addElement(filename);
oos.writeObject(vec);
oos.flush();
oos.close();
sa.close();
}
catch(Exception e) {
System.out.println(e);
}
try {
//Create socket:
ServerSocket servesock = new ServerSocket(2345);
while(true) {
System.out.println("Waiting…");

Socket sock = servesock.accept();
System.out.println("Accepted Connection…");
//send file:
File myFile = new File(inputFile);
byte[] mybytearray = new byte [(int)myFile.length()];
System.out.println("Available bytes = "+myFile.length());
FileInputStream fis = new FileInputStream(myFile);
System.out.println("Total available Bytes:" + fis.available());
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending..");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();
}
}
catch(Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
FileCopy log = new FileCopy();
}
}

  • Filename: FileCopyTarget.java

import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
class FileCopyTarget {
JPanel panel;
JFrame jf;
JLabel label1,label2;
JButton Browse,Target,Copy;
JTextField textfield1,textfield2,textfield3;
JPasswordField passwordfield;
String inputFile;
String filename,location;
public FileCopyTarget() {
initComponents();
handlingEvents();
}
public void initComponents() {
jf=new javax.swing.JFrame();
jf.setTitle("File target");
jf.setLayout(null);
jf.setSize(800,500);
jf.show();
jf.setVisible(true);
JScrollPane scrollBar=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jf.add(scrollBar);
label1=new javax.swing.JLabel("Target Location");
label1.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
label1.setBounds(50,130,200,40);
jf.add(label1);
textfield2=new javax.swing.JTextField();
textfield2.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textfield2.setBounds(250,130,200,30);
jf.add(textfield2);
Target=new javax.swing.JButton("Browse");
Target.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Target.setBounds(480,130,140,30);
jf.add(Target);
Copy=new javax.swing.JButton("Copy");
Copy.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Copy.setBounds(300,180,140,30);
jf.add(Copy);
}

public void handlingEvents() {
Target.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
location=fc.getSelectedFile().getPath();
textfield2.setText(location);
}
}
});
Copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
receive();
}
});
}
public void receive() {
try {
Vector vec=new Vector();
Socket s = new Socket("",2000);
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
vec = (Vector)ois.readObject();
filename = (String)vec.elementAt(0);
ois.close();
s.close();
}
catch(Exception e) {
System.out.println(e);
}
try {
int filesize = 6022386;
long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
//local host for testing
Socket sock = new Socket("",2345);
System.out.println("Connecting…");
//Receive file:
byte[] mybytearray = new byte[filesize];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(location+"/"+filename);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
System.out.println("bytesRead="+bytesRead);
current = bytesRead;

do {
bytesRead = is.read(mybytearray,current,(mybytearray.length-current));
if(bytesRead >= 0)
current+=bytesRead;
}
while(bytesRead >-1);
System.out.println("current="+current);
bos.write(mybytearray,0,current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
sock.close();
}
catch(Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
FileCopyTarget log = new FileCopyTarget();
}
}
catch(Exception e) {

System.out.println(e);

}

}

public static void main(String args[]) {

FileCopy log = new FileCopy();

}

}

Screenshots
  • Execution: FileCopy.java
  • Browse and Copy
  • Execution: FileCopyTarget.java
  • Browse and Copy

Copy a file from one location to another location in java
Browse and Copy
FileCopyTarget