文件操作
来源:互联网 发布:我的淘宝卖家网页版 编辑:程序博客网 时间:2024/06/06 05:12
package fileOpt;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class FileMove extends JPanel implements ActionListener{
public static int FROM = 1;
public static int TO = 2;
private JTextField fromField;
private JButton fromButton;
private JTextField toField;
private JButton toButton;
private JButton moveButton;
private File fromFile;
private File toFile;
public FileMove(){
this.setPreferredSize(new Dimension(350,251));
SpringLayout layout = new SpringLayout();
this.setLayout(layout);
this.fromField = new JTextField();
this.fromField.setPreferredSize(new Dimension(230,26));
this.add(this.fromField);
layout.putConstraint(SpringLayout.NORTH, this.fromField, 10, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, this.fromField, 10, SpringLayout.NORTH, this);
this.fromButton = new JButton("refer to");
this.fromButton.setPreferredSize(new Dimension(80,26));
this.add(this.fromButton);
layout.putConstraint(SpringLayout.NORTH, this.fromButton, 10, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, this.fromButton, 20, SpringLayout.EAST, this.fromField);
this.fromButton.addActionListener(this);
this.toField = new JTextField();
this.toField.setPreferredSize(new Dimension(230,26));
this.add(this.toField);
layout.putConstraint(SpringLayout.NORTH, this.toField, 10, SpringLayout.SOUTH, fromField);
layout.putConstraint(SpringLayout.WEST, this.toField, 10, SpringLayout.NORTH, this);
this.toButton = new JButton("refer to");
this.toButton.setPreferredSize(new Dimension(80,26));
this.add(this.toButton);
layout.putConstraint(SpringLayout.NORTH, this.toButton, 10, SpringLayout.SOUTH, fromField);
layout.putConstraint(SpringLayout.WEST, this.toButton, 20, SpringLayout.EAST, this.toField);
this.toButton.addActionListener(this);
moveButton = new JButton("move");
this.moveButton.setPreferredSize(new Dimension(80,26));
this.add(this.moveButton);
moveButton.addActionListener(this);
layout.putConstraint(SpringLayout.NORTH, this.moveButton, 10, SpringLayout.SOUTH, toField);
layout.putConstraint(SpringLayout.WEST, this.moveButton, 140, SpringLayout.WEST, this);
}
public void moveFile() throws IOException{
if (this.fromFile == null|| this.toField == null ) {
System.err.println("path null");
}
byte[] bs = new byte[1560];
// FileOutputStream outputStream = new FileOutputStream(fromPath);
BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(this.fromFile));
int len;
BufferedOutputStream outputStream =new BufferedOutputStream(new FileOutputStream(toFile));
try {
while ((len = fileInputStream.read(bs)) != -1) {
for(int i=0;i<len;i++) {
outputStream.write(bs[i]);
}
}
} finally {
fileInputStream.close();
outputStream.close();
}
}
public void showReferPathDialog(int flag){
System.err.println();;
if (flag == FileMove.FROM) {
JFileChooser chosee = new JFileChooser();
//chosee.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chosee.showOpenDialog(this);
this.fromFile = chosee.getSelectedFile();
this.fromField.setText(fromFile.getPath());
}
if (flag == FileMove.TO) {
JFileChooser chosee = new JFileChooser();
chosee.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int n = chosee.showOpenDialog(this);
if (n == JFileChooser.APPROVE_OPTION) {
this.toFile = chosee.getSelectedFile();
String[] strings = toFile.getPath().split("////");
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < strings.length; i++) {
buffer.append(strings[i] + File.separator);
}
this.toField.setText(this.toFile.getPath());
buffer.append(fromFile.getName());
this.toFile = new File(buffer.toString());
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == this.fromButton) {
showReferPathDialog(FileMove.FROM);
}
if (e.getSource() == this.toButton) {
showReferPathDialog(FileMove.TO);
}
if (e.getSource() == this.moveButton) {
try {
this.moveFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
JPanel panel = new FileMove();
JFrame frame = new JFrame();
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}