java _io_文件输出

1、创建源
2、选择流
3、操作(写出)
4、刷新缓存,避免数据驻留内存
5、释放资源

File f =new File("D:/d/t.txt"); //文件不存在stream流会自动创建
OutputStream os=new FileOutputStream(f,true) //添加布尔类型true,会开启追加模式,
认为false。
byte[] data =s.getBytes() //编码
os.write(byte[] data) //写出字节数组的内容
os.write(byte[] data,length) //写出从索引位置0处偏移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,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();
        }

    }

}

}

相关文章

Java中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...