报文编号

package com.huawei;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Convert 
{	
	// '?'对应的assic码
	public final int QUESTION_MARK = 63;
	
	public void convert2Number(String path, String resFileName, String newFileName) throws IOException
	{
		FileReader fr = null;
		FileWriter fw = null;
		try {
			fr = new FileReader(new File(path + resFileName));
			fw = new FileWriter(new File(path + newFileName));
			int i = fr.read();
			int num = 1;
			while(-1 != i)
			{
				if(QUESTION_MARK == i)
				{
					fw.write(String.valueOf(num));
					num++;
				}
				else
				{
					fw.write(i);
				}
				
				i = fr.read();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			fr.close();
			fw.close();
		}
	}
}


===================
package com.huawei;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class GUI 
{
	private String filePath;
	private File srcFile;
	private JFrame jFrame = null;
	private JButton choseButton = null;
	private JButton computeButton = null;
	private JTextField jtf = null;
	private JLabel la = null;
	private JFileChooser jfc = null;

	private ButtonListener buttLis = new ButtonListener();
	public void Frame() {
		jFrame = new JFrame("报 文 编 号");
		jFrame.setLayout(new FlowLayout());
		jtf = new JTextField(15);
		choseButton = new JButton("选择");
		computeButton = new JButton("编号");
		la = new JLabel("请选择文件路径:");
		jfc = new JFileChooser();
		jfc.setCurrentDirectory(new File("D:\\"));
		jfc.setDialogTitle("选择文件");
		jfc.setDragEnabled(false);

		//添加控件
		jFrame.add(la);
		jFrame.add(jtf);
		jFrame.add(choseButton);
		jFrame.add(computeButton);
		choseButton.addActionListener(buttLis);
		computeButton.addActionListener(buttLis);
		
		//设置
		jFrame.setBounds(250, 200, 600, 350);//frame初始显示位置
		jFrame.setSize(400, 100);
		jFrame.setVisible(true);
		jFrame.setResizable(false);//将frame设置成不能改变大小
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	/**
	 * main	函数
	 */
	public static void main(String[] args) {
		GUI gui = new GUI();
		gui.Frame();
	}

	/**
	 * 监听类
	 */
	class ButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent event) 
		{
			// TODO Auto-generated method stub
			if (event.getSource().equals(choseButton)) 
			{
				// 设置其只能选择文件
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				int state = jfc.showOpenDialog(null);
				if (state == 1) 
				{
					return;
				} 
				else 
				{
					srcFile = jfc.getSelectedFile();
					jtf.setText(srcFile.getAbsolutePath());
					filePath = srcFile.getPath();
				}
			}
			else
			{
				Convert con = new Convert();
				if (filePath == null)
				{
					JOptionPane.showMessageDialog(jFrame, "请先选择文件");
				} 
				else
				{
					try {
						con.convert2Number(filePath.replace(srcFile.getName(), ""), srcFile.getName(), "编号结果.txt");
						JOptionPane.showMessageDialog(jFrame, "编号完成!");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						JOptionPane.showMessageDialog(jFrame, e);
					}
				}
				
			}
		}

	}
}

你可能感兴趣的:(报文编号)