| @ -0,0 +1,121 @@ | |||
| package mywindow.antoniofrische; | |||
| import java.awt.EventQueue; | |||
| import java.awt.event.ActionEvent; | |||
| import java.awt.event.ActionListener; | |||
| import java.io.File; | |||
| import java.io.FileInputStream; | |||
| import java.io.FileOutputStream; | |||
| import java.io.IOException; | |||
| import javax.swing.JFrame; | |||
| import javax.swing.JTextField; | |||
| import javax.swing.JProgressBar; | |||
| import javax.swing.JButton; | |||
| import javax.swing.JLabel; | |||
| public class MyWindow implements ActionListener{ | |||
| private JFrame frame; | |||
| private JTextField tfInput; | |||
| private JTextField tfNewName; | |||
| private JProgressBar pbCopy; | |||
| private static JButton btnCopy; | |||
| /** | |||
| * Launch the application. | |||
| */ | |||
| public static void main(String[] args) { | |||
| EventQueue.invokeLater(new Runnable() { | |||
| public void run() { | |||
| try { | |||
| MyWindow window = new MyWindow(); | |||
| window.frame.setVisible(true); | |||
| btnCopy.addActionListener(window); | |||
| } catch (Exception e) { | |||
| e.printStackTrace(); | |||
| } | |||
| } | |||
| }); | |||
| } | |||
| /** | |||
| * Create the application. | |||
| */ | |||
| public MyWindow() { | |||
| initialize(); | |||
| } | |||
| /** | |||
| * Initialize the contents of the frame. | |||
| */ | |||
| private void initialize() { | |||
| frame = new JFrame(); | |||
| frame.setBounds(100, 100, 450, 300); | |||
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |||
| frame.getContentPane().setLayout(null); | |||
| tfInput = new JTextField(); | |||
| tfInput.setBounds(65, 44, 279, 20); | |||
| frame.getContentPane().add(tfInput); | |||
| tfInput.setColumns(10); | |||
| tfNewName = new JTextField(); | |||
| tfNewName.setBounds(65, 98, 279, 20); | |||
| frame.getContentPane().add(tfNewName); | |||
| tfNewName.setColumns(10); | |||
| pbCopy = new JProgressBar(); | |||
| pbCopy.setBounds(39, 203, 339, 14); | |||
| frame.getContentPane().add(pbCopy); | |||
| btnCopy = new JButton("Hacer copia"); | |||
| btnCopy.setBounds(65, 143, 117, 23); | |||
| frame.getContentPane().add(btnCopy); | |||
| JLabel lblNewLabel = new JLabel("Ruta del FIchero a copiar"); | |||
| lblNewLabel.setBounds(65, 21, 135, 14); | |||
| frame.getContentPane().add(lblNewLabel); | |||
| } | |||
| public void actionPerformed(ActionEvent e) { | |||
| boolean copy = copiarArch(); | |||
| if(copy) { | |||
| //anything | |||
| }else { | |||
| //anything | |||
| } | |||
| } | |||
| private boolean copiarArch() { | |||
| String inputFile = tfInput.getText().toString(); | |||
| String newName = tfNewName.getText().toString(); | |||
| String[]spliter = inputFile.split("[.]"); | |||
| File input = new File(inputFile); | |||
| File output = new File(input.getParent(),newName + ".txt"); | |||
| try(FileInputStream in = new FileInputStream(input); | |||
| FileOutputStream out = new FileOutputStream(output); | |||
| ){ | |||
| byte[] complet = in.readAllBytes(); | |||
| for(int i = 0; i < complet.length; i++) { | |||
| Thread.sleep(500); | |||
| out.write(complet[i]); | |||
| pbCopy.setValue(i); | |||
| pbCopy.update(pbCopy.getGraphics()); | |||
| } | |||
| pbCopy.setValue(100); | |||
| pbCopy.update(pbCopy.getGraphics()); | |||
| return true; | |||
| }catch(NullPointerException npe) { | |||
| //impremir que el archivo no existe! | |||
| return false; | |||
| }catch(IOException ioe) { | |||
| return false; | |||
| } catch (InterruptedException e) { | |||
| // TODO Auto-generated catch block | |||
| return false; | |||
| } | |||
| } | |||
| } | |||