Скопировать выбранный файл в другую директорию - Java

Узнай цену своей работы

Формулировка задачи:

Всем привет, помогите новичку с кодом, занимаюсь по книжке, вообщем задание было сделать программу графическую которая бы копировала выбранный файл, в другую директорию. Собственно вод код.
Листинг программы
  1. import java.io.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. public class Copyra implements ActionListener {
  6. JLabel label1, label2;
  7. JTextField field1, field2;
  8. JFileChooser choose1, choose2;
  9. JButton browse1, browse2, copyButton;
  10. JPanel windowContent;
  11. FileInputStream someFile = null;
  12. BufferedInputStream buff1 = null;
  13. FileOutputStream newFile = null;
  14. BufferedOutputStream buff2 = null;
  15. public Copyra() {
  16. GridLayout gl = new GridLayout(3,3);
  17. windowContent = new JPanel();
  18. windowContent.setLayout(gl);
  19. label1 = new JLabel("Choose file:");
  20. label2 = new JLabel("Select directory:");
  21. browse1 = new JButton("Browse");
  22. browse1.setBackground(Color.RED);
  23. browse1.addActionListener(this);
  24. browse2 = new JButton("Browse");
  25. browse2.setBackground(Color.YELLOW);
  26. browse2.addActionListener(this);
  27. copyButton = new JButton("Copy");
  28. copyButton.setBackground(Color.GREEN);
  29. copyButton.addActionListener(this);
  30. field1 = new JTextField();
  31. filed2 = new JTextField();
  32. public void actionPerformed(ActionEvent e) {
  33. choose1 = new JFileChooser();
  34. int a = choose1.showDialog(null, "Choose file");
  35. if (a == JFileChooser.APPROVE_OPTION) {
  36. File file1 = choose1.getSelectedFile();
  37. field1.setText(file1.getName());
  38. }
  39. choose2 = new JFileChooser();
  40. int b = choose2.showDialog(null, "Select directory");
  41. if (b == JFileChooser.APPROVE_OPTION) {
  42. File file2 = choose2.getSelectedFile();
  43. field2.setText(file2.getName());
  44. }
  45. JButton button = (JButton) e.getSource();
  46. if (button == copyButton) {
  47. try {
  48. someFile = new FileInputStream(file1);
  49. buff1 = new BufferedInputStream(someFile, 5000);
  50. newFile = new FileOutputStream(file2);
  51. buff2 = new BufferedOutputStream(newFile);
  52. while (true) {
  53. int byteValue = buff1.read();
  54. buff2.write(byteValue);
  55. if (byteValue == -1)
  56. break;
  57. }
  58. }
  59. catch (IOException e) {
  60. System.out.println("Sorry operation failed " + e.toString());
  61. }
  62. finally {
  63. try {
  64. someFile.close();
  65. newFile.close();
  66. }
  67. catch (Exception e1) {
  68. e1.printStackTrace();
  69. }
  70. System.out.println("copy complete.");
  71. }
  72. }
  73. }
  74. windowContent.add(label1);
  75. windowContent.add(field1);
  76. windowContent.add(browse1);
  77. windowContent.add(label2);
  78. windowContent.add(field2);
  79. windowContent.add(browse2);
  80. windowContent.add(copyButton);
  81. JFrame frame = new Frame("Copyra");
  82. frame.setContentPane(windowContent);
  83. frame.pack();
  84. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85. frame.setVisible(true);
  86. }
  87. public static void main(String[] args) {
  88. Copyra copyra = new Copyra();
  89. }

Решение задачи: «Скопировать выбранный файл в другую директорию»

textual
Листинг программы
  1. import java.io.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5.  
  6. public class Copyra implements ActionListener {
  7.  
  8.     JLabel label1, label2, label3;
  9.     JTextField field1, field2;
  10.     JFileChooser choose1, choose2;
  11.     JButton browse1, browse2, copyButton;
  12.     JPanel windowContent;
  13.     FileInputStream someFile = null;
  14.     BufferedInputStream buff1 = null;
  15.     FileOutputStream newFile = null;
  16.     BufferedOutputStream buff2 = null;
  17.  
  18.     public Copyra() {
  19.  
  20.         GridLayout gl = new GridLayout(3,3);
  21.         windowContent = new JPanel();
  22.         windowContent.setLayout(gl);
  23.  
  24.         label1 = new JLabel("Choose file:");
  25.         label2 = new JLabel("Select directory:");
  26.         label3 = new JLabel();
  27.  
  28.         browse1 = new JButton("Browse");
  29.         browse1.setBackground(Color.RED);
  30.         browse1.addActionListener(this);
  31.  
  32.         browse2 = new JButton("Browse");
  33.         browse2.setBackground(Color.YELLOW);
  34.         browse2.addActionListener(this);
  35.  
  36.         copyButton = new JButton("Copy");
  37.         copyButton.setBackground(Color.GREEN);
  38.         copyButton.addActionListener(this);
  39.  
  40.         field1 = new JTextField();
  41.         field2 = new JTextField();
  42.  
  43.        
  44.         windowContent.add(label1);
  45.         windowContent.add(field1);
  46.         windowContent.add(browse1);
  47.         windowContent.add(label2);
  48.         windowContent.add(field2);
  49.         windowContent.add(browse2);
  50.         windowContent.add(label3);
  51.         windowContent.add(copyButton);
  52.  
  53.         JFrame frame = new JFrame("Copyra");
  54.         frame.setContentPane(windowContent);
  55.         frame.pack();
  56.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57.         frame.setVisible(true);
  58.  
  59.     }
  60.  
  61.     public void actionPerformed(ActionEvent e) {
  62.         JButton button = (JButton) e.getSource();
  63.  
  64.         if (button == browse1) {
  65.             choose1 = new JFileChooser();
  66.             choose1.showDialog(null, "Choose file");
  67.             File file1 = choose1.getSelectedFile();
  68.             field1.setText(file1.getName());
  69.         }
  70.  
  71.         if (button == browse2) {
  72.             choose2 = new JFileChooser();
  73.             choose2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  74.             choose2.showDialog(null, "Select directory");
  75.             File file2 = choose2.getSelectedFile();
  76.             field2.setText(file2.getName());
  77.         }
  78.  
  79.    
  80.         if (button == copyButton) {
  81.             someFile = new FileInputStream(choose1.getSelectedFile());
  82.             buff1 = new BufferedInputStream(someFile);
  83.             File file3 = (File).createNewFile();
  84.             newFile = new FileOutputStream(choose2.getSelectedFile()+file);
  85.             buff2 = new BufferedOutputStream(newFile);
  86.            
  87.             while (true) {
  88.  
  89.                 int byteValue = buff1.read();
  90.                 buff2.write(byteValue);
  91.  
  92.                 if (byteValue == -1)
  93.                     break;
  94.             }
  95.         }
  96.  
  97.     }
  98.  
  99.  
  100.     public static void main(String[] args) {
  101.         Copyra copyra = new Copyra();
  102.     }
  103. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.222 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы