文件流初识------笔记

import java.io.*;
public class newcollection {
    public static void main(String args[]) 
    {
        File file=new File("D://1//WORLD.txt");//创建文件对象
        try
        {
            if(!file.exists()) //判断文件是否存在
            {
                file.createNewFile();//不存在时新建
            }
            FileOutputStream out=new FileOutputStream(file);//定义FileOutputStream对象
            byte buy[]="IO文件流初次使用!".getBytes();
            out.write(buy);//将buy字节写入流(写入文件)
            out.close();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try
        {
            FileInputStream in=new FileInputStream(file);
            byte a[]=new byte[1204];
            //--------------------------------------------len的用处?int?string
            int len=in.read(a);//从文件中读取信息
            System.out.println("IO流中的数据:"+new String(a,0,len));
            in.close();
            
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41191715/article/details/82940266
今日推荐