Скопировать выбранный файл в другую директорию - Java
Формулировка задачи:
Всем привет, помогите новичку с кодом, занимаюсь по книжке, вообщем задание было сделать программу графическую которая бы копировала выбранный файл, в другую директорию.
Собственно вод код.
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Copyra implements ActionListener {
JLabel label1, label2;
JTextField field1, field2;
JFileChooser choose1, choose2;
JButton browse1, browse2, copyButton;
JPanel windowContent;
FileInputStream someFile = null;
BufferedInputStream buff1 = null;
FileOutputStream newFile = null;
BufferedOutputStream buff2 = null;
public Copyra() {
GridLayout gl = new GridLayout(3,3);
windowContent = new JPanel();
windowContent.setLayout(gl);
label1 = new JLabel("Choose file:");
label2 = new JLabel("Select directory:");
browse1 = new JButton("Browse");
browse1.setBackground(Color.RED);
browse1.addActionListener(this);
browse2 = new JButton("Browse");
browse2.setBackground(Color.YELLOW);
browse2.addActionListener(this);
copyButton = new JButton("Copy");
copyButton.setBackground(Color.GREEN);
copyButton.addActionListener(this);
field1 = new JTextField();
filed2 = new JTextField();
public void actionPerformed(ActionEvent e) {
choose1 = new JFileChooser();
int a = choose1.showDialog(null, "Choose file");
if (a == JFileChooser.APPROVE_OPTION) {
File file1 = choose1.getSelectedFile();
field1.setText(file1.getName());
}
choose2 = new JFileChooser();
int b = choose2.showDialog(null, "Select directory");
if (b == JFileChooser.APPROVE_OPTION) {
File file2 = choose2.getSelectedFile();
field2.setText(file2.getName());
}
JButton button = (JButton) e.getSource();
if (button == copyButton) {
try {
someFile = new FileInputStream(file1);
buff1 = new BufferedInputStream(someFile, 5000);
newFile = new FileOutputStream(file2);
buff2 = new BufferedOutputStream(newFile);
while (true) {
int byteValue = buff1.read();
buff2.write(byteValue);
if (byteValue == -1)
break;
}
}
catch (IOException e) {
System.out.println("Sorry operation failed " + e.toString());
}
finally {
try {
someFile.close();
newFile.close();
}
catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("copy complete.");
}
}
}
windowContent.add(label1);
windowContent.add(field1);
windowContent.add(browse1);
windowContent.add(label2);
windowContent.add(field2);
windowContent.add(browse2);
windowContent.add(copyButton);
JFrame frame = new Frame("Copyra");
frame.setContentPane(windowContent);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
Copyra copyra = new Copyra();
}Решение задачи: «Скопировать выбранный файл в другую директорию»
textual
Листинг программы
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Copyra implements ActionListener {
JLabel label1, label2, label3;
JTextField field1, field2;
JFileChooser choose1, choose2;
JButton browse1, browse2, copyButton;
JPanel windowContent;
FileInputStream someFile = null;
BufferedInputStream buff1 = null;
FileOutputStream newFile = null;
BufferedOutputStream buff2 = null;
public Copyra() {
GridLayout gl = new GridLayout(3,3);
windowContent = new JPanel();
windowContent.setLayout(gl);
label1 = new JLabel("Choose file:");
label2 = new JLabel("Select directory:");
label3 = new JLabel();
browse1 = new JButton("Browse");
browse1.setBackground(Color.RED);
browse1.addActionListener(this);
browse2 = new JButton("Browse");
browse2.setBackground(Color.YELLOW);
browse2.addActionListener(this);
copyButton = new JButton("Copy");
copyButton.setBackground(Color.GREEN);
copyButton.addActionListener(this);
field1 = new JTextField();
field2 = new JTextField();
windowContent.add(label1);
windowContent.add(field1);
windowContent.add(browse1);
windowContent.add(label2);
windowContent.add(field2);
windowContent.add(browse2);
windowContent.add(label3);
windowContent.add(copyButton);
JFrame frame = new JFrame("Copyra");
frame.setContentPane(windowContent);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if (button == browse1) {
choose1 = new JFileChooser();
choose1.showDialog(null, "Choose file");
File file1 = choose1.getSelectedFile();
field1.setText(file1.getName());
}
if (button == browse2) {
choose2 = new JFileChooser();
choose2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
choose2.showDialog(null, "Select directory");
File file2 = choose2.getSelectedFile();
field2.setText(file2.getName());
}
if (button == copyButton) {
someFile = new FileInputStream(choose1.getSelectedFile());
buff1 = new BufferedInputStream(someFile);
File file3 = (File).createNewFile();
newFile = new FileOutputStream(choose2.getSelectedFile()+file);
buff2 = new BufferedOutputStream(newFile);
while (true) {
int byteValue = buff1.read();
buff2.write(byteValue);
if (byteValue == -1)
break;
}
}
}
public static void main(String[] args) {
Copyra copyra = new Copyra();
}
}