解答例 - 実習課題3 - 2.バイナリファイルの入出力
(実習課題3)
実習課題2のプログラムを改良しなさい。
- 画像ファイルのコピーだった機能を、指定の文字コードで書かれたテキストファイルを指定の文字コードに変換してコピーする機能に変えなさい。
- ファイルの拡張子は「.txt」。
- サポートする文字コードは「EUC_JP」「SJIS」「ISO2022JP」の3つで良い。
解答例
/**
* FilterCopyExample.java
* TECHSCORE Java 入出力2章 実習課題3
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.io.chapter2.exercise3;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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 javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class FileCopyExample extends JFrame implements ActionListener {
private JButton from = new JButton("コピー元");
private JButton to = new JButton("コピー先");
private JButton copy = new JButton("コピー実行");
private JLabel fromLabel = new JLabel("コピー元ファイル");
private JLabel toLabel = new JLabel("コピー先ファイル");
private JLabel copyLabel = new JLabel();
//文字コード指定用ラジオボタン
private JRadioButton eucFrom = new JRadioButton("EUC-JP");
private JRadioButton sjisFrom = new JRadioButton("SJIS", true);
private JRadioButton iso2022jpFrom = new JRadioButton("ISO2022JP");
private JRadioButton eucTo = new JRadioButton("EUC-JP");
private JRadioButton sjisTo = new JRadioButton("SJIS", true);
private JRadioButton iso2022jpTo = new JRadioButton("ISO2022JP");
private JFileChooser chooser = new JFileChooser();
private String fromCharCode = "SJIS";
private String toCharCode = "SJIS";
private File fromFile = null;
private File toFile = null;
//コンストラクタ
public FileCopyExample() {
super("ファイルのコピー");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 200);
getContentPane().setLayout(new GridLayout(6, 1));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
ButtonGroup fromGroup = new ButtonGroup();
ButtonGroup toGroup = new ButtonGroup();
fromGroup.add(eucFrom);
fromGroup.add(sjisFrom);
fromGroup.add(iso2022jpFrom);
toGroup.add(eucTo);
toGroup.add(sjisTo);
toGroup.add(iso2022jpTo);
panel1.add(from);
panel1.add(eucFrom);
panel1.add(sjisFrom);
panel1.add(iso2022jpFrom);
panel2.add(to);
panel2.add(eucTo);
panel2.add(sjisTo);
panel2.add(iso2022jpTo);
getContentPane().add(panel1);
getContentPane().add(fromLabel);
getContentPane().add(panel2);
getContentPane().add(toLabel);
getContentPane().add(copy);
getContentPane().add(copyLabel);
from.addActionListener(this);
to.addActionListener(this);
copy.addActionListener(this);
eucFrom.addActionListener(this);
sjisFrom.addActionListener(this);
iso2022jpFrom.addActionListener(this);
eucTo.addActionListener(this);
sjisTo.addActionListener(this);
iso2022jpTo.addActionListener(this);
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(new TextFilter());
}
public static void main(String[] args) {
new FileCopyExample().setVisible(true);
}
//ボタンが押されたときの処理
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(from)) {
fromCopyFile();
} else if (e.getSource().equals(to)) {
toCopyFile();
} else if (e.getSource().equals(copy)) {
copyFile();
} else if (e.getSource().equals(eucFrom)) {
fromCharCode = "EUC-JP";
} else if (e.getSource().equals(sjisFrom)) {
fromCharCode = "SJIS";
} else if (e.getSource().equals(iso2022jpFrom)) {
fromCharCode = "ISO2022JP";
} else if (e.getSource().equals(eucTo)) {
toCharCode = "EUC-JP";
} else if (e.getSource().equals(sjisTo)) {
toCharCode = "SJIS";
} else if (e.getSource().equals(iso2022jpTo)) {
toCharCode = "ISO2022JP";
}
}
//コピー先ファイルを設定するメソッド
private void toCopyFile() {
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
toFile = chooser.getSelectedFile();
toLabel.setText(toFile.getAbsolutePath());
}
}
//コピー元ファイルを設定するメソッド
private void fromCopyFile() {
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
fromFile = chooser.getSelectedFile();
fromLabel.setText(fromFile.getAbsolutePath());
}
}
//コピーを実行するメソッド
private void copyFile() {
try {
BufferedReader reader =
new BufferedReader(
new InputStreamReader(new FileInputStream(fromFile), fromCharCode));
BufferedWriter writer =
new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(toFile), toCharCode));
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
writer.flush();
writer.close();
reader.close();
copyLabel.setText("コピーされました");
} catch (FileNotFoundException e) {
copyLabel.setText("指定されたファイルが見つかりません");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* TextFilter.java
* TECHSCORE Java 入出力2章 実習課題3
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.io.chapter2.exercise3;
import java.io.File;
import javax.swing.filechooser.FileFilter;
public class TextFilter extends FileFilter {
public boolean accept(File file) {
if (file != null) {
//ディレクトリ判定
if (file.isDirectory()) {
return true;
} else {
//拡張子判定
String ext = getExtension(file);
if (ext != null && ext.equals("txt")) {
return true;
}
}
}
//trueが返らなかったとき
return false;
}
//拡張子を取得するメソッド
private String getExtension(File file) {
if (file == null) {
return null;
} else {
//ファイル名を取得
String name = file.getName();
//最後のピリオド位置を取得
int period = name.lastIndexOf('.');
if (period > 0 && period < name.length() - 1) {
//拡張子を小文字で返す
return name.substring(period + 1).toLowerCase();
} else {
return null;
}
}
}
public String getDescription() {
return "テキストファイル(*.txt)";
}
}

