//************************************************************* // 文字コードの認識、指定コードでの出入力 // 「CharCodeTest」 // //          作 成 者:ルート高菜    //          作成開始月:2011/5 //          最終更新月:2011/5 [TN128/J118] //************************************************************* import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import org.mozilla.universalchardet.UniversalDetector; public class CharCodeTest extends JPanel implements ActionListener { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; //------------------------------------------------------------------ // public static void main(String[] args) { JFrame frame=new JFrame(); frame.getContentPane().add(new CharCodeTest()); frame.setTitle("CharCodeTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //右上×印で終了 frame.setSize(550,400); frame.setVisible(true); frame.setLocationRelativeTo(null); //画面の真ん中に表示 } //------------------------------------------------------------------ // File fileBefore; File fileAfter; // JTextField fieldBefore=new JTextField(10); JButton buttonBefore=new JButton("入力元"); JTextField fieldCode=new JTextField(7); JComboBox comboInput=new JComboBox(new String[]{"SHIFT-JIS","JIS","ISO-2022-JP","EUC-JP","UTF-8","UTF-16","UNICODE","JISAutoDetect"}); JButton buttonInput=new JButton("入力"); // JTextArea area=new JTextArea(); // JTextField fieldAfter=new JTextField(10); JButton buttonAfter=new JButton("出力先"); JComboBox comboOutput=new JComboBox(new String[]{"SHIFT-JIS","JIS","ISO-2022-JP","EUC-JP","UTF-8","UTF-16","UNICODE"}); JButton buttonOutput=new JButton("出力"); // //------------------------------------------------------------------コンストラクタ // public CharCodeTest(){ // fileBefore=fileAfter=null; // fieldBefore.setEditable(false); fieldCode.setEditable(false); fieldAfter.setEditable(false); // JPanel panelInput=new JPanel(); panelInput.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"入力")); //枠線 panelInput.add(buttonBefore); panelInput.add(fieldBefore); panelInput.add(fieldCode); panelInput.add(new JLabel(" ")); panelInput.add(comboInput); panelInput.add(buttonInput); // JPanel panelOutput=new JPanel(); panelOutput.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"出力")); //枠線 panelOutput.add(buttonAfter); panelOutput.add(fieldAfter); panelOutput.add(new JLabel(" ")); panelOutput.add(comboOutput); panelOutput.add(buttonOutput); // setLayout(new BorderLayout()); add(panelInput,BorderLayout.NORTH); add(area,BorderLayout.CENTER); add(panelOutput,BorderLayout.SOUTH); // buttonBefore.addActionListener(this); buttonInput.addActionListener(this); buttonAfter.addActionListener(this); buttonOutput.addActionListener(this); } //------------------------------------------------------------------イベント // public void actionPerformed(ActionEvent e) { Object object=e.getSource(); if(object==buttonBefore){ //テキストファイルを指定し、文字コードを調べて読み込み //ダイアログ準備 JFileChooser chooser=new JFileChooser(fileBefore); // if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){ fileBefore=chooser.getSelectedFile(); fieldBefore.setText(fileBefore.getName()); // String code=checkCode(); //文字コード取得 if(code!=null){ //得られたコードで読み込み fieldCode.setText(code); // readText(code); }else{ //エラー fieldCode.setText(""); area.setText(""); } } }else if(object==buttonInput){ //文字コードを渡して再読み込み readText( (String)comboInput.getSelectedItem() ); }else if(object==buttonAfter){ //出力先ファイルを取得 //ダイアログ準備 JFileChooser chooser=new JFileChooser(fileBefore); // if(chooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){ fileAfter=chooser.getSelectedFile(); fieldAfter.setText(fileAfter.getName()); } }else if(object==buttonOutput){ //文字コードを渡して保存 writeText( (String)comboOutput.getSelectedItem() ); } } //------------------------------------------------------------------ //指定の文字コードで読み込み private void readText(String code){ // if(fileBefore==null){ JOptionPane.showMessageDialog(this,"ファイル未指定","エラー",JOptionPane.ERROR_MESSAGE); return; } // try{ BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(fileBefore),code)); // String line; area.setText(""); while((line=reader.readLine())!=null){ area.append(line+"\n"); //改行コード:Java上では\nに(JtextAreaで改行するとそうなるから) } reader.close(); }catch(FileNotFoundException fnfe){ JOptionPane.showMessageDialog(this,fnfe.getMessage(),"エラー",JOptionPane.ERROR_MESSAGE); }catch(UnsupportedEncodingException uee){ JOptionPane.showMessageDialog(this,uee.getMessage(),"エラー",JOptionPane.ERROR_MESSAGE); }catch(IOException ioe){ JOptionPane.showMessageDialog(this,ioe.getMessage(),"エラー",JOptionPane.ERROR_MESSAGE); } } //指定の文字コードで書き込み private void writeText(String code){ // if(fileAfter==null){ JOptionPane.showMessageDialog(this,"ファイル未指定","エラー",JOptionPane.ERROR_MESSAGE); return; } // try{ String line=area.getText(); line=line.replaceAll("\n", System.getProperty("line.separator")); //改行コード:ファイル上ではメニューで選んだもの(初期値はシステムのもの) // PrintWriter writer=new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileAfter),code)); writer.print(line); writer.close(); }catch(FileNotFoundException fnfe){ JOptionPane.showMessageDialog(this,fnfe.getMessage(),"エラー",JOptionPane.ERROR_MESSAGE); }catch(UnsupportedEncodingException uee){ JOptionPane.showMessageDialog(this,uee.getMessage(),"エラー",JOptionPane.ERROR_MESSAGE); } } //文字コード認識 private String checkCode(){ // if(fileBefore==null){ JOptionPane.showMessageDialog(this,"ファイル未指定","エラー",JOptionPane.ERROR_MESSAGE); return null; } // try { UniversalDetector detector=new UniversalDetector(null); FileInputStream fis=new FileInputStream(fileBefore); // byte[] buf=new byte[4096]; int nread; while((nread=fis.read(buf))>0 && !detector.isDone()){ detector.handleData(buf, 0, nread); } detector.dataEnd(); // String encoding=detector.getDetectedCharset(); if(encoding==null){ //不明なら(アルファベットのみでも)自動認識「JISAutoDetect」を使う JOptionPane.showMessageDialog(this,"エンコーディング不明\nJISAutoDetectを使用","エラー",JOptionPane.ERROR_MESSAGE); encoding="JISAutoDetect"; } // detector.reset(); // fis.close(); // return encoding; //文字コードを返す } catch (FileNotFoundException fnfe) { JOptionPane.showMessageDialog(this,fnfe.getMessage(),"エラー",JOptionPane.ERROR_MESSAGE); return null; //エラーならnullを返す } catch (IOException ioe) { JOptionPane.showMessageDialog(this,ioe.getMessage(),"エラー",JOptionPane.ERROR_MESSAGE); return null; //エラーならnullを返す } } }