15、关于String,File,InputStream之间的相互转换
来源:互联网 发布:vb中cls含义 编辑:程序博客网 时间:2024/05/13 00:20
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)发生异常!!!");}}}
1 0
- 15、关于String,File,InputStream之间的相互转换
- android中String与InputStream之间的相互转换方式
- java file inputstream string 相互转换
- Java之InputStream与String与File之间的转换
- 关于OutPutStream、InPutStream、String之间转换的问题:
- InputStream、OutputStream、String的相互转换
- InputStream、OutputStream、String的相互转换
- InputStream、OutputStream、String的相互转换
- InputStream、OutputStream、String的相互转换
- InputStream、OutputStream、String的相互转换
- InputStream和String的相互转换
- String byte[] stream File之间的相互转换
- InputStream,String,File相互转化
- InputStream,String,File相互转化
- InputStream,String,File相互转化
- InputStream,String,File相互转化
- InputStream,String,File相互转化
- InputStream,String,File相互转化
- 【原创】一笔画问题(欧拉路)
- 兼容iOS 10 资料整理笔记
- phonegap jQuerymobile 跨域访问
- WebApplication 与WebSite
- 勾股定理一日一证连载126
- 15、关于String,File,InputStream之间的相互转换
- 和通讯录产品分析报告
- ios developer tiny share-20160922
- Nodejs连接mysql的增、删、改、查操作
- android oom 全解析
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- 使用向量的方法来计算点到直线的距离
- 自绘控件打造界面超炫功能超强的圆形进度条
- Eclipse关于cordova+AngularJS环境搭建