java 利用文件对话框实现文件的转存功能

好久就想写了 可惜 那时候书本还没看多少。

这两天又给同学装系统。昨天晚上才有时间写。可是遇到了一个问题 就是不知道怎么用

文件对话框选择文件夹 又百度搜索 ,找到了。

后来又发现转存的文件只能保存在文件夹所在的那个文件夹。不能保存在所选择的文件夹里面。

百度了好久 找到了、

package 文件转存;
public class 文件转存 {
	public static void main(String[] args) {
		WindowBuild win=new WindowBuild();
		win.setTitle("文件转存");
	}

}
package 文件转存;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
public  class WindowBuild extends JFrame implements ActionListener{
	JFileChooser fileDialog1,fileDialog2;
	JButton choosepath1,confirm,choosepath2;
	JTextField path1,path2;
	BufferedReader in;
	FileReader fileReader;
	BufferedWriter out;
	FileWriter fileWriter;
	Box box1,box2,box3,baseBox;
	File dir1=null,dir2=null;
	String name1=null,pathChoose=null,pathConfirm=null;
	JLabel mark;
	boolean hasChoosed1=false,hasChoosed2=false;
	WindowBuild(){
		setLayout(new FlowLayout());
		init();
		setSize(300,200);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	void init(){
		path1=new JTextField(10);
		path2=new JTextField(10);
		choosepath1=new JButton("选择文件");
		choosepath2=new JButton("选择地址");
		confirm=new JButton("确定");
		box1=Box.createHorizontalBox();
		box2=Box.createHorizontalBox();
		box3=Box.createHorizontalBox();
		baseBox=Box.createVerticalBox();
		box1.add(path1);
		box1.add(Box.createHorizontalStrut(5));
		box1.add(choosepath1);
		box2.add(path2);
		box2.add(Box.createHorizontalStrut(5));
		box2.add(choosepath2);
		box3.add(mark=new JLabel("确认转存吗?"));
		box2.add(Box.createHorizontalStrut(5));
		box3.add(confirm);
		baseBox.add(box1);
		baseBox.add(Box.createVerticalStrut(20));
		baseBox.add(box2);
		baseBox.add(Box.createVerticalStrut(20));
		baseBox.add(box3);
		add(baseBox);
		choosepath1.addActionListener(this);
		choosepath2.addActionListener(this);
		confirm.addActionListener(this);
		fileDialog1=new JFileChooser();
		FileNameExtensionFilter filter=new FileNameExtensionFilter("TXT文件","txt");
		fileDialog1.setFileFilter(filter);
		fileDialog2=new JFileChooser();
		fileDialog2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==choosepath1){
			int state=fileDialog1.showSaveDialog(this);
			if(state==0){
				dir1=fileDialog1.getCurrentDirectory();
				name1=fileDialog1.getSelectedFile().getName();
				pathChoose=fileDialog1.getSelectedFile().getPath();
				path1.setText(pathChoose);
				hasChoosed1=true;
			}
		}
		else if(e.getSource()==choosepath2){
			int state=fileDialog2.showSaveDialog(this);
			if(state==0){
				pathConfirm=fileDialog2.getSelectedFile().getPath();
				path2.setText(pathConfirm);		
				hasChoosed2=true;
			}
		}
		else if(e.getSource()==confirm&&hasChoosed1==true&&hasChoosed2==true){
			try{
				File file1=new File(dir1,name1);
				File file2=new File(pathConfirm,"新文件.txt");
				fileWriter=new FileWriter(file2);
				fileReader=new FileReader(file1);
				out=new BufferedWriter(fileWriter);
				in=new BufferedReader(fileReader);
				String s=null;
				while((s=in.readLine())!=null){
					out.write(s);
					out.newLine();
					System.out.println(s);
				}
				in.close();
				out.close();
				fileReader.close();
				fileWriter.close();
				JOptionPane.showMessageDialog(this, "保存成功!!!","消息对话框",JOptionPane.INFORMATION_MESSAGE);
			}
			catch(IOException exp){
				JOptionPane.showMessageDialog(this, "保存失败!!!","消息对话框",JOptionPane.INFORMATION_MESSAGE);
			}
		}
		else
			JOptionPane.showMessageDialog(this, "保存失败!!!","消息对话框",JOptionPane.INFORMATION_MESSAGE);
		
	}

}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值