java Io流更新文件内容

时间:2024-01-05 13:44:56

package com.hp.io;

import java.io.FileOutputStream;

import java.io.IOException;

public class FileOutputStreamTest{

public static void main(String []args){

FileOutputStream fos=null;    //文件输出流

try{

String str="java从入门到放弃";

byte [] words =str.getBytes();   //创建一个中转存放字节

fos =new FileOutputStream("f:\\入门.txt");  //可以选择已有的也可以随便写一个 会自动创建一个文件

fos.write(words,0,words.length);  // 开始写入  words中的数据  数据大小从0到words的长度,注意这里不是求最大值 不是words.length-1

System.out.println("文件已更新");

}catch(IOException e){

System.out.print("创建文件时出错");

}finally{

try{

if(fos!=null)

fos.close();

}catch(IOException e){

e.printStackTrace();

}

}

}

}