1. 程式人生 > >inputStream、File、Byte、String等等之間的相互轉換

inputStream、File、Byte、String等等之間的相互轉換

array odi finall ioutils post 字符串 stack sts col

一:inputStream轉換

1、inputStream轉為byte

//方法一  org.apache.commons.io.IOUtils包下的實現(建議)
IOUtils.toByteArray(inputStream);
//方法二  用java代碼實現(其實就是對上面方法一的解析)
public static byte[] toByteArray(InputStream input) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new
byte[1024]; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); } return output.toByteArray(); }

2、inputStream轉為file

    public static void inputstreamtofile(InputStream ins, File file) throws IOException {
        OutputStream os = new FileOutputStream(file);
        
int bytesRead; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); }

3、inputStream轉為String

//方法一  使用org.apache.commons.io.IOUtils包下的方法
IOUtils.toString(inputStream);

//方法二 /** * 將InputStream轉換成某種字符編碼的String * * @param in * @param encoding * @return * @throws Exception */ public static String inputStreamToString(InputStream in, String encoding) { String string = null; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[BUFFER_SIZE]; int count = -1; try { while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) outStream.write(data, 0, count); } catch (IOException e) { logger.error("io異常", e.fillInStackTrace()); } try { string = new String(outStream.toByteArray(), encoding); } catch (UnsupportedEncodingException e) { logger.error("字符串編碼異常", e.fillInStackTrace()); } return string; }

二:byte[]轉換

1、byte[]轉為inputStream

InputStream sbs = new ByteArrayInputStream(byte[] buf); 

2、byte[]轉為File

  public static void byte2File(byte[] buf, String filePath, String fileName)
    {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try
        {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory())
            {
                dir.mkdirs();
            }
            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(buf);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (bos != null)
            {
                try
                {
                    bos.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (fos != null)
            {
                try
                {
                    fos.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

3、byte[]轉String

//先將byte[]轉為inputStream,然後在轉為String
InputStream is = new ByteArrayInputStream(byte[] byt);
//然後在根據上文提到的將inputStream轉為String的方法去轉換

三、File的轉換

1、file轉inputStream

FileInputStream fileInputStream = new FileInputStream(file);

inputStream、File、Byte、String等等之間的相互轉換