How to convert a Reader to InputStream and a Writer to OutputStream?

有没有简单的方法来避免处理文本编码问题? You can't really avoid dealing with the text encoding issues, but there are existing solutions: Reader to InputStream : ReaderInputStream Writer to OutputStream : WriterOutputStream You just need to pick the encoding of your choice. 如果你从一个字符串开始,你也可以执行以下操作: new ByteArrayInputStream(inputString.getBytes("UTF-8")) Well, a Re

如何将Reader转换为InputStream并将Writer转换为OutputStream?

有没有简单的方法来避免处理文本编码问题? 你不能真正避免处理文本编码问题,但现在有解决方案: Reader到InputStream : ReaderInputStream Writer到OutputStream : WriterOutputStream 你只需要选择你选择的编码。 如果你从一个字符串开始,你也可以执行以下操作: new ByteArrayInputStream(inputString.getBytes("UTF-8")) 那么,Reader会处理字符,InputStream会处理字节。 编码指定了如何将字符表示为字节,因

How do I turn a String into a InputStreamReader in java?

如何将String值转换为InputStreamReader ? ByteArrayInputStream也有诀窍: InputStream is = new ByteArrayInputStream( myString.getBytes( charset ) ); 我也发现了apache的commons IOUtils类,所以: InputStreamReader isr = new InputStreamReader(IOUtils.toInputStream(myString)); Does it have to be specifically an InputStreamReader? How about using StringReader? Otherwise, you could use StringBufferInp

如何在Java中将字符串转换为InputStreamReader?

如何将String值转换为InputStreamReader ? ByteArrayInputStream也有诀窍: InputStream is = new ByteArrayInputStream( myString.getBytes( charset ) ); 我也发现了apache的commons IOUtils类,所以: InputStreamReader isr = new InputStreamReader(IOUtils.toInputStream(myString)); 它是否必须专门用于InputStreamReader? 如何使用StringReader? 否则,你可以使用StringBufferInputStream,但由于字符转换问题而

8 byte[] to String

Let's suppose I have just used a BufferedInputStream to read the bytes of a UTF-8 encoded text file into a byte array. I know that I can use the following routine to convert the bytes to a string, but is there a more efficient/smarter way of doing this than just iterating through the bytes and converting each one? public String openFileToString(byte[] _bytes) { String file_string = "";

8字节[]为字符串

假设我刚刚使用BufferedInputStream将UTF-8编码文本文件的字节读入字节数组。 我知道我可以使用下面的例程将字节转换为一个字符串,但是这样做是否有更高效/更智能的方式,而不仅仅是迭代字节并转换每个字节? public String openFileToString(byte[] _bytes) { String file_string = ""; for(int i = 0; i < _bytes.length; i++) { file_string += (char)_bytes[i]; } return file_string;

How to get my data from website so I can parse it?

This question already has an answer here: Read/convert an InputStream to a String 56 answers You can use this: BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String parseThis = reader.readLine(); That'll give you a string of the output. If it's in JSON, add: JSONObject jsonObj = new JSONObject(parseThis); JSONArray arr

如何从网站获取我的数据,以便我可以解析它?

这个问题在这里已经有了答案: 将InputStream读取/转换为字符串56个答案 你可以使用这个: BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String parseThis = reader.readLine(); 这会给你一串输出。 如果它在JSON中,请添加: JSONObject jsonObj = new JSONObject(parseThis); JSONArray arrayOfStuff = jsonObj.getJSONArray(array_tag);

Best way to build a String out of characters

This question already has an answer here: Read/convert an InputStream to a String 56 answers 你可以做类似的事情public String readTextFile(File file) { StringBuilder sb = new StringBuilder(); try(BufferedReader br = new BufferedReader(new FileReader(file))){ String line; while ((line = br.readLine()) != null){ sb.append(line); } }

从字符中构建字符串的最佳方法

这个问题在这里已经有了答案: 将InputStream读取/转换为字符串56个答案 你可以做类似的事情public String readTextFile(File file) { StringBuilder sb = new StringBuilder(); try(BufferedReader br = new BufferedReader(new FileReader(file))){ String line; while ((line = br.readLine()) != null){ sb.append(line); } } return sb.toString();

length file from /sys/fs/cgroup/ in Java?

This question already has an answer here: Read/convert an InputStream to a String 56 answers 即使操作系统将文件长度报告为0 ,以下方法(使用Java NIO)仍然有效: String file = "/sys/fs/cgroup/cpu,cpuacct/cpuacct.usage"; String content = new String(Files.readAllBytes(Paths.get(file))); System.out.println(content); // 972897369764987 I recommend the IOUtils class in the Apache Commons IO library

来自/ sys / fs / cgroup /在Java中的长度文件?

这个问题在这里已经有了答案: 将InputStream读取/转换为字符串56个答案 即使操作系统将文件长度报告为0 ,以下方法(使用Java NIO)仍然有效: String file = "/sys/fs/cgroup/cpu,cpuacct/cpuacct.usage"; String content = new String(Files.readAllBytes(Paths.get(file))); System.out.println(content); // 972897369764987 我推荐Apache Commons IO库中的IOUtils类。 由于这些文件的内容是文本,因此可以使用IOUti

how to read body of a post packet in java

This question already has an answer here: Read/convert an InputStream to a String 56 answers if you use Java 8 you can do List<String> content = BR.lines().collect(Collectors.toList()); Otherwise you can do List<String> content = new ArrayList<>(); String line; while((line = BR.readLine()) != null) { content.add(line); } Then parse/format as you wish to get the respon

如何在java中阅读一个post包的正文

这个问题在这里已经有了答案: 将InputStream读取/转换为字符串56个答案 如果你使用Java 8,你可以做 List<String> content = BR.lines().collect(Collectors.toList()); 否则,你可以做 List<String> content = new ArrayList<>(); String line; while((line = BR.readLine()) != null) { content.add(line); } 然后解析/格式化,因为您希望整体获得响应。 BR.readLine()只返回第一行; 您需要

Decoding InputStream for Java HTTP GET

This question already has an answer here: Read/convert an InputStream to a String 56 answers 您可以使用org.json API从InputStream创建JSONObject ,如下所示: JSONObject obj = new JSONObject(new JSONTokener(connection.getInputStream())

解码Java HTTP GET的InputStream

这个问题在这里已经有了答案: 将InputStream读取/转换为字符串56个答案 您可以使用org.json API从InputStream创建JSONObject ,如下所示: JSONObject obj = new JSONObject(new JSONTokener(connection.getInputStream())

How to return value of FileInputStream in Android

This question already has an answer here: Read/convert an InputStream to a String 56 answers Consider the difference between your code: while (inputStream.read(reader) != -1) {} UserPassFile = new String(reader); And this alternative: while (inputStream.read(reader) != -1) { UserPassFile = new String(reader); } In your case, UserPassFile is ass

如何在Android中返回FileInputStream的值

这个问题在这里已经有了答案: 将InputStream读取/转换为字符串56个答案 考虑你的代码之间的区别: while (inputStream.read(reader) != -1) {} UserPassFile = new String(reader); 而这个选择: while (inputStream.read(reader) != -1) { UserPassFile = new String(reader); } 在你的情况下,每次read()调用后,UserPassFile都会从reader中分配,包括最后一次失败。

How to Create a string from a BufferedInputStream using bytes?

This question already has an answer here: Read/convert an InputStream to a String 56 answers The buffer is too large. Try this: InputStream content = new FileInputStream(file); int numRead; final int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; ByteArrayOutputStream outString = new ByteArrayOutputStream(); try{ while ((numRead = content.read(buff

如何使用字节从BufferedInputStream创建字符串?

这个问题在这里已经有了答案: 将InputStream读取/转换为字符串56个答案 缓冲区太大。 尝试这个: InputStream content = new FileInputStream(file); int numRead; final int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; ByteArrayOutputStream outString = new ByteArrayOutputStream(); try{ while ((numRead = content.read(buffer)) != -1) { outString.