java FileStream文件流操作

来源:互联网 发布:unity3d声音不会衰减 编辑:程序博客网 时间:2024/05/16 11:58

直接上代码,函数使用说明详见Java API文档

import java.io.*;public class StreamDemo{public static void main(String[] args){File f=new File("F:\\workspace\\JavaPrj\\test.txt");FileOutputStream out=null;try {out=new FileOutputStream(f);byte[] b=new String("Hello,my name is cjc").getBytes();System.out.println("\"Hello,my name is cjc\"has been writeen into "+f.getName()+".");out.write(b);out.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}try{FileInputStream in=new FileInputStream(f);byte b[]=new byte[1024];int cnt=0;cnt=in.read(b);//注意这里byte转化成String的方法System.out.println("\""+new String(b,0,cnt)+"\"has been read from "+f.getName()+".");}catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


1 0