新行“\ n”的重要性在Java网络中 [英] Importance of the new line "\n" in Java networking

查看:93
本文介绍了新行“\ n”的重要性在Java网络中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我需要澄清一下 <\\ n>新行在String变量中的重要性

Well, I need a clarify of what is the importance of the "\n" new line in the String variable here

import java.net.*;
import java.io.*;

public class Whois{
    public static void main(String[] args){
        try{
            Socket soc = new Socket("whois.internic.net",43);
            InputStream in = soc.getInputStream();
            OutputStream out = soc.getOutputStream();
            String url = "http://www.infiniteskills.com\n";
            byte[] buffer = url.getBytes();
            out.write(buffer);
            int c;
            while((c = in.read()) != -1){
                    System.out.print((char)c);  
            }
            in.close();
            out.close();
        }catch(IOException e){
            System.out.println(e.getMessage());
        }
    }
}

注意: - 没有\\ \\ n该程序无法正常工作且没有输出。

Note :- without the \n the program doesn't work correctly and no output.

推荐答案

新行在Java网络中并不重要。

The newline has no importance in Java networking.

然而,它在许多协议中都很重要,其中许多协议基于 \r的'Telnet终端'约定\ n 作为行终止符。这当然包括SMTP,FTP,HTTP和Telnet本身。

However it does have importance in many protocols, many of which are based on the 'Telnet Terminal' convention of \r\n as the line terminator. This certainly includes SMTP, FTP, HTTP, and Telnet itself.

它对 BufferedReader.readLine()方法。你会在这里找到数百个关于 readLine()永远阻塞的问题,答案是'你正在读行但是你没有写行',即只是发送一个字符串没有行终止符。这不构成一个完整的行,因此 readLine()不会返回它。

It also has importance for the BufferedReader.readLine() method. You will find hundreds of questions here about readLine() blocking forever, to which the answer is 'you are reading lines but you are not writing lines', i.e. just sending a string with no line terminator. This does not constitute a complete line, so readLine() doesn't return it.

这篇关于新行“\ n”的重要性在Java网络中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆