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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 伪元素的作用_获取iframe中的元素

    伪元素的作用_获取iframe中的元素目标网站红薯中文网获取网页源代码也获取不了这些动态渲染的数据所以用简单的,但是有点麻烦的方法使用selenium执行js,或者直接在浏览器里面执行jsfunctionkkk(){varmyIdElement=document.getElementsByClassName(“context_kw11″);varbeforeStyle=window.getComputedStyle(myIdElement[0],”::before”);returnbeforeStyle.con

    2022年10月12日
    0
  • hdu 3336 Count the string 用心写的题解

    hdu 3336 Count the string 用心写的题解ProblemDescriptionItiswellknownthatAekdyCoinisgoodatstringproblemsaswellasnumbertheoryproblems.Whengivenastrings,wecanwritedownallthenon-emptyprefixesofthisstring.Fo

    2022年7月23日
    6
  • VM15pro安装MacOS10.15.1系统(超详细,可用)[通俗易懂]

    VM15pro安装MacOS10.15.1系统(超详细,可用)[通俗易懂]前段时间去开发了款app需要用到打包,Android打包就不说了那个不涉及到环境,但是ipa打包就烦了,还要在mac的环境中去打包,但是作为一个底层程序员为了一个打包去买一个macpro就有点亏了,………

    2022年10月1日
    0
  • Vue双向绑定原理

    Vue双向绑定原理vue的双向绑定原理:vue数据的双向绑定是通过数据劫持结合发布者-订阅者模式的方式来实现的。其核心就是通过obj.defineProperty()方法来实现数据的劫持,在数据变化时发布消息给订阅者,触发相应的监听回调。也就是说数据和视图同步,数据发生变化,视图跟着变化,视图变化,数据也随之发生改变;简单介绍一下Object.defineProperty()方法:1、Object.defineProperty(obj,prop,descriptor),这个语法内有三个参数,分别为obj(要定

    2022年10月18日
    0
  • mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因

    mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因

    2021年10月21日
    127
  • ubuntu设置nginx开机启动_ubuntussh

    ubuntu设置nginx开机启动_ubuntussh转载自:https://www.cnblogs.com/xiaoL/p/6964217.htmlnginx-sreload:修改配置后重新加载生效nginx-sreopen:重新打开日志文件nginx-t-c/path/to/nginx.conf测试nginx配置文件是否正确关闭nginx:nginx-sstop:快速停止nginx…

    2022年8月13日
    55

发表回复

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

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