Java学習->ファイルを開くと保存します。
8122 ワード
TextArea
Javaテキストエリアを表し、テキストエリアはユーザーが複数行のテキストを入力することをサポートします。TextArea():作成方法により、テキストエリアオブジェクトの行数と列数を作成し、デフォルト値をとります。
TextAreaによくある方法 public void set Text(String s)設定テキストエリアのテキストはs です。 public void get Text()は、テキストエリアのテキスト を取得する。 public voidアプリ(String s)は、テキストの末尾にテキスト を追加する。 public void addText Listener(Text Listener)がテキストエリアにテキストモニタを追加するには、もちろん多くの方法がありますが、ここでは詳細なリストはありません。未記入のいくつかの方法はデフォルトで表示されます。標準テキストは編集できます。
テキストエリアのイベント テキストエリアの内容が変化すると、TextEventクラスは自動的にイベントオブジェクトを作成します。 TextEventイベントが発生したイベントソースをモニタに取得する方法は、addTextListener(モニタ)である。 TextEventイベントが発生したインターフェースはTextListenerであり、インターフェースに書き換えが必要な方法は、textValue Chend(TextEvent e) である。
例:TextAreaの使用
ファイルをロードまたは保存するために、指定されたタイトルのファイルダイアログウィンドウを作成します。コンストラクタ:FileDialog(Frame mf,String title,int mode):指定されたタイトルでファイルを読み込みまたは保存するファイルダイアログウィンドウを作成します。mfはフォームオブジェクトを表し、フォームオブジェクトがファイルダイアログを持つことを示し、titleはファイルダイアログウィンドウのタイトル名を表します。modeはダイアログモードで、2つのタイプがあります。 FileDialog.LOADの定数値は、どこからファイルを読み込むかを決定する。 FileDialog.SAVEはどこにファイルを書き込むかを決定します。 FileDialog一般的な方法 get Directory()は、ファイルダイアログのディレクトリを取得します。 get File()は、ファイルダイアログで選択されたファイル を取得する。 set Directory(String s)は、ファイルダイアログウィンドウのディレクトリを指定ディレクトリ に設定します。 set File(String s)は、ファイルダイアログウィンドウの選択ファイルを指定ファイル に設定する。
ファイルパス=get Directory()+get File()
BufferedReaderとBufferedWriter
これらの2つのクラスはそれぞれ8192文字のバッファを持っています。BufferedReaderがテキストファイルを読み込む時は、なるべくファイルから文字データを読み込んでバッファに入れます。その後、read()方法を使うと、バッファから先に読み取ります。バッファでデータが足りない場合は、ファイルから読み込みます。BufferedWriterを使用する場合、書き込んだデータは目的地に先に出力されるのではなく、まずバッファに格納されます。バッファの中のデータがいっぱいになったら、目的地に対して書くことができます。readLine()メソッドは、使用者の改行文字を読み取ったときに、全行の文字を入力します。
FileReader
ストリーム中のデータを文字で読みだします。構造方法: FileReader(File file)は、データを読み出すFileが与えられた場合、新しいFileReaderを作成する。 FileReader(String filename)は、データを読み出すファイル名を与えられた場合、新しいFileReaderを作成する。 FileReader常用方法 public int read()throws IOExceptionは、1つのint型変数を返して、読み取った文字を表します。 public int read(char[]c,int offset,int len)は文字をc配列に読み出し、文字の個数に戻る。 例:ファイルを開く操作
Javaテキストエリアを表し、テキストエリアはユーザーが複数行のテキストを入力することをサポートします。TextArea():作成方法により、テキストエリアオブジェクトの行数と列数を作成し、デフォルト値をとります。
TextAreaによくある方法
テキストエリアのイベント
例:TextAreaの使用
//
// , ,
textArea = new TextArea();
//
textArea.setBounds(0, 0, mf.getWidth(),mf.getHeight());
textArea.setBackground(Color.WHITE);//
//new Font(String ,int ,int ),Font.PLAIN 0:
textArea.setFont(new Font(" ",0,16));
mf.add(textArea);
mf.setVisible(true);
FileDialogファイルをロードまたは保存するために、指定されたタイトルのファイルダイアログウィンドウを作成します。コンストラクタ:FileDialog(Frame mf,String title,int mode):指定されたタイトルでファイルを読み込みまたは保存するファイルダイアログウィンドウを作成します。mfはフォームオブジェクトを表し、フォームオブジェクトがファイルダイアログを持つことを示し、titleはファイルダイアログウィンドウのタイトル名を表します。modeはダイアログモードで、2つのタイプがあります。
ファイルパス=get Directory()+get File()
BufferedReaderとBufferedWriter
これらの2つのクラスはそれぞれ8192文字のバッファを持っています。BufferedReaderがテキストファイルを読み込む時は、なるべくファイルから文字データを読み込んでバッファに入れます。その後、read()方法を使うと、バッファから先に読み取ります。バッファでデータが足りない場合は、ファイルから読み込みます。BufferedWriterを使用する場合、書き込んだデータは目的地に先に出力されるのではなく、まずバッファに格納されます。バッファの中のデータがいっぱいになったら、目的地に対して書くことができます。readLine()メソッドは、使用者の改行文字を読み取ったときに、全行の文字を入力します。
FileReader
ストリーム中のデータを文字で読みだします。構造方法:
FileDialog dia_Open = new FileDialog(mf," ",FileDialog.LOAD);
dia_Open.setVisible(true);//
BufferedReader in;//
textArea.setText(null);//
/*
* dia_Open.getDirectory():
* dia_Open.getFile():
* dia_Open.getDirectory()+dia_Open.getFile():
*/
try {
in = new BufferedReader(new FileReader(dia_Open.getDirectory()+dia_Open.getFile()));
try {
while((str = in.readLine())!=null) {//in.readLine(): ,
c = str.toCharArray();//
a = new int[str.length()];// ASCII
str = String.valueOf(c);// char String
textArea.append(str+"
");//
}
in.close();//
mf.setTitle(dia_Open.getFile()+" - ");
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
例:ファイルの保存操作 FileDialog dia_Save = new FileDialog(mf," ",FileDialog.SAVE);
dia_Save.setVisible(true);
FileOutputStream out;//
// txt ,
try {
out = new FileOutputStream(dia_Save.getDirectory()+dia_Save.getFile()+".txt");
str = textArea.getText().replace("
", "\r
");// , "
" "\r
",
c = str.toCharArray();// char[]
a = new int[str.length()];// ASCII
str = String.valueOf(c);// char String
try {
out.write(str.getBytes());
out.flush();
out.close();
mf.setTitle(dia_Save.getFile()+" - ");
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}