15、关于String,File,InputStream之间的相互转换

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)发生异常!!!");
			}

		}

	}






  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值