有没有简单的方法来避免处理文本编码问题? 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 : ReaderInputStream Writer到OutputStream : WriterOutputStream 你只需要选择你选择的编码。 如果你从一个字符串开始,你也可以执行以下操作: new ByteArrayInputStream(inputString.getBytes("UTF-8")) 那么,Reader会处理字符,InputStream会处理字节。 编码指定了如何将字符表示为字节,因
如何将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
如何将String值转换为InputStreamReader ? ByteArrayInputStream也有诀窍: InputStream is = new ByteArrayInputStream( myString.getBytes( charset ) ); 我也发现了apache的commons IOUtils类,所以: InputStreamReader isr = new InputStreamReader(IOUtils.toInputStream(myString)); 它是否必须专门用于InputStreamReader? 如何使用StringReader? 否则,你可以使用StringBufferInputStream,但由于字符转换问题而
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 = "";
假设我刚刚使用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;
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);
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();
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
这个问题在这里已经有了答案: 将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
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
这个问题在这里已经有了答案: 将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()只返回第一行; 您需要
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())
这个问题在这里已经有了答案: 将InputStream读取/转换为字符串56个答案 您可以使用org.json API从InputStream创建JSONObject ,如下所示: JSONObject obj = new JSONObject(new JSONTokener(connection.getInputStream())
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
这个问题在这里已经有了答案: 将InputStream读取/转换为字符串56个答案 考虑你的代码之间的区别: while (inputStream.read(reader) != -1) {} UserPassFile = new String(reader); 而这个选择: while (inputStream.read(reader) != -1) { UserPassFile = new String(reader); } 在你的情况下,每次read()调用后,UserPassFile都会从reader中分配,包括最后一次失败。
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
这个问题在这里已经有了答案: 将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.