Java源代码详解之FileOutputStream

版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/84180180

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();
        }
    }

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

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/84180180