Copying Information utilizing Java Code
On this Copying Information utilizing Java utility we are going to learn to copy file utilizing java code. I’ve used swings for making a pattern utility which has progress bar to inform the progress of copying course of. We’re utilizing easy java enter stream and output stream to repeat the file from one location to different location. It’s important to specify source location which is the file that person is copying. Then he/she has to specify vacation spot location for the copied file, if each the areas are legitimate file might be copied.
Steps for copying File
- Enter source location.
- Enter Vacation spot location.
- Click on the button. If each the areas are legitimate file might be copied.
Snapshot for File copy Software
Supply Code for Copy File Software
MainWindow.java
1 2 |
package deal com.projectsgeek;<br/><br/>import com.projectsgeek.copy.CopyThread;<br/>import java.awt.occasion.ActionEvent;<br/>import java.awt.occasion.ActionListener;<br/><br/>/**<br/> *<br/> * @writer Projectsgeek.com<br/> */<br/>public class MainWindow extends javax.swing.JFrame implements ActionListener{<br/><br/> /**<br/> * Creates new type MainWindow<br/> */<br/> public MainWindow() {<br/> initComponents();<br/> // Registering the motion listner for button<br/> jButton1.addActionListener(this);<br/> }<br/><br/> /**<br/> * This methodology is named from throughout the constructor to initialize the shape.<br/> * WARNING: Do NOT modify this code. The content material of this methodology is all the time<br/> * regenerated by the Kind Editor.<br/> */<br/> @SuppressWarnings("unchecked")<br/> // <editor-fold defaultstate="collapsed" desc="Generated Code"> <br/> non-public void initComponents() {<br/><br/> jLabel1 = new javax.swing.JLabel();<br/> jLabel2 = new javax.swing.JLabel();<br/> jTextField1 = new javax.swing.JTextField();<br/> jTextField2 = new javax.swing.JTextField();<br/> jButton1 = new javax.swing.JButton();<br/> jProgressBar1 = new javax.swing.JProgressBar();<br/><br/> setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);<br/> setTitle("Copy File Software");<br/> setPreferredSize(new java.awt.Dimension(500, 200));<br/><br/> jLabel1.setText("Supply File");<br/><br/> jLabel2.setText("Vacation spot File");<br/><br/> jButton1.setText("Copy");<br/><br/> jProgressBar1.setStringPainted(true);<br/><br/> javax.swing.GroupLayout structure = new javax.swing.GroupLayout(getContentPane());<br/> getContentPane().setLayout(structure);<br/> structure.setHorizontalGroup(<br/> structure.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br/> .addGroup(structure.createSequentialGroup()<br/> .addGap(206, 206, 206)<br/> .addComponent(jButton1)<br/> .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Brief.MAX_VALUE))<br/> .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, structure.createSequentialGroup()<br/> .addGap(28, 28, 28)<br/> .addGroup(structure.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)<br/> .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Brief.MAX_VALUE)<br/> .addGroup(structure.createSequentialGroup()<br/> .addGroup(structure.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br/> .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)<br/> .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))<br/> .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Brief.MAX_VALUE)<br/> .addGroup(structure.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br/> .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)<br/> .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))))<br/> .addGap(31, 31, 31))<br/> );<br/> structure.setVerticalGroup(<br/> structure.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)<br/> .addGroup(structure.createSequentialGroup()<br/> .addContainerGap()<br/> .addGroup(structure.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)<br/> .addComponent(jLabel1)<br/> .addComponent(jLabel2))<br/> .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)<br/> .addGroup(structure.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)<br/> .addCom ponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)<br/> .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))<br/> .addGap(27, 27, 27)<br/> .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)<br/> .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Brief.MAX_VALUE)<br/> .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)<br/> .addContainerGap())<br/> );<br/><br/> pack();<br/> }// </editor-fold> <br/><br/> /**<br/> * @param args the command line arguments<br/> */<br/> public static void most important(String args[]) {<br/> /* Set the Nimbus feel and look */<br/> //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "><br/> /* If Nimbus (launched in Java SE 6) just isn't accessible, stick with the default feel and look.<br/> * For particulars see https://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html <br/> */<br/> attempt {<br/> for (javax.swing.UIManager.LookAndFeelInfo data : javax.swing.UIManager.getInstalledLookAndFeels()) {<br/> if ("Nimbus".equals(data.getName())) {<br/> javax.swing.UIManager.setLookAndFeel(data.getClassName());<br/> break;<br/> }<br/> }<br/> } catch (ClassNotFoundException ex) {<br/> java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Stage.SEVERE, null, ex);<br/> } catch (InstantiationException ex) {<br/> java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Stage.SEVERE, null, ex);<br/> } catch (IllegalAccessException ex) {<br/> java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Stage.SEVERE, null, ex);<br/> } catch (javax.swing.UnsupportedLookAndFeelException ex) {<br/> java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Stage.SEVERE, null, ex);<br/> }<br/> //</editor-fold><br/><br/> /* Create and show the shape */<br/> java.awt.EventQueue.invokeLater(new Runnable() {<br/> public void run() {<br/> new MainWindow().setVisible(true);<br/> }<br/> });<br/> }<br/><br/> // Variables declaration - don't modify <br/> non-public javax.swing.JButton jButton1;<br/> non-public javax.swing.JLabel jLabel1;<br/> non-public javax.swing.JLabel jLabel2;<br/> non-public javax.swing.JProgressBar jProgressBar1;<br/> non-public javax.swing.JTextField jTextField1;<br/> non-public javax.swing.JTextField jTextField2;<br/> // Finish of variables declaration <br/><br/> // Catching the motion occasion and the peforming the Process. This methodology will get executed when button is clicked.<br/> @Override<br/> public void actionPerformed(ActionEvent e) {<br/> <br/> // Making a runnable for the duty<br/> CopyThread copythread = new CopyThread();<br/> // Setting the source file<br/> copythread.setSourceFile(jTextField1.getText());<br/> // Setting the vacation spot File<br/> copythread.setDestinationFile(jTextField2.getText());<br/> // Setting the progress bar occasion in runnable class<br/> copythread.setjProgressBar1(jProgressBar1);<br/> // Passing the runnable to string<br/> Thread thread = new Thread(copythread);<br/> // Beginning the thread<br/> thread.begin();<br/> }<br/><br/>}<br/> |
CopyThread.java
1 |
/*<br/> * To alter this license header, select License Headers in Venture Properties.<br/> * To alter this template file, select Instruments | Templates<br/> * and open the template within the editor.<br/> */<br/>package deal com.projectsgeek.copy;<br/><br/>import java.io.File;<br/>import java.io.FileInputStream;<br/>import java.io.FileOutputStream;<br/>import java.io.IOException;<br/>import java.io.InputStream;<br/>import java.io.OutputStream;<br/>import javax.swing.JProgressBar;<br/><br/>/**<br/> *<br/> * @writer Projectsgeek.com<br/> */<br/>public class CopyThread implements Runnable{<br/> <br/> non-public String sourceFile;<br/> non-public String destinationFile;<br/> non-public javax.swing.JProgressBar jProgressBar1;<br/> <br/> // Setters and getters for sourceFile, destinationFile and progressbar<br/> public JProgressBar getjProgressBar1() {<br/> return jProgressBar1;<br/> }<br/><br/> public void setjProgressBar1(JProgressBar jProgressBar1) {<br/> this.jProgressBar1 = jProgressBar1;<br/> }<br/> <br/> public String getSourceFile() {<br/> return sourceFile;<br/> }<br/><br/> public String getDestinationFile() {<br/> return destinationFile;<br/> }<br/><br/> public void setSourceFile(String sourceFile) {<br/> this.sourceFile = sourceFile;<br/> }<br/><br/> public void setDestinationFile(String destinationFile) {<br/> this.destinationFile = destinationFile;<br/> }<br/><br/> // That is the run methodology which is able to do the true activity of copying the file in addition to updating the progress bar primarily based on the present standing <br/> @Override<br/> public void run() {<br/> <br/> // Creating the enter stream to learn the file<br/> InputStream inputStream = null;<br/> // Creating the output stream to put in writing the file<br/>OutputStream outputStream = null;<br/><br/> attempt{<br/> <br/> // creating an occasion of source file <br/> File sourcefile =new File(sourceFile);<br/> // creating an occasion of vacation spot file<br/> File destinationfile =new File(destinationFile);<br/> <br/> // Now studying the file and loading it in inputstream<br/> inputStream = new FileInputStream(sourcefile);<br/> // Now pointing output stream to the vacation spot file<br/> outputStream = new FileOutputStream(destinationfile);<br/> <br/> byte[] buffer = new byte[1024];<br/> // Taking the size of source file for updating progress bar<br/> lengthy dimension = sourcefile.size();<br/> lengthy depend=0;<br/> <br/> int size;<br/> // Now we are going to begin byte by byte to learn the inputstream and passing it to outputstream <br/> whereas ((size = inputStream.learn(buffer)) > 0){<br/> outputStream.write(buffer, 0, size);<br/> depend+=size;<br/> // Updating the progress bar in share<br/> jProgressBar1.setValue((int) (depend*100/dimension));<br/> jProgressBar1.setString((int) (depend*100/dimension)+"%");<br/> }<br/> // Now copied succesfully updating progress bar<br/> jProgressBar1.setValue(100);<br/> jProgressBar1.setString("Copied Efficiently");<br/> <br/> // Closing enter in addition to outputstream<br/> inputStream.shut();<br/> outputStream.shut();<br/> <br/> }catch(IOException e){<br/> e.printStackTrace();<br/> }<br/> }<br/> <br/>}<br/> |
Obtain Copying Information utilizing Java Code
Supply projectgeek.com