了解getInputStream和getOutputStream [英] Understanding getInputStream and getOutputStream

查看:112
本文介绍了了解getInputStream和getOutputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码

  import java.io. *; 
import java.net。*;
公共类Whois
{
public static void main(String [] args)
throws Exception
{
// TODO自动生成的方法stub
int c;
Socket s = new Socket(whois.internic.net,43);
* InputStream in = s.getInputStream();
* OutputStream out = s.getOutputStream();
String str =(args.length == 0?osborne.com:args [0])+\ n;
byte buf [] = str.getBytes();
* out.write(buf);
System.out.print(嘿宝贝);
while((c = in.read())!= -1)
{
System.out.print((char)c);
}
s.close();
}
}

我已经标记了我对问题有所了解的陈述。我不明白OutputStream对象 out 在分配 s.getOutputStream()时会保留什么,需要什么将 buf 传递给 out out.write(buf)



我学习了输入和输出流使用文件,但我不明白 getinputstream outputstreams 。我用谷歌搜索了它,在SO上以及从许多不同的书和oracle文档中读取它。请详细讨论。



我知道如何从文件中读取以及如何写入文件。但在这里我不明白传递<$ c的必要性$ c> buf 只包含一个字符串的数组。我想要问的是,当有输入流的套接字为什么我们不能直接读取它?
什么是套接字输入流输出流



我在这里发现了一些东西,这里是链接


  • java DataOutputStream getOutputStream()getInputStream()


  • 示例getInputStream和getOutputStream


    1. http://zerioh.tripod.com/ressources/sockets.html

    新链接
    http ://docs.oracle.com/javase/tutorial/essential/io/buffers.html


    Here is a code

    import java.io.*;
    import java.net.*;
    public class Whois
    {
        public static void main(String[] args)
            throws Exception
        {
            // TODO Auto-generated method stub
            int c;
            Socket s = new Socket("whois.internic.net",43);
            *InputStream in = s.getInputStream();
            *OutputStream out = s.getOutputStream();
            String str = (args.length == 0 ? "osborne.com" : args[0] ) + "\n";
            byte buf[] = str.getBytes();
            *out.write(buf);
            System.out.print("hey baby");
            while ((c=in.read()) != -1)
            {
                System.out.print((char) c);
            }
            s.close();
        }
    }
    

    I have marked the statements that i have problem understanding.I do not understand what OutputStream object out will hold when it is assigned s.getOutputStream() and what is the need of passing buf to out by out.write(buf) .

    I have learned Input and output Streams using files but i do not understand getinputstream and outputstreams.I have googled it ,read it here on SO as well as from many different book and from oracle documents as well . please discuss it in detail .

    I know how to read from files and how to write to them.but here i do not understand what is the need of passing buf array which holds only a string.what i mean to ask is that when in has the input stream of the socket why cant we directly just read from it ? What exactly is a sockets inputstream and outputstream?

    I found something here on SO here is the link "Java Networking: Explain InputStream and OutputStream in Socket" ,here an answer by DNA says

    In Java, to send data via the socket, you get an OutputStream (1) from it, and write to the OutputStream (you output some data)."

    This is confusing me , when outputStream is used to send data via socket what was the need of out.write(buf) why do we need to send "google.com" to outputStream?

    解决方案

    first thing you need to understand is what is STREAM

    A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.

    ****Next is type of streams****

     we have byte stream and character stream.
    

    classes we have in Input Stream and output stream 
    

    as the name suggests in simple terms input stream is used to input the data and output stream is used to output the data

    Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are , FileInputStream and FileOutputStream. also

    Java Byte streams are used to perform input and output of 8-bit bytes, where as Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are , FileReader and FileWriter.. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.

    For reference

    1. What is InputStream & Output Stream? Why and when do we use them?

    2. java DataOutputStream getOutputStream() getInputStream()

    Example for getInputStream and getOutputStream

    1. http://zerioh.tripod.com/ressources/sockets.html

    New Link http://docs.oracle.com/javase/tutorial/essential/io/buffers.html

    这篇关于了解getInputStream和getOutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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