String/InputStream/File之间的相互转换

String/InputStream/File之间的相互转换InputStrem与String之间转换String转InputStream/***将str转换为inputStream*@paramstr*@return*/publicstaticInputStreamstr2InputStream(Stringstr){ ByteArrayInputStreamis=newByteArrayInputStr…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

InputStream与String之间转换

String转InputStream

/**
 * 将str转换为inputStream
 * @param str
 * @return
 */
public static InputStream str2InputStream(String str) {
	ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes());
	return is;
}

InputStream转String

/**
 * 将inputStream转换为str
 * @param is
 * @return
 * @throws IOException
 */
public static String inputStream2Str(InputStream is) throws IOException {
	StringBuffer sb;
	BufferedReader br = null;
	try {
		br = new BufferedReader(new InputStreamReader(is));

		sb = new StringBuffer();

		String data;
		while ((data = br.readLine()) != null) {
			sb.append(data);
		}
	} finally {
		br.close();
	}

	return sb.toString();
}

InputStream与File之间转换

File转InputStream

/**
 * 将file转换为inputStream
 * @param file
 * @return
 * @throws FileNotFoundException
 */
public static InputStream file2InputStream(File file) throws FileNotFoundException {
	return new FileInputStream(file);
}

InputStream转File

/**
 * 将inputStream转化为file
 * @param is
 * @param file 要输出的文件目录
 */
public static void inputStream2File(InputStream is, File file) throws IOException {
	OutputStream os = null;
	try {
		os = new FileOutputStream(file);
		int len = 0;
		byte[] buffer = new byte[8192];

		while ((len = is.read(buffer)) != -1) {
			os.write(buffer, 0, len);
		}
	} finally {
		os.close();
		is.close();
	}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191067.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JS 数字取整数方式[通俗易懂]

    JS 数字取整数方式[通俗易懂]1、parseInt(number)varnum1=parseInt(“2015nov”),//2015num2=parseInt(“”),//NaNnum3=parseInt(“0xA”),//10(十六进制)num4=parseInt(20.15),//20num5=parseInt(-20.15),//-20num6=parseInt(“070”);//56(八进制数)2、~~numbe

    2022年6月16日
    35
  • BGP与Anycast

    BGP与AnycastBGP属于一项技术,一般应用于IDC多线机房,也就是把接入到机房的多条线路融合为一体。实现多线单IP。因为最早的多线机房都是双线双IP,现在很多双线机房开始利用该技术,让用户使用IP的时候操作更简单些Anycast技术具有以下优势:一、不同客户端将访问不同目的主机,此过程对客户端透明,从而实现了目的主机的负载均衡;二、当任意目的主机接入的网络出现故障,导致该目的主机不可达…

    2022年5月24日
    71
  • BCG库的一个bug[通俗易懂]

    BCG库的一个bug[通俗易懂]作者:朱金灿来源:http://blog.csdn.net/clever101            同事在使用BCG库图表组件(BCG库的版本为BCGCBPRO1510)时遇到一个问题,就是图表标题总是出现乱码,具体如下图:我查看了一下BCG库的源码,发现BCG库的绘制接口是采用D2D的接口。D2D是微软推出的一个二维绘制的com组件,只支持unicode字符,非unicode字符需要转换

    2022年10月8日
    1
  • phantomjs环境搭建已经运行

    phantomjs环境搭建已经运行

    2021年12月6日
    42
  • android studio sdk安装失败「建议收藏」

    android studio sdk安装失败「建议收藏」androidstudiosdk安装失败原因电脑只有一个c盘是无法下载sdk的,软件设置不允许在c盘安装sdk

    2022年7月21日
    27
  • “D:/WWW/favicon.ico” failed (2: The system cannot find the file specified)

    “D:/WWW/favicon.ico” failed (2: The system cannot find the file specified)”D:/WWW/favicon.ico” failed (2: The system cannot find the file specified)

    2022年4月24日
    75

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号