IOフロー(2)
19581 ワード
Javaのオブジェクト向け バイトバッファストリームの概要と使用バイトバッファストリーム: 変換ストリームおよび符号化テーブル Stringクラスにおける符号化復号 変換ストリームにおける符号化および復号化 文字ストリーム書き込みデータの5つの方法 判定取得機能 IOストリームの概要と分類 バイトストリーム書き込みデータ 文字ストリーム読み出しデータ
バイトバッファフローの概要とバイトバッファフローの使用:
BufferedOutputStream:バイトバッファ出力ストリーム
BufferedInputStream:バイトバッファ入力ストリーム
BufferedOutputStream(OutputStream out)
この構築方法は、デフォルトのバッファ代理販売を提供し、一般的にデフォルトのバッファを使用すればよい.
バイト変換バッファストリームはバッファのみを提供しますが、実際の最下位レベルの読み書きデータは基本的なストリームオブジェクトが必要です.
実装コード:
変換フローとエンコーディングテーブル
変換ストリーム=バイトストリーム+エンコーディングテーブル
常用コード表:ASCII
GB2312
GBK
UTF8
文字化けし:同じデータの符号化と復号化に対する符号化テーブルが異なる
Stringクラスでの符号化復号
例:String s=「こんにちは」;
エンコーディング:
変換ストリームの符号化と復号化
OutputStreamWriter文字出力ストリーム
public OutputStreamWriter(OutputStreamout).デフォルトの符号化に基づいてバイトストリームを文字ストリームに変換
public OutputStreamWriter(OutputStreamout,String charsetName).指定されたエンコーディングに基づいてバイトストリームに変換
例:
InputStreamWriter文字入力ストリーム
public InputStreamWriter(InputStreamout).デフォルト符号化に従ってデフォルト符号化でデータを読み出す
public InputStreamWriter(InputStreamout,String charsetName).指定コードでデータを読み込む
文字ストリームのデータ書き込みの5つの方法
public void write (String str)
public void write(“hello”):
文字列を書く
public void write (String str,int off, int len)
osw.write(“hello”,0,3);文字配列の一部を書く
判断と取得機能
IOフローの概要と分類
流れ:
データ型:
バイトストリーム書き込みデータ
バイトストリームにデータを書き込むには:
A.バイトストリーム出力オブジェクトの作成方法
B.ライトデータを呼び出す方法(write()方法)
C.リソースの解放(close()方法)
3つの方法:
文字ストリーム読み出しデータ
バイトバッファフローの概要とバイトバッファフローの使用:
BufferedOutputStream:バイトバッファ出力ストリーム
BufferedInputStream:バイトバッファ入力ストリーム
BufferedOutputStream(OutputStream out)
この構築方法は、デフォルトのバッファ代理販売を提供し、一般的にデフォルトのバッファを使用すればよい.
バイト変換バッファストリームはバッファのみを提供しますが、実際の最下位レベルの読み書きデータは基本的なストリームオブジェクトが必要です.
実装コード:
BufferedOnputStream bos = new BufferedOnputStream(new FileOutputStream("a.txt"));
bos.write("hello.getBytes());
bos.close(); //
BufferedInputStream bos = new BufferedInputStream(new FileOutputStream("a.txt"));
int by ;
while((by=bis.read())!=-1){
System.out.print((char)by); //
}
変換フローとエンコーディングテーブル
変換ストリーム=バイトストリーム+エンコーディングテーブル
常用コード表:ASCII
GB2312
GBK
UTF8
文字化けし:同じデータの符号化と復号化に対する符号化テーブルが異なる
Stringクラスでの符号化復号
例:String s=「こんにちは」;
エンコーディング:
byte[ ] bys = s.getBytes("UTF-8"); // UTF8
System.out.println(Arrays.toString(bys));
:
String ss = new String(bys,“UTF-8”);
System.out.println(ss);
変換ストリームの符号化と復号化
OutputStreamWriter文字出力ストリーム
public OutputStreamWriter(OutputStreamout).デフォルトの符号化に基づいてバイトストリームを文字ストリームに変換
public OutputStreamWriter(OutputStreamout,String charsetName).指定されたエンコーディングに基づいてバイトストリームに変換
例:
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt") , "GBK")
osw.write(" "):
osw.close();
InputStreamWriter文字入力ストリーム
public InputStreamWriter(InputStreamout).デフォルト符号化に従ってデフォルト符号化でデータを読み出す
public InputStreamWriter(InputStreamout,String charsetName).指定コードでデータを読み込む
InputStreamWriter isw = new InputStreamWriter(new FileInputStream("a.txt") , "GBK")
int ch ;
while( (ch = isw.read() ) != -1{
System.out.print(char)ch);
isw.close();
文字ストリームのデータ書き込みの5つの方法
public void write(int c):
osw.write('a');
= ,
oswflush(); osw.close();
public void write (char[ ] cbuf)
char [ ] chs = {
'a','b','c','d'};
public void write(chs):
osw.write(chs);
public void write (char[ ] cbuf, int off, int len)
osw.write(chs,0,3);
public void write (String str)
public void write(“hello”):
文字列を書く
public void write (String str,int off, int len)
osw.write(“hello”,0,3);文字配列の一部を書く
判断と取得機能
isDirectory(
isFile():
exists( ):
getAbsoulte():
getPath():
getName():
IOフローの概要と分類
流れ:
データ型:
バイトストリーム書き込みデータ
バイトストリームにデータを書き込むには:
A.バイトストリーム出力オブジェクトの作成方法
B.ライトデータを呼び出す方法(write()方法)
C.リソースの解放(close()方法)
3つの方法:
public void write(int b):
public void write(byte[ ] b):
public void write(byte[ ] b,int offf, int len) :
:
FileOutputStream of = new FileOutputStream("b.txt"));
String getBytes( ) :
fos.write(65);
fos.write("ABCDE".getByetes());
fos.write("ABCDE".getByetes(),0,3);
fos.close();
文字ストリーム読み出しデータ
:
:
InputStreamReader isr = new InputStreamReader(new FileputStream("a.txt"));
int ch;
while((ch=isr.read()!=-1){
System.out.print((char))ch);
}
isr.close();
char[ ] chs =new char [1024];
int len;
while((len=isr.read (chs))!=-1){
System.out.print(new String(chs,0,len));
}
isr.close();