SWT/JFace ファイルの読み書き
色々とネットを放浪してみた結果、なんとかそれっぽいのはできました。
ちょうどいいサンプルが無かったもので、対象ファイル名を取得するところまではほとんどコピペなんですが、実際の読み書き部分は自分で作るはめに。
いや、それも面白くてよかったのですが、出来上がったものを見てみるとなんだかお粗末な代物に。
こんなのでいいのか……?
String型変数に千行弱のテキストデータを格納できたことにはかなり感動しました。終わる。
一応出来上がったものを晒しときます。意見があればどうぞ。
OpenAction.java
package myeditor.action; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import myeditor.MyEditor; import org.eclipse.jface.action.*; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; public class OpenAction extends Action { private MyEditor myEditor; // ファイルダイアログ用のフィルタ拡張子 private final String[] EXTENSIONS = { "*.eve", "*.txt", "*" }; public OpenAction(MyEditor myEditor) { this.myEditor = myEditor; setText("開く(&O)@Ctrl+O"); } public void run() { // ファイル名を取得 String fileName = getFileName(); // ファイル名を取得できたら if(fileName != null){ loadFile(fileName); } } private String getFileName() { FileDialog openDialog = new FileDialog(myEditor.getShell(), SWT.OPEN); openDialog.setFilterExtensions(EXTENSIONS); return openDialog.open(); } private void loadFile(String fileName) { BufferedReader reader = null; try { File file = new File(fileName); // ファイルが存在しなかったら if (!file.exists()) { // 終了 return; } // ファイルを開く reader = new BufferedReader(new FileReader(file)); String line = null; String doc = ""; while ((line = reader.readLine()) != null) { doc += line + '\n'; } myEditor.document.set(doc); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // 何もしない } } } } }
SaveAction.java
package myeditor.action; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import myeditor.MyEditor; import org.eclipse.jface.action.Action; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; public class SaveAction extends Action { private MyEditor myEditor; // ファイルダイアログ用のフィルタ拡張子 private final String[] EXTENSIONS = { "*.eve", "*.txt", "*" }; public SaveAction(MyEditor myEditor) { this.myEditor = myEditor; setText("名前を付けて保存(&A)@Ctrl+S"); } public void run() { // ファイル名を取得 String fileName = getFileName(); // ファイル名を取得できたら if(fileName != null){ saveFile(fileName); } } private String getFileName() { FileDialog openDialog = new FileDialog(myEditor.getShell(), SWT.SAVE); openDialog.setFilterExtensions(EXTENSIONS); return openDialog.open(); } private void saveFile(String fileName) { BufferedWriter writer = null; try { File file = new File(fileName); // 指定されたファイルが存在していたら if (file.exists()) { MessageDialog dialog = new MessageDialog( myEditor.getShell(), "選択してください", null, "指定されたファイルは存在します。\n上書きしますか?", MessageDialog.QUESTION, new String[]{"はい", "いいえ"}, 1 ); int ret = dialog.open(); if (ret == 1) return; } // ファイルを開く writer = new BufferedWriter(new FileWriter(file)); // ドキュメントを文字列型に変更 String doc = myEditor.document.get(); // ファイルに書き込む writer.write(doc); } catch (IOException e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { // 何もしない } } } } }