Java源代码详解之FileOutputStream

1.类定义

A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.

一个把数据写到一个File或者到一个FileDescriptor的文件输出流。无论文件是否可用,或者是否可被创建,取决于底层的平台。尤其是在某些平台上,一次只允许一个 FileOutputStream(或者其它的文件写对象) 打开文件进行写入。 在这样的情况下,如果涉及的文件已经打开,那么这个类的构造器将会执行失败。

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

FileOutputStream 是用于写诸如图片数据等原始字节流。如果要写字符流,请考虑使用 FileWriter

2. 实战代码

将图片存储在如下的文件夹中。
在这里插入图片描述

  • getConnection(String url)
 //get connection with specific url
    public InputStream getConnection(String url) {
        //01.CloseableHttpClient is a abstract class
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //use get
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; rv:63.0) Gecko/20100101 Firefox/63.0");
        InputStream inputStream =null;
        try {
            //get the request's response
            CloseableHttpResponse response = httpClient.execute(httpGet);

            HttpEntity entity = response.getEntity();

            //get the InputStream of entity
            inputStream = entity.getContent();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  inputStream;
    }
  • getImageName()
 //this part,use a customed method,rather than a specific path to store new file
    public String getImageName(){
        String imageName ;
        Date date = new Date();
        //DateFormat dateFormat = DateFormat.getDateInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
        //System.out.println(simpleDateFormat.format(date));
        imageName = dirPath+simpleDateFormat.format(date) + ".jpg";
        return imageName;
    }
  • writeImageInDisk(InputStream inputStream)
public void writeImageInDisk(InputStream inputStream){
        String fileAddress = getImageName();
        File newFile = new File(fileAddress);

        //使用字节存储图片,但是这里的大小只有1024B = 1kB,是否会导致数组溢出?
        byte [] image = new byte[1024] ;

        int length ;
        FileOutputStream fileOutputStream = null;
        try {
            if(inputStream!=null){
                fileOutputStream = new FileOutputStream(newFile);
                while((length = inputStream.read(image))!=-1){
                    fileOutputStream.write(image,0,length);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

执行结果如下:
在这里插入图片描述

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐