我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
Maven配置
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
Maven 配置
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";
String result = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));
String result = new BufferedReader(new InputStreamReader(inputStream)).lines().parallel().collect(Collectors.joining("\n"));
int bufferSize = 1024;
char[] buffer = new char[bufferSize];
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(stream, StandardCharsets.UTF_8);
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
out.append(buffer, 0, numRead);
}
return out.toString();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, "UTF-8");
return writer.toString();
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = inputStream.read(buffer)) != -1; ) {
result.write(buffer, 0, length);
}
// StandardCharsets.UTF_8.name() > JDK 7
return result.toString("UTF-8");
String newLine = System.getProperty("line.separator");
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
for (String line; (line = reader.readLine()) != null; ) {
if (result.length() > 0) {
result.append(newLine);
}
result.append(line);
}
return result.toString();
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
for (int result = bis.read(); result != -1; result = bis.read()) {
buf.write((byte) result);
}
// StandardCharsets.UTF_8.name() > JDK 7
return buf.toString("UTF-8");
StringBuilder sb = new StringBuilder();
for (int ch; (ch = inputStream.read()) != -1; ) {
sb.append((char) ch);
}
return sb.toString();
1、解决方案4、5、9此解决方案将不同的换行符(如\r\n转换为\n),(\r\n是Windows系统的换行符,而在Unix、Liunx等系统中的换行符为 \n)。
2、解决方案11不能正确地处理Unicode文本。
package eleven.莱迪娜的风声;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
/**
* 时间:2022年9月24日11:18:17
*
* @author 莱迪娜的风声
*/
public class InputStreamToChar {
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream(new File("Test.txt"));
one(inputStream);
two(inputStream);
three(inputStream);
four(inputStream);
five(inputStream);
six(inputStream);
seven(inputStream);
eight(inputStream);
nine(inputStream);
ten(inputStream);
eleven(inputStream);
}
public static void one(InputStream inputStream) throws IOException {
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
System.out.println(result);
}
public static void two(InputStream inputStream) throws IOException {
String result = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
System.out.println(result);
}
public static void three(InputStream inputStream) throws IOException {
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";
System.out.println(result);
}
public static void four(InputStream inputStream) throws IOException {
String result = new BufferedReader(new InputStreamReader(inputStream)).lines()
.collect(Collectors.joining("\n"));
System.out.println(result);
}
public static void five(InputStream inputStream) throws IOException {
String result = new BufferedReader(new InputStreamReader(inputStream)).lines().parallel()
.collect(Collectors.joining("\n"));
System.out.println(result);
}
public static void six(InputStream inputStream) throws IOException {
int bufferSize = 1024;
char[] buffer = new char[bufferSize];
StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0;) {
out.append(buffer, 0, numRead);
}
String result = out.toString();
System.out.println(result);
}
public static void seven(InputStream inputStream) throws IOException {
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, "UTF-8");
String result = writer.toString();
System.out.println(result);
}
public static void eight(InputStream inputStream) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = inputStream.read(buffer)) != -1;) {
result.write(buffer, 0, length);
}
// StandardCharsets.UTF_8.name() > JDK 7
System.out.println(result.toString("UTF-8"));
}
public static void nine(InputStream inputStream) throws IOException {
String newLine = System.getProperty("line.separator");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
for (String line; (line = reader.readLine()) != null;) {
if (result.length() > 0) {
result.append(newLine);
}
result.append(line);
}
System.out.println(result.toString());
}
public static void ten(InputStream inputStream) throws IOException {
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
for (int result = bis.read(); result != -1; result = bis.read()) {
buf.write((byte) result);
}
// StandardCharsets.UTF_8.name() > JDK 7
System.out.println(buf.toString("UTF-8"));
}
public static void eleven(InputStream inputStream) throws IOException {
StringBuilder sb = new StringBuilder();
for (int ch; (ch = inputStream.read()) != -1;) {
sb.append((char) ch);
}
System.out.println(sb.toString());
}
}