11月4号

昨天上了一下午的Java课,主要学了Java的文件流,包括文件的一些操作,打开文件,读写文件创建缓冲区等。

public static void main(String [] args) throws IOException{
//创建字节输入流
FileInputStream fis= new FileInputStream("FileInputStreamTest.java");
//创建一个长度为1024的竹筒
byte[] bbuf =new byte[1024];
//用于保存实际读取的字节数
int hasRead=0;
//使用循环来重复“取水”过程
while((hasRead =fis.read(bbuf))>0){
//取出“竹筒”中水滴(字节),将字节数组转换成字符串输入
System,out.println(bbuf,0,hasRead)
}
fis.close();
} 
 public static void main(String[] args) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //创建字节输入流
            fis = new FileInputStream("FileOutputStreamTest.java");
            //创建字节输入流
            fos = new FileOutputStream("newFile.txt");
            byte[] bbuf = new byte[32];
            int hasRead = 0;
            //循环从输入流中取出数据
            while ((hasRead = fis.read(bbuf)) > 0) {
                //每读取一次,即写入文件输出流,读了多少,就写多少。
                fos.write(bbuf, 0, hasRead);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            //使用finally块来关闭文件输入流
            if (fis != null) {
                fis.close();
            }
            //使用finally块来关闭文件输出流
            if (fos != null) {
                fos.close();
            }
        }
    }

  

原文地址:https://www.cnblogs.com/12248H/p/13928623.html