図1に示すように、ソースを作成するため
、選択したストリーム2を
図3に示すように、操作(書き込み)
データは、メモリの存在を回避するために、キャッシュをフラッシュし、4
リソースの解放、5
ファイルf =新しいファイル( "D :/d/t.txt"); // ファイルが自動的に存在しない流れを作成し
たOutputStream OS =新しいのFileOutputStream(F、真)// ブール値trueを追加し、追加のモードを開きます、
デフォルトはfalseです。
バイト[]データ=のs.getBytes() // エンコード
os.write(バイト[]データは)//バイト配列の内容を書き込む
os.write(バイト[]データ、0 、長さ)// インデックスを書き込みます0コンテンツ長位置オフセット長
パブリッククラスのテスト{
public static void main(String[]args)
{
//创建源
File f =new File("D:/d/t.txt"); //文件不存在stream流会自动创建
//选择流
OutputStream os =null;
try {
os =new FileOutputStream(f,true);
//os =new FileOutputStream(f,true); //添加布尔类型true ,将会开启追加模式
//操作(写出),通过字节数组写出
String s="hello world";
byte[] data=s.getBytes();
try {
//os.write(data);
os.write(data,0,data.length);
//刷新数据,避免数据驻留在内存中
os.flush();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
finally {
//释放资源
try {
if(null!=os)
{
os.close();
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
}