view src/main/java/christie/textEditor/textFrame.java.orig @ 230:fddd6f6e7693

tweak
author ichikitakahiro <e165713@ie.u-ryukyu.ac.jp>
date Tue, 21 Jan 2020 19:36:20 +0900
parents
children
line wrap: on
line source

package christie.textEditor;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JInternalFrame;
import javax.swing.JTextArea;



public class textFrame extends JInternalFrame{
    private JTextArea textArea;

    private static String DEFAULT_CHARACTER_CODE = "Shift_JIS";

    public textFrame(){
        this("新規テキスト");
    }

    public textFrame(String title){
        //JInternalFrameのコンストラクタの呼び出しを実行
        super(title, true, true, true , true);
        //サイズの指定
        this.setSize(800,600);
        // JTextArea(テキスト入力のコンポーネントを追加する。)
        textArea = new JTextArea();
        this.add(textArea);
    }

    public textFrame(File file) throws IOException {
    this(file.getName());
    this.openFile(file);
    }

    void openFile(File file) throws IOException{
        FileInputStream fiStream = null;
        ByteArrayOutputStream baoStream = null;
        try {
            fiStream = new FileInputStream(file);
            baoStream = new ByteArrayOutputStream();
            //読み込みデータ格納用配列
            byte[] byteData = new byte[1];
            int ret = fiStream.read(byteData);
            //ファイルの最後まで繰り返す。
            while (ret != -1) {
                baoStream.write(byteData);
                ret = fiStream.read(byteData);
            }
            //バイト配列を文字列に変換
            String text = new String(baoStream.toByteArray(), DEFAULT_CHARACTER_CODE);
            //テキストGUIに読み込んだファイルの内容を設定
            textArea.setText(text);
            //タイトルを開いたファイル名へ変更
            this.setTitle(file.getName());
        }finally{
            if(fiStream != null){
                fiStream.close();
            }
            if(baoStream != null){
                baoStream.close();
            }
        }
    }
}