JAVA标准输入输出流

 

Java程序设计精讲第12章—— java流与文件操作。

      主要讲述了JAVA数据流和输入/输出流,标准数据流主要分为3种:标准的输入(System.in)、标准的输出(System.out)、标准的错误输出(System.err)。

System有三个成员 in out err;

1. System.in

      作为字节输入流类InputStream的对象in实现标准的输入,其中有read()方法从键盘接收数据。

2. System.out

      作为打印流类PrintStream的对象out实现标准输出。其中有print和println两个方法,这两个方法支持java的任意的基本类型作为参数。

3. System.err

      与System.out相同,以PrintStream类的对象err实现标准的错误输出。

从键盘输入字符的代码如下:

Import  java.io.*;

Public class System Test{

Public static void main(String [] args)throws  IOException{

System.out.println(“Please input a  String:”);

//创建数组缓冲区

Byte b [] = new byte[512];

//从标准输入流读取数据

Int ocunt=System.in.read(b);

//按字符方式输出b元素值

System.out.println(“your input String is:”);

For (int i=0;i<count; i++)

System.out.println((char)b[i]);

}

}

程序运行,输入字符 “This is a Test” 按Enter键,字符串保存在缓冲区中;当程序运行到系统标准输出语句时,把数据从缓冲区写到标准输出设备屏幕上:mian()方法采用throws语句声明抛出IOException异常交由系统处理。

还有 JAVA.IO包中的数据流及文件类,分别有 字节流类(InputStream和OuputStream)、字符流类(Reader和Writer)、文件类(File和RandomAccessFile)。最后讲了字节文件的输入/输出流(FileInputStream/FileOutputStream)以及它们的构造方法。例如:

Public FileInputStream(String name) throws FileNotFoundException

qi

Public FileInputStrream(String file) throws fileNotFoundException

Name为文件名,file为文件类File的对象,即可以以文件或File对象构造文件输入流对象。

例如作业中用到的一文件名data.txt构造文件数据流入对象f.

FileInputStream f = new FileInputStream(“data.txt”);

读取字符串的方法 red()方法有3种:

1、 public int read()throws IOException

2、 public int read(byte[] b)throws IOException

3、 public int read(byte[] b,int off, int len)throws IOException

关闭输入流:(public void close() thorws IOException:close()

 

2011.08.29

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值