InputStreamReader和 OutputStreamWriter
new InputStreamReader (byte stream, "UTF-8") // designation mode
The byte stream into a character stream, to facilitate processing, such processing can be used BufferedReader stream
may process the character set: InputStreamReader isr = new InputStreamReader (byte stream, "UTF-8");
network flow: new new the URL ( " HTTP: / /www.baidu.com ") openStream (), which is a byte stream.
stream into the decorative decorative convert character stream: BufferedReader Reader = new new BufferedReader (new new InputStreamReader (the URL of new new (" HTTP: // the WWW. Baidu.com "). openStream ()," UTF-. 8 "))
A conversion to read the keyboard input stream output from the byte stream:
try(BufferedReader isr=new BufferedReader( new
InputStreamReader(System.in));
BufferedWriter osw=new BufferedWriter(new OutputStreamWriter(System.out));){
//键盘循环读取,exit退出
String s=" ";
while(!s.equals("exit"))
{
s=isr.readLine(); //按行读取
osw.write(s); //写出
osw.newLine();
osw.flush(); //强制刷新,因为数据太小无法输出,要满足一定kb才会自动输出
}
}
catch(IOException e)
{
System.out.println("操作异常");
}
Operating a network stream, read Baidu Source:
Decorator:
//使用转换流,读取中文不会乱码
try(BufferedReader reader=new BufferedReader(new InputStreamReader(new URL("http://www.baidu.com").openStream(),"UTF-8"));
){ //网页的字符集是utf-8
//读取
String s;
while((s=reader.readLine())!=null)
{
System.out.println(s);
}
}
catch(IOException e)
{
System.out.println("操作异常");
}
}
Do not use the decorator:
try(InputStreamReader is=new InputStreamReader(new URL("http://www.baidu.com").openStream());
){
//读取
int len;
while((len=is.read())!=-1)
{
System.out.print((char)len);
}
}
catch(IOException e)
{
System.out.println("操作异常");
}