Java file picker and file copying

// There are three classes in total, namely the encapsulated MyFileChooser class, the Botton class and the test class
package homework;
import java.awt.Container;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MyFileChooser extends JFileChooser{

	public File fileChooser(Container c) { // The method parameter is the container variable
 		FileNameExtensionFilter filter = new FileNameExtensionFilter( "JPG & GIF Images", "jpg", "gif");
 		this.setFileFilter(filter);// These two sentences are used As for the picture for selection, you can not
 int x = this.showOpenDialog(c);// Define the int type variable x to store the user's choice, if the user chooses to cancel, the return value is 1
 if(x == 1) {
 return null;
 }
 File f = this.getSelectedFile();// Define the File type variable to accept the selected file and return
 return f;
 }		
														
}
package work;
import java.awt.Container;
import javax.swing.JButton;
public class MyJBotton extends JButton{
	public MyJBotton(String info,int x,int y,Container c) {
		this.setText(info);
		this.setBounds(x, y, 100, 20);
		c.add(this);
	}
}
package homework;

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 java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class MyTestFrame extends JFrame{
	// Define a global variable f
 	private File f;

	public MyTestFrame() { this.setLayout
 		(null); 
 MyJBotton b1 = new MyJBotton("select file", 200, 100, this);
 MyJBotton b2 = new MyJBotton("copy file", 200, 200, this);
 b1. addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 MyFileChooser fc = new MyFileChooser(); // new a MyFileChooser object to call its methods f = fc.fileChooser(MyTestFrame.this); // call selection After the method, pass the object reference to the f global variable
 System.out.println(f.getAbsolutePath()); // print out the source file path as a record in the console
 }
 });
 b2.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) { fileSave(f); // Call the file copy method
 JOptionPane.showMessageDialog(null, "Copy successfully!");
		
					
					
										
								
															
					
										
											}
 		});
 this.setSize(500, 500);
 this.setVisible(true);
 this.setDefaultCloseOperation(3);
 this.setLocationRelativeTo(null);
 this.setResizable(false);
 this.setTitle("TEST");
 }
 private void fileSave(File f) {
 InputStream in = null;
 OutputStream out = null;
 File fold = new File("copy"); // Create a new directory object "copy" in the current project
 if(fold.exists() == false) { // If the "copy" directory does not exist in the current project, create a new
 fold.mkdirs();
 }
 try {
 in = new FileInputStream(f.getAbsolutePath()); // The parent class object calls the child class The construction method, pass in the absolute address of the source file, note that it must be the absolute address of the file! out = new FileOutputStream("copy/" + f.getName()); // The parent class object calls the subclass constructor and passes in the new address. Note that it must be the absolute address of the file!		
		
														
	
							
											
					
			
			
			byte[] by = new byte[10240]; // use byte array to transfer byte stream
 			int len ​​= 0;
 while((len = in.read(by)) != -1) { // each time from in Read by is stored in memory, and record the length of each transmission
 out.write(by, 0, len); // The three parameters are the byte length of each transmission, the length of the start of transmission, and the length of each transmission , until all are written to a new file
 } } catch (Exception e) {
 e.printStackTrace();
 } finally {
 try {
 out.close();
 in.close();
 } catch (IOException e) {
 e.printStackTrace( );
 } } }
 public static void main(String[] args) {
 MyTestFrame m = new MyTestFrame();
 }			
													
																															
				
		
				


}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324603405&siteId=291194637