关于流的问题

xabba 2003-03-03 03:09:45
大家好,我初学java,现在看到<理解流>这一章了,可是看着很是纳闷,感到难以理解.
比如:java是如何区分输入流来自什么设备的?还有InputStream;OutputStream与readers;writers与标准流三者之间的关系?我自己理不清了,请哪位耐心的朋友能给我讲一下,最好再带一个键盘输入并写入文件的例子,谢谢了.
...全文
50 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeahspyme 2003-03-08
  • 打赏
  • 举报
回复
/**
*Notebook.java
*
*receive Name,QQ,Email from keyboard,and save into a file.
*
*@author yeahspyme
*@version 1.0, Mar 8 2003
*
*/

package app;
import java.io.*;

public class Notebook
{
public static void main(String[] args)
{
byte[] Name=new byte[15];
byte[] QQ=new byte[10];
byte[] Email=new byte[30];
File fNote=new File("notebook.txt");
FileOutputStream fOut;

try
{
System.out.print("Name:");
System.in.read(Name,0,15);
System.out.print("QQ:");
System.in.read(QQ,0,10);
System.out.print("Email:");
System.in.read(Email,0,20);
}
catch(IOException e)
{
System.out.println("Error: input not available.");
}

try
{
if(fNote.exists())
{
fOut=new FileOutputStream(fNote,true);
}
else
{
fNote.createNewFile();
fOut=new FileOutputStream(fNote);
}
fOut.write(Name);
fOut.write(QQ);
fOut.write(Email);
fOut.close();
}
catch(Exception e)
{
System.out.println("Error: file error.");
}
}
}
现在这个例子还有一个问题,输出格式不是非常好,大家可以加以改进。有进展给我发消息。
xabba 2003-03-07
  • 打赏
  • 举报
回复
谢谢大家的帮助!
要是给个键盘输入个人资料(姓名,QQ号,Email)然后写入文件的例子,剩下的30分全送了,呵呵,
storm999 2003-03-06
  • 打赏
  • 举报
回复
import java.io.*;
import java.net.*;

public class TestChar {
public static void main(String[] args){
BufferedReader in = null;
FileWriter out = null;
try {
while(true){
//建立緩存﹐并轉化為雙字節字符形式
in = new BufferedReader(
new InputStreamReader(System.in));
out = new FileWriter("e:\\abbcc.txt",true);
String line;
line = in.readLine();
//當輸入'bye'時退出
if (line.equalsIgnoreCase("bye")) break;
//加入換行符
out.write(line+"\r\n");
out.flush();
}
}
catch (IOException ex) {
}
finally{
try {
//關閉流
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
catch (IOException ex1) {
}
}

}
}
wyqiang 2003-03-06
  • 打赏
  • 举报
回复
就是c++中的io吧
xabba 2003-03-06
  • 打赏
  • 举报
回复
给个例子我送分
zhangsanja 2003-03-03
  • 打赏
  • 举报
回复
OutputStream、InputStream 是抽象类,我们在实际开发中很少使用的,流是为了不依赖硬件而设计的,但我觉得流最好是理解为管道,只要适合就可以通过参数套接使用,当然每个流有不同的用途。
biti_9512207 2003-03-03
  • 打赏
  • 举报
回复
看tutorial吧.
xabba 2003-03-03
  • 打赏
  • 举报
回复
你好,能不能给一个键盘输入个人资料,包括姓名\身份证\住址\Email地址,并将其写入一个.txt文件的实例,让我更好的理解流.谢谢
yeahspyme 2003-03-03
  • 打赏
  • 举报
回复
流是一个抽象的概念,是有向的二进制数据,方向是从源到目的。流的抽象就是为了脱离硬件环境而做的,有了流的概念,我们就不必区分输入端到底是特定的键盘,还是某个文件,当然也不用区分输出端是显示器,是文件,还是网络端口,从而能够简单地进行传输。
hayai 2003-03-03
  • 打赏
  • 举报
回复
Stream是面向byte的。
reader/writer是面向字符的。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧