解答例 - 実習課題2 - 2.バイナリファイルの入出力
(実習課題2)
実習課題1のプログラムを改良しなさい。
- ファイルのコピーに「BufferedInputStream」「BufferedOutputStream」を使用するようにする事。
- コピーするファイルを「.gif」「.jpeg」など、画像ファイルに特化する事。
解答例
/**
* FilterCopyExample.java
* TECHSCORE Java 入出力2章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.io.chapter2.exercise2;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
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 JFileChooser chooser = new JFileChooser();
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));
getContentPane().add(from);
getContentPane().add(fromLabel);
getContentPane().add(to);
getContentPane().add(toLabel);
getContentPane().add(copy);
getContentPane().add(copyLabel);
from.addActionListener(this);
to.addActionListener(this);
copy.addActionListener(this);
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(new PictureFilter());
}
public static void main(String[] args) {
new FileCopyExample().setVisible(true);
}
//ボタンが押されたときの処理
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(from)) {
setFromFile();
} else if (e.getSource().equals(to)) {
setToFile();
} else if (e.getSource().equals(copy)) {
copyFile();
}
}
//コピー先ファイルを設定するメソッド
private void setToFile() {
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
toFile = chooser.getSelectedFile();
toLabel.setText(toFile.getAbsolutePath());
}
}
//コピー元ファイルを設定するメソッド
private void setFromFile() {
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
fromFile = chooser.getSelectedFile();
fromLabel.setText(fromFile.getAbsolutePath());
}
}
//コピーを実行するメソッド
private void copyFile() {
try {
BufferedInputStream input = new BufferedInputStream(new FileInputStream(fromFile));
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(toFile));
byte buf[] = new byte[256];
int len;
while ((len = input.read(buf)) != -1) {
output.write(buf, 0, len);
}
output.flush();
output.close();
input.close();
copyLabel.setText("コピーされました");
} catch (FileNotFoundException e) {
copyLabel.setText("指定されたファイルが見つかりません");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* PictureFilter.java
* TECHSCORE Java 入出力2章 実習課題2
*
* Copyright (c) 2004 Four-Dimensional Data, Inc.
*/
package com.techscore.io.chapter2.exercise2;
import java.io.File;
import javax.swing.filechooser.FileFilter;
public class PictureFilter extends FileFilter {
public boolean accept(File file) {
if (file != null) {
//ディレクトリ判定
if (file.isDirectory()) {
return true;
} else {
//拡張子判定
String ext = getExtension(file);
if (ext != null) {
if (ext.equals("bmp") || ext.equals("gif") || ext.equals("jpg")) {
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 "画像ファイル(*.bmp, *.gif, *.jpg)";
}
}

