——JAVA篇
使用IO流进行输入输出
一、练习目标:
用IO流对输入输出处理,以及对文件的操作。
本次练习所用到的所有类都在java.io.*包中。
二、练习时间与作业要求:
本次练习一共需要180分钟。翻译练习写在JAVA笔记本上,书面作业写在JAVA家庭作业本上。
后面的书面作业不包括在这个时间范围内。
三、翻译练习:
将下面要求翻译的内容,写在记事本上(阴影部分不写出来,要求看懂)
Method Detail read
public abstract int read() throws IOExceptionReads
java类的概念the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. A subclass must provide an implementation of this method. Returns:the next byte of data, or -1 if the end of the stream is reached. Throws: IOException - if an I/O error occurs.
public int read(byte[] b) throws IOExceptionReads
some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown. If b is null, a NullPointerException is thrown. If the length of b is zero, then no bytes are read and 0 is r
eturned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b. The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected. If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed. The read(b) method for class InputStream has the same effect as:read(b, 0, b.length) Parameters:b - the buffer into which the data is read. Returns:the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached. Throws: IOException - if an I/O error occurs. NullPointerException - if b is null.See Also:
四、上机练习:
1、 编写一个程序,在D盘根目录下创建一个以自己名称命名的文件夹,并在该文件夹下创建一个以自己名称命名的文本文件。
2、 编写java程序,查看刚才所创建的文件的属性:包括是否能读、写、相对路径、绝对路径、长度,最后修改时间等。
3、 编写程序向文本文件中写入自己的信息,格式为:姓名:XXX 性别:X 年龄:XX 班级:XXX,将该信息读出后显示的屏幕上后把文件删除。
4、 编写一个程序,将你所编写的.java文件读出显示在屏幕上。
五、书面作业:
1、 在JAVA中流的概念是什么?
2、 JAVA中的流是怎么分类的?
3、 File类的作用是什么?它是一种流吗?
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论