我正在创建一个移动应用程序,以便在手机上运行并尝试以最有效的方式从中读取数据.应用程序将数据发送到我的服务器应用程序(以字节的形式,不一定是字符).
我不知道数据的长度; 结尾将标有3字节标记(即0x11,0x22,0x33),然后将发送一组新数据.很可能会在每组"数据集"中发送大量数据.我想知道,读取这些数据最有效的方法是什么.我应该用InputStreamReader吗?BufferedReader?显然,我需要检查每个字符以查看它是否是标记的一部分,如果是,则将标记之前的所有数据发送到另一个方法进行处理.
从我所知道的,如果我的结束标记是一个(显然,情况并非如此),BufferedReader.readLine()那就是我想要的.我是否需要编写自己的方法来逐字节读取并查找我的标记?(我也不知道这是否是最有效的方式?)\nBufferedReader
我正在尝试使用缓冲读取器读取文件,但它有时会跳过一行中的第一个字符.这是文件,我正在阅读:http: //files.moonmana.com/forums/Rectangle.h
这是结果,我得到:
LINE: #ifndef RECTANGLE_H
LINE: include "Shape.h"
LINE: lass Rectangle : public Shape {
LINE: rivate:
LINE: double _width;
LINE: std::vector<b2Vec2*>* _vertices;
LINE: ublic:
LINE: Rectangle(std::vector<b2Vec2*>* vertices) { _vertices = vertices;};
LINE: void createVertices();
LINE: bool isMimePoint(b2Vec2);
LINE: double getWidth(){return _width;};
LINE: void setWidth(double width);
LINE: void setHeights(double heights);
LINE: ShapeType getType();
LINE: void moveOn( b2Vec2 ,b2Vec2*);
LINE: virtual b2Vec2* getCenter();
LINE: ;
LINE: endif
Run Code Online (Sandbox Code Playgroud)
这是我的源代码:
String path = file.getPath();
BufferedReader _br;
try {
_br = …Run Code Online (Sandbox Code Playgroud) 我正在使用txt文件进行我的关卡设计.我使用下面的内容并转换为字符串缓冲区,然后遍历这些行来生成我的游戏对象.
问题是它从上到下读取,因此我必须将我的水平设计为颠倒,以使它们正确.
如何更改流以相反的方式读取?或者以相反的方式将行写入String Builder?
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
Log.w("LOG", e.getMessage());
} finally {
try {
is.close();
} catch (IOException e) {
Log.w("LOG", e.getMessage());
}
}
return sb.toString();
Run Code Online (Sandbox Code Playgroud) 我正在使用BufferedReader,虽然我调用了close()方法,但eclipse仍然给了我一个警告.如果我在while之前放置close()调用,Eclipse不会给我一个警告,但是在那时代码不起作用.我的代码中是否有错误,或者还有什么问题?码:
Hashtable<String, Hashtable<String, Integer>> buildingStats = new Hashtable<String, Hashtable<String, Integer>>();
try
{
BufferedReader br = new BufferedReader(new FileReader(new File("Assets/Setup/Buildings.txt"))); // Sets the buildings values to the values in Buildings.tx
String line;
int lineNum = 0;
while((line = br.readLine()) != null)
{
++lineNum;
String[] values = line.split(",");
if (values.length != 3)
throw new Exception("Invalid data in Assets/Setup/Buildings.txt at line " + lineNum);
if (buildingStats.containsKey(values[0]))
{
buildingStats.get(values[0]).put(values[1], Integer.parseInt(values[2]));
}
else
{
buildingStats.put(values[0], new Hashtable<String, Integer>());
buildingStats.get(values[0]).put(values[1], Integer.parseInt(values[2]));
}
}
br.close();
}
catch …Run Code Online (Sandbox Code Playgroud) 我试图用"服务器"和客户端创建一个简单的聊天程序,现在我的问题是程序在从服务器读取消息到客户端时阻塞,反之亦然.此示例解决了从客户端到服务器的消息问题.
我在服务器端的示例:
private Reader input;
private Writer output;
try {
server = new ServerSocket(this.port);
while (true) {
Socket connection = server.accept();
serverDisplay("We have a connection");
input = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
output = new BufferedWriter(new OutputStreamWriter(
connection.getOutputStream()));
int c;
StringBuffer sb = new StringBuffer();
// This is where it blocks, the input stream should return -1 at the end of the
// stream and break the loop, but it doesnt
while ((c = input.read()) != -1) {
sb.append((char) c);
} …Run Code Online (Sandbox Code Playgroud) 代码:
我有TriangleSummer类有变量的类
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Run Code Online (Sandbox Code Playgroud)
我的主要功能是
public static void main(String args[]) throws IOException {
int noOfTestCases = Integer.parseInt(reader.readLine());
for(int i=0;i<noOfTestCases;i++){
int noOfEntries = Integer.parseInt(reader.readLine());
System.out.println(computeMaxSum(noOfEntries));
}
}
Run Code Online (Sandbox Code Playgroud)
我computeMaxSum()方法的相关部分是
public static int computeMaxSum(int noOfEntries) throws IOException {
int[][] triangle = new int[noOfEntries][noOfEntries];
for(int j=0;j<noOfEntries;j++){
int k=0;
for(String str : reader.readLine().split(" ")){
triangle[j][k] = Integer.parseInt(str);
k++;
}
}
//further logic nothing to do with I/O
}
Run Code Online (Sandbox Code Playgroud)
最后堆栈跟踪是
Exception in thread "main" java.lang.NumberFormatException: For input …Run Code Online (Sandbox Code Playgroud) 我正在尝试读取充满文本的csv文件; 然而,如果某个地方的中间有一个空白行,整个事情就会中断,我得到一个:
java.lang.RuntimeException: java.lang.StringIndexOutOfBoundsException
Run Code Online (Sandbox Code Playgroud)
只要它不是文件的末尾,我将如何删除/忽略空行?
file = new FileReader(fileName);
@SuppressWarnings("resource")
BufferedReader reader = new BufferedReader(file);
while ((line = reader.readLine()) != null) {
//do lots of stuff to sort the data into lists etc
}
} catch (Exception e) {
System.out.println("INPUT DATA WAS NOT FOUND, PLEASE PLACE FILE HERE: " + System.getProperty("user.dir"));
throw new RuntimeException(e);
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
// Ignore issues during closing
}
}
}
Run Code Online (Sandbox Code Playgroud) 我实现了一个从命令行获取输入文件的代码.然后,对此输入进行排序.然后将输出写入当前目录.我的代码工作,但我想知道该类型的文件.我的input.txt类型是dos\Windows,如图所示.我生成的output.txt类型是UNIX.它们的尺寸也不同.为什么它们以不同的格式存储?我使用bufferedReader,fileWriter来实现这段代码.
code.java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.FileWriter;
public class code{
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader(args[0])))
{
int lines = 0;
while (br.readLine() != null) lines++; // to get text's number of lines
String sCurrentLine;
BufferedReader br2 = new BufferedReader(new FileReader(args[0])); //to read and sort the text
String[] array; //create a new array
array = new String[lines];
int i=0;
while ((sCurrentLine = br2.readLine()) != null) {//fill array with …Run Code Online (Sandbox Code Playgroud) String str = "";
try {
BufferedReader br = new BufferedReader(new FileReader(file.getAbsolutePath()));
while (br.readLine() != null) {
str += br.readLine();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String replace = str.replace("HTTP Request: ", "")
.replace("Resource URL: ","")
.replace("Attribute\t\tDescription", "| Attribute | Type | Description |<P>|----|----|<P>")
.replace("Data Type | Max Length | Requirement |", "")
.replace("N/A", "Object")
.replace("String", "| String")
.replace("255 |", "")
.replace("Required", "**Required**")
.replace("Optional", "**Optional**")
.replace("Request Example <P>", "")
.replace("Response Example <P>", "Nothing"); …Run Code Online (Sandbox Code Playgroud) 我想把a的内容读InputStream成String:
private String readToString(InputStream stream) {
return new BufferedReader(new InputStreamReader(stream))
.lines().collect(Collectors.joining("\n"));
}
Run Code Online (Sandbox Code Playgroud)
流来自java.lang.Process.
问题:我是否必须明确关闭任何一个InputStream,InputStreamReader或者BufferedReader在这种情况下?
旁注:链接的问题不是重复的,因为我的问题是如何正确关闭流,而不是如何将流读取到字符串!
bufferedreader ×10
java ×10
android ×1
blank-line ×1
eclipse ×1
file-io ×1
filewriter ×1
inputstream ×1
io ×1
java-8 ×1
sockets ×1
warnings ×1