文件转码

package com.ba.file;

import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.JFileChooser;

/** 文件转码 */
public class FileEncode {
	public static void main(String[] args) {
		//存放上次路径
		final File configFile = new File("config.properties");
		final Properties config = new Properties();
		if (configFile.exists() && configFile.isFile()) {
			try {
				config.load(new FileInputStream(configFile));
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

		final Frame f = new Frame();
		f.setLayout(null);
		f.setSize(400, 100);
		f.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		final TextField tf = new TextField();
		tf.setBounds(10, 35, 340, 25);
		tf.setText(config.getProperty("path", ""));
		f.add(tf);
		Button sbt = new Button("select");
		sbt.setBounds(350, 35, 40, 25);
		f.add(sbt);
		sbt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				if (config.getProperty("path") != null) {
					System.out.println(config.getProperty("path"));
					File of = new File(config.getProperty("path"));
					if (of.exists()) {
						//设置 文件选择器 当前打开的目录
						fc.setCurrentDirectory(of);
					}
				}
				fc.showOpenDialog(null);
				File sf = fc.getSelectedFile();
				config.setProperty("path", sf.getAbsolutePath());
				try {
					config.save(new FileOutputStream(configFile), null);
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
				tf.setText(sf.getAbsolutePath());
			}
		});
		Label flb = new Label("from encode:");
		flb.setBounds(10, 65, 80, 25);
		f.add(flb);
		final TextField ftf = new TextField();
		ftf.setBounds(90, 65, 50, 25);
		ftf.setText("utf-8");
		f.add(ftf);
		Label tlb = new Label("to encode:");
		tlb.setBounds(140, 65, 60, 25);
		f.add(tlb);
		final TextField ttf = new TextField();
		ttf.setBounds(200, 65, 50, 25);
		ttf.setText("GBK");
		f.add(ttf);
		Label elb = new Label("end:");
		elb.setBounds(250, 65, 30, 25);
		f.add(elb);
		final TextField etf = new TextField();
		etf.setBounds(280, 65, 50, 25);
		etf.setText("js");
		f.add(etf);

		final Button cbt = new Button("change");
		cbt.setBounds(340, 65, 50, 25);
		f.add(cbt);
		cbt.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String fstr = tf.getText().trim();
				if (fstr.equals("")) {
					return;
				}
				File file = new File(fstr);
				if (!file.exists()) {
					return;
				}
				if ("".equals(etf.getText().trim())) {
					return;
				}
				f.setTitle("waint...");
				List<File> files = filer(file, etf.getText().trim());
				ChangeThread cthr = new ChangeThread(files, ftf.getText(), ttf
						.getText(), f, cbt);
				cthr.start();
			}
		});
		f.setVisible(true);
	}

	/** 更改编码 */
	public static void change(File file, String fencode, String tencode) {
		try {
			byte[] bs = new byte[(int) file.length()];
			FileInputStream fis = new FileInputStream(file);
			fis.read(bs);
			fis.close();
			byte[] wbs = new String(bs, fencode).getBytes(tencode);
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(wbs);
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/** 找到文件 */
	public static List<File> filer(File file, String en) {
		ArrayList<File> al = new ArrayList<File>();
		if (file.isFile()) {
			if (file.getName().toLowerCase().endsWith(en)) {
				al.add(file);
			}
		} else {
			for (File f : file.listFiles()) {
				if (f.isFile() && f.getName().toLowerCase().endsWith(en)) {
					al.add(f);
				}
			}
			for (File f : file.listFiles()) {
				if (f.isDirectory()) {
					al.addAll(filer(f, en));
				}
			}
		}
		return al;
	}

}

class ChangeThread extends Thread {
	private List<File> files;
	private String from;
	private String to;
	private Frame f;
	private Button cbt;

	public ChangeThread(List<File> files, String from, String to, Frame f,
			Button cbt) {
		this.files = files;
		this.from = from;
		this.to = to;
		this.f = f;
		this.cbt = cbt;
		setDaemon(true);
	}

	@Override
	public void run() {
		cbt.setEnabled(false);
		try {
			f.setTitle("0/" + files.size());
			for (int i = 0; i < files.size(); i++) {
				FileEncode.change(files.get(i), from, to);
				f.setTitle((i + 1) + "/" + files.size());
			}
		} finally {
			cbt.setEnabled(true);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值