- FileOutputStream实现了向文件写出byte数据的方法
案例:FileOutDemo1.java
public class FileOutDemo1{
public static void main(String args)throws IOException{
//如果该文件不存在,则直接创建,如果存在,删除后创建
FlieOutputStream out = new FileOutputStram("demo /out.dat")//("demo /out.dat",true)//如果要追加内容加true。
out.write('A');//写出了‘A’的低八位
out.write('B')//写出了'B'的低八位
int a = 10;//write只能写八位,那么写一个int需要写4次每次8位
out.write(a>>>24);
out.write(a>>>16);
out.write(a>>>8);
out.write(a);
byte[] gbk = "中国",getBytes("gbk");
out.write(gbk);
out.close();
Ioutil.printHex(''demo/out.dat");
}
}
IOutil.java
public static void copyFile(File srcFile,file destFile)throws IOException{
if(!srcFile.exists()){
tjrow new IllegalArgumentException("文件:"+srcFile+"不存在");
}
if(!srcFile.isFile()){
throw new IllegalArgumentException(srcFile+"不是文件");
}
FiieIputStream in = new FileInputStream(srcfile);
byte[] buf = new byte[8*1024];
int b;
while((b = in.read(buf,0,buf.length))!=-1){
out.write(buf,0,b);
out.flush();//最好加上
}
in.close();
out.close();
}
IOUtilTest3.java
public ststic void main (String[] args){
try{
IOUtil.copyFile(new File("e:\\javaio\\imooc.txt"),new File(''e:\\javaio\\imood.tat");
}catch(IOException e){
e.printStackTrace();
}
}
}
网友评论