JAVA输入流,
分享于 点击 43053 次 点评:7
JAVA输入流,
今天总结一下本项目中的java输入输出流的几种用法
1、BufferedInputStream:允许程序一次一个字节地从流读取数据
这样可以设定每次读取的字节数,比如下载文件时,可以采用下面的句子
URL url = new URL(urlStr);
BufferedInputStream in = new BufferedInputStream(url.openStream());
File newFile = new File(saveFile);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(newFile));
byte[] buf = new byte[2048];
int length = 0;
while ((length = in.read(buf)) != -1)
{
out.write(buf, 0, length);
}
2、DataInputStream:提供读取任意对象的能力
比如读取socket传递来的字符串消息
Socket client = new Socket(hostName,"8080");
DataInputStream din = new DataInputStream(client.getInputStream());
Message msg =din.readUTF();
3、BufferedReader:接受Reader对象为参数,并对其添加字符缓冲器
比如读取键盘输入的字符串
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while((userInput = stdIn.readLine())!="exit")
{
System.out.println("您输入的内容为:"+userInput);
}
相关文章
- 暂无相关文章
用户点评