Java 如何包含BufferedReader忽略的换行符
我需要使用缓冲区读取器通过套接字进行通信,我只是想知道如何保存新行。我希望最终在密码学中使用它,所以我不能假设消息的末尾有新行 我是一个新的插座一般,所以任何帮助将不胜感激 客户端正在发送的文本Java 如何包含BufferedReader忽略的换行符,java,sockets,cryptography,bufferedreader,Java,Sockets,Cryptography,Bufferedreader,我需要使用缓冲区读取器通过套接字进行通信,我只是想知道如何保存新行。我希望最终在密码学中使用它,所以我不能假设消息的末尾有新行 我是一个新的插座一般,所以任何帮助将不胜感激 客户端正在发送的文本 ct.txt3nd0fh34d3rl1n31663nd0fh34d3rl1n3This is a test message to see if the file decrypts properly. Nothing special in this file. Just a pure test
ct.txt3nd0fh34d3rl1n31663nd0fh34d3rl1n3This is a test message to see if the file decrypts properly. Nothing special in this file.
Just a pure test to see if my code works. Two chemists walk into a bar...
这根绳子的长度是205
在服务器端,我得到
ct.txt3nd0fh34d3rl1n31663nd0fh34d3rl1n3This is a test message to see if the file decrypts properly. Nothing special in this file.
Just a pure test to see if my code works. Two chemists walk into a bar...
这一行的长度是206,它在末尾添加了一个新行字符。这是在我尝试实施我发现的一些解决方案之后,但它们不起作用,因为我离开了1个字符。
下面是读取客户端输入的代码
while (incoming != null)
{
/* If the client has sent "exit", instruct the server to
* remove this thread from the vector of active connections.
* Then close the socket and exit.
*/
if (incoming.compareTo("exit") == 0)
{
parent.kill (this);
try {
in.close ();
sock.close ();
}
catch (IOException e)
{/*nothing to do*/}
return;
}
/* If the client has sent "die", instruct the server to
* signal all threads to shutdown, then exit.
*/
else if (incoming.compareTo("die") == 0)
{
parent.killall ();
return;
}
System.out.println("LINE: " + incoming.length() + " " + incoming);
if(incoming.equals("3nd0f5krr4hf1l3") && !msgGotten)
{
//Decrypt
SecretKeySpec key = CryptoUtilities.key_from_seed("H".getBytes());
System.out.print(message);
System.out.println(message.length());
byte[] encryptedMessageAndDigest = message.getBytes();
byte[] decryptedMessageAndDigest = CryptoUtilities.decrypt(encryptedMessageAndDigest, key);
if(CryptoUtilities.verify_hash(decryptedMessageAndDigest, key))
{
byte[] headerMessage = CryptoUtilities.extract_message(decryptedMessageAndDigest);
String payload = new String(headerMessage);
String[] plainTextArray = payload.split("3nd0fh34d3rl1n3", 3);
String fileName = plainTextArray[0];
byte[] plaintextBytes = plainTextArray[2].getBytes();
try
{
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(plaintextBytes);
fos.close();
}
catch (IOException e)
{
}
}
else
{
}
}
else
{
/* Otherwise, just echo what was received. */
//System.out.println ("Client " + idnum + ": " + incoming);
if(incoming.length() == 0)
{
message = message + "\n";
}
else
{
message = message + incoming + "\n";
}
}
提前谢谢
编辑:在我的初始传输结束时,我发送3nd0f5krr4hf1l3表示它。我需要
readLine()
,这样我就可以一次性解析,但是如果read()
确实是最好的方法,我该如何实现呢?\n(\r\n)字符会在读取中通过吗?如果您不能假定有行终止符,那么显然您首先不能使用readLine()
。您应该使用一个read()
重载
\n
(\r\n
)字符在读取过程中会通过吗
所有字符都将通过
read()
抱歉,我的意思是,每当原始文本中有换行符时,它都会变成单独的标记,但当标记为=换行符时,我似乎无法使其工作。我无法理解这一点,但如果您想保留行终止符,请不要使用readLine()
。