ファイルチューザを使う
戻る
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
/**
$Id: TextFileEditor.html,v 1.1 2009/06/22 16:12:00 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class TextFileEditor extends JFrame implements ActionListener {
private JButton openButton;
private JButton saveButton;
private JTextArea area;
public TextFileEditor() {
super( "テキストファイルのオープン・保存のテスト" );
setLayout( new BorderLayout() ); //コンテントペインにボーダーレイアウトのセット
setDefaultCloseOperation( EXIT_ON_CLOSE );
setSize( new Dimension( 300, 400 ) );
openButton = new JButton( "ファイルを開く" );
openButton.addActionListener( this );
openButton.setActionCommand( "OPEN" );
saveButton = new JButton( "ファイルを保存する" );
saveButton.addActionListener( this );
saveButton.setActionCommand( "SAVE" );
JPanel panel = new JPanel();
panel.add( openButton );
panel.add( saveButton );
area = new JTextArea( 10, 30 ); //10行30列のテキスト表示用テキストエリア
//スクロールペインを作成して、テキストエリアをセットする
JScrollPane areaScrollPane = new JScrollPane( area );
getContentPane().add( panel, BorderLayout.NORTH );
getContentPane().add( areaScrollPane, BorderLayout.SOUTH );
pack();
}
private class OnlyTextFilter extends javax.swing.filechooser.FileFilter {
// java.io.FileFilterと名前が衝突するので注意!
public boolean accept( java.io.File f ) {
// ディレクトリなら表示
if ( f.isDirectory() ) {
return true;
}
// ファイルなら拡張子がtxtのものだけを表示
String fileName = f.getName();
int extIndex = fileName.lastIndexOf( "." );
if ( extIndex >= 0 ) {
if ( fileName.substring( extIndex ).equals( ".txt" ) ) {
return true;
}
}
return false;
}
public String getDescription() {
return "テキストファイル(*.txt)";
}
}
public void actionPerformed( ActionEvent event ) {
String action = event.getActionCommand();
JFileChooser fileChooser = new JFileChooser( "." );
fileChooser.addChoosableFileFilter( new OnlyTextFilter() );
/** ファイルを開く */
if ( "OPEN".equals( action ) ) {
if ( fileChooser.showOpenDialog( this ) == JFileChooser.APPROVE_OPTION ) {
JOptionPane.showMessageDialog(
this,
fileChooser.getSelectedFile().getPath(),
"FILE PATH",
JOptionPane.INFORMATION_MESSAGE );
File file = new File( fileChooser.getSelectedFile().getPath() );
StringBuffer sb = new StringBuffer();
try {
BufferedReader br = new BufferedReader( new FileReader( file ) );
String line = null;
while ( ( line = br.readLine() ) != null ) {
sb.append( line + "\n" );
}
br.close();
} catch ( Exception e ) {
e.printStackTrace();
}
area.setText( sb.toString() );
} else {
JOptionPane.showMessageDialog( this, "You don't choose a file.", "WARNING", JOptionPane.WARNING_MESSAGE );
}
}
/** ファイルを保存する */
if ( "SAVE".equals( action ) ) {
if ( fileChooser.showSaveDialog( this ) == JFileChooser.APPROVE_OPTION ) {
JOptionPane.showMessageDialog(
this,
fileChooser.getSelectedFile().getPath(),
"FILE PATH",
JOptionPane.INFORMATION_MESSAGE );
String text = area.getText();
File file = new File( fileChooser.getSelectedFile().getPath() );
try {
BufferedWriter bw = new BufferedWriter( new FileWriter( file ) );
bw.write( text );
bw.close();
} catch ( Exception e ) {
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog( this, "You don't choose a file.", "WARNING", JOptionPane.WARNING_MESSAGE );
}
}
}
/** メインメソッド */
public static void main( String[] args ) {
new TextFileEditor().setVisible( true );
}
}
戻る