1、介绍了关于String,File,InputStream之间的相互转换
1.1 String2InputStream
/** * String2InputStream(String str)的工具方法 * * @param str * 需要转换的字符串str * @return 返回的是字符串str转换为inputstream的结果 */ public static InputStream String2InputStream(String str) { ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); return stream; }
1.2 inputStream2String(InputStream is) ; String inputStream2String(InputStream is, String charset)
/** * inputStream2String(InputStream is) * * @param is * 输入流InputStream is * @return 输入流InputStream is转换为的String */ public static String inputStream2String(InputStream is) { StringBuffer buffer = null; BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(is)); buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } } catch (Exception e) { throw new RuntimeException( "在调用Utils.inputStream2String(InputStream is)发生异常!!!"); } return buffer.toString(); } /** * inputStream2String(InputStream is, String charset) * * @param is * 输入流InputStream is * @param charset * 字符集String charset * @return 输入流InputStream is,字符集String charset转换为的String */ public static String inputStream2String(InputStream is, String charset) { ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); byte data[] = new byte[1024]; int len = -1; while ((len = is.read(data)) != -1) { baos.write(data, 0, len); } String a = baos.toString(charset); return baos.toString(charset); } catch (IOException e) { throw new RuntimeException( "在调用Utils.inputStream2String(InputStream is,String charset)发生异常!!!"); } finally { if (null != baos) { try { baos.close(); } catch (IOException e) { throw new RuntimeException( "在调用Utils.inputStream2String(InputStream is, String charset)发生异常!!!"); } baos = null; } } }
1.3 file2InputStream
/** * file2InputStream(File file) * * @param file * 文件 File file * @return 文件File转换为的InputStream */ public static InputStream file2InputStream(File file) { InputStream in = null; try { in = new FileInputStream(file); return in; } catch (FileNotFoundException e) { throw new RuntimeException( "在调用Utils.file2InputStream(File file)发生异常!!!"); } } /** * file2InputStream(String filenPath) * * @param filenPath * 文件 File的具体路径 * @return 文件File转换为的InputStream */ public static InputStream file2InputStream(String filenPath) { InputStream in = null; File file = null; try { file = new File(filenPath); in = new FileInputStream(file); return in; } catch (FileNotFoundException e) { throw new RuntimeException( "在调用Utils.file2InputStream(String filenPath)发生异常!!!"); } }
1.4 inputstreamtofile
/** * inputstreamtofile(InputStream ins, File file) * * @param ins * 输入流InputStream ins * @param file * 输入流InputStream ins转换的文件 */ public static void inputstreamtofile(InputStream ins, File file) { OutputStream os = null; try { os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[1024]; while ((bytesRead = ins.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } } catch (Exception e) { throw new RuntimeException( "在调用Utils.inputstreamtofile(InputStream ins, File file)发生异常!!!"); } finally { try { if (os != null) os.close(); if (ins != null) { ins.close(); } } catch (IOException e) { throw new RuntimeException( "在调用Utils.inputstreamtofile(InputStream ins, File file)发生异常!!!"); } } } public static void inputstreamtofile(InputStream ins, String filePath) { OutputStream os = null; File file = null; try { file = new File(filePath); os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[1024]; while ((bytesRead = ins.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } } catch (Exception e) { throw new RuntimeException( "在调用Utils.inputstreamtofile(InputStream ins, File file)发生异常!!!"); } finally { try { if (os != null) os.close(); if (ins != null) { ins.close(); } } catch (IOException e) { throw new RuntimeException( "在调用Utils.inputstreamtofile(InputStream ins, File file)发生异常!!!"); } } }
时间: 2024-11-01 02:35:43