Javaでのファイル入出力
■ファイル読込の基本形
1)FileInputStreamのインスタンス生成
FileInputStream fis = new FileInputStream("abc.txt");
2)InputStreamReaderのインスタンス生成
InputStreamReader isr = new InputStreamReader(fis , "MS932");
3)BufferedReaderのインスタンス生成
BufferedReader br = new BufferedReader(isr);
4)一行読込
String str = br.readLine();
5)順に閉じていく
fis.close();
isr.close();
fis.close();
■ファイル出力の基本形
1)FileOutputStreamのインスタンス生成
FileOutputStream fos = new FileOutputStream("abc.txt");
2)OutputStreamWriterのインスタンス生成
OutputStreamWriter osw = new OutputStreamWriter(fos , "MS932");
3)BufferedWriterのインスタンス生成
BufferedWriter bw = new BufferedWriter(osw);
4)一行書き込み
bw.write("あいうえお");
5)順に閉じていく
bw.close();
osw.close();
fos.close();
■各クラスの使用目的
■flushについて
■テキストファイル入出力専用のクラスで、ファイル読込
1)Fileのインスタンス生成
File inFile = new File("abc.txt");
2)FileReaderのインスタンス生成
FileReader in = new FileReader(inFile);
3)データがなくなるまで、1バイトずつ読み込む
int data =0;
while ((data = in.read()) != -1) {
}
4)読込ストリームを閉じる
in.close();
■テキストファイル入出力専用のクラスで、ファイル出力
1)Fileのインスタンス生成
File outFile = new File("xyz.txt");
2)FileWriterのインスタンス生成
FileWriter out = new FileWriter(outFile);
3)1バイトずつ、データを書き込む
out.write(data);
4)書き込みストリームを閉じる
out.close( );
■オブジェクトの入出力
1)FileOutputStreamのインスタンス生成
FileOutputStream outFile = new FileOutputStream("hello.txt");
2)ObjectOutputStreamのインスタンス生成
ObjectOutputStream outObject = new ObjectOutputStream(outFile);
3)HelloクラスをObjectOutputStreamに書き込む
Hello hello = new hello();
outObject.writeObject(hello);
※シリアライズ化されたHelloクラスがhello.txtに書き込まれる。
ファイルを開いても、文字化けしたものが見えるだけ。
4)出力ストリームを閉じる
outObject.close();
outFile.close();
5)FileInputStreamのインスタンス生成
FileInputStream inFile = new FileInputStream("hello.txt");
6)ObjectInputStreamのインスタンス生成
ObjectInputStream inObject = new ObjectInputStream(inFile);
7)helloクラスを読み込む
Hello exHello = (Hello)inObject.readObject();
8)helloクラスのメソッド呼び出し
exHello.sayHello();
9)入力ストリームを閉じる
inObject.close();
inFile.close();
■ディレクトリを指定して、ファイル出力する場合
FileOutputStream fos = new FileOutputStream("C:\\abc.txt");
■strutsを用いた場合のファイルアップロード
1)JSP:ファイル取込ボタンを付ける
<html:form action="/UpLoadAction" enctype="multipart/form-data">
<html:file property="upLoadFile" />
<html:submit property="submit" value="送信"/>
</html:form>
2)ActionForm:フォームファイルクラスを保持する
private FormFile upLoadFile
3)Action:フォームファイルを入力ストリームに連結
FormFile file = upLoadForm.getUpLoadFile();
InputStream is = file.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
※最後に、一時領域のアップロードデータを削除
file.destroy();
■Jakarta-Commonsを使用しないファイルアップロード
<form action="fileUploadServlet" enctype="multipart/form-data" method="post">
ファイル名:<input type="file" name="fileName">
<input type="submit" value="送信">
</form>
1)FileInputStreamのインスタンス生成
FileInputStream fis = new FileInputStream("abc.txt");
2)InputStreamReaderのインスタンス生成
InputStreamReader isr = new InputStreamReader(fis , "MS932");
3)BufferedReaderのインスタンス生成
BufferedReader br = new BufferedReader(isr);
4)一行読込
String str = br.readLine();
5)順に閉じていく
fis.close();
isr.close();
fis.close();
■ファイル出力の基本形
1)FileOutputStreamのインスタンス生成
FileOutputStream fos = new FileOutputStream("abc.txt");
2)OutputStreamWriterのインスタンス生成
OutputStreamWriter osw = new OutputStreamWriter(fos , "MS932");
3)BufferedWriterのインスタンス生成
BufferedWriter bw = new BufferedWriter(osw);
4)一行書き込み
bw.write("あいうえお");
5)順に閉じていく
bw.close();
osw.close();
fos.close();
■各クラスの使用目的
- バイトデータのまま入出力する場合
- 文字エンコーディングをして入出力する場合
- バッファリングをして入出力する場合
■flushについて
- OutputStream#flush・・・出力ストリームは閉じずに、バッファ内の内容をディスクに書き出す
- OutputStream#close・・・バッファ内にデータが残っている場合は、ディスクに書き込んでから、出力ストリームを閉じる。
■テキストファイル入出力専用のクラスで、ファイル読込
1)Fileのインスタンス生成
File inFile = new File("abc.txt");
2)FileReaderのインスタンス生成
FileReader in = new FileReader(inFile);
3)データがなくなるまで、1バイトずつ読み込む
int data =0;
while ((data = in.read()) != -1) {
}
4)読込ストリームを閉じる
in.close();
■テキストファイル入出力専用のクラスで、ファイル出力
1)Fileのインスタンス生成
File outFile = new File("xyz.txt");
2)FileWriterのインスタンス生成
FileWriter out = new FileWriter(outFile);
3)1バイトずつ、データを書き込む
out.write(data);
4)書き込みストリームを閉じる
out.close( );
■オブジェクトの入出力
1)FileOutputStreamのインスタンス生成
FileOutputStream outFile = new FileOutputStream("hello.txt");
2)ObjectOutputStreamのインスタンス生成
ObjectOutputStream outObject = new ObjectOutputStream(outFile);
3)HelloクラスをObjectOutputStreamに書き込む
Hello hello = new hello();
outObject.writeObject(hello);
※シリアライズ化されたHelloクラスがhello.txtに書き込まれる。
ファイルを開いても、文字化けしたものが見えるだけ。
4)出力ストリームを閉じる
outObject.close();
outFile.close();
5)FileInputStreamのインスタンス生成
FileInputStream inFile = new FileInputStream("hello.txt");
6)ObjectInputStreamのインスタンス生成
ObjectInputStream inObject = new ObjectInputStream(inFile);
7)helloクラスを読み込む
Hello exHello = (Hello)inObject.readObject();
8)helloクラスのメソッド呼び出し
exHello.sayHello();
9)入力ストリームを閉じる
inObject.close();
inFile.close();
■ディレクトリを指定して、ファイル出力する場合
FileOutputStream fos = new FileOutputStream("C:\\abc.txt");
■strutsを用いた場合のファイルアップロード
1)JSP:ファイル取込ボタンを付ける
<html:form action="/UpLoadAction" enctype="multipart/form-data">
<html:file property="upLoadFile" />
<html:submit property="submit" value="送信"/>
</html:form>
2)ActionForm:フォームファイルクラスを保持する
private FormFile upLoadFile
3)Action:フォームファイルを入力ストリームに連結
FormFile file = upLoadForm.getUpLoadFile();
InputStream is = file.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
※最後に、一時領域のアップロードデータを削除
file.destroy();
■Jakarta-Commonsを使用しないファイルアップロード
<form action="fileUploadServlet" enctype="multipart/form-data" method="post">
ファイル名:<input type="file" name="fileName">
<input type="submit" value="送信">
</form>
2006年06月09日(金) 22:24:18 Modified by alivealive