1, to create the source
2, select stream
3, operation (write)
4, flush the cache, the data to avoid the presence of memory
5, the release of resources
File f = new File ( "D : /d/t.txt"); // file does not exist automatically creates a flow stream
OutputStream os = new FileOutputStream (f, true) // add Boolean true, will open an additional mode,
The default is false.
byte [] data = s.getBytes () // encoding
os.write (byte [] data) // write the contents of the byte array
os.write (byte [] data, 0 , length) // write the index 0 content length position offset length
public class test{
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();
}
}
}
}