Skip to content

Instantly share code, notes, and snippets.

@zbo2018
Last active March 26, 2019 08:22
Show Gist options
  • Save zbo2018/9b731c249023832744712df9178be916 to your computer and use it in GitHub Desktop.
Save zbo2018/9b731c249023832744712df9178be916 to your computer and use it in GitHub Desktop.
字符串转输入流
new ByteArrayInputStream("abcd".getBytes());
文件获取输入流
file.getInputStream();
代码获取web部署后resources目录资源文件内容
InputStream in = this.getClass().getResourceAsStream("/customer_faq");
String --> InputStream
ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
File --> InputStream
InputStream in = new FileInputStream(file);
InputStream --> String
String inputStream2String(InputStream is){
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null){
buffer.append(line);
}
return buffer.toString();
}
InputStream --> File
public void inputstreamtofile(InputStream ins,File file){
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment