我正在尝试读取资源(asdf.txt),但如果文件大于5000字节,(例如)将4700个空字符插入到内容变量的末尾.有没有办法删除它们?(或设置正确的缓冲区大小?)
这是代码:
String content = "";
try {
InputStream in = this.getClass().getResourceAsStream("asdf.txt");
byte[] buffer = new byte[5000];
while (in.read(buffer) != -1) {
content += new String(buffer);
}
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个简单的服务器 - 客户端程序,但我遇到了一个问题:我可以将数据从客户端发送到服务器,但我无法从服务器发送数据(我无法在客户端中收到它): (
那么如何从服务器发送数据,并在客户端中重现它?
服务器:
//this is in a thread
try {
server = new ServerSocket(1365);
} catch (IOException e) {
e.printStackTrace();
}
while (!exit) {
try {
clientSocket = server.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
while ((line = is.readLine()) != null) {
System.out.println("Message from client: " + line);
//if (line.equals("exit")) {
// exit = true;
//}
if (line.equals("say something")) {
os.write("something".getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch …Run Code Online (Sandbox Code Playgroud)