Java语言程序设计课程教学辅导技术资料——第10章JavaIO技术及应用(第2部分)
- 格式:docx
- 大小:21.61 KB
- 文档页数:11
Java语言程序设计课程教学辅导技术资料——第10章JavaIO技术及应用(第2部分)
目录
1.1Java语言程序设计课程教学辅导技术资料——第10章Java
I/O 技术及应用(第2部分) (2)
1.1.1随机文件读写 (2)
1.1.2过滤流--带格式的读写操作 (5)
1.1.3标准输入输出流 (9)
1.1Java语言程序设计课程教学辅导技术资料——第10章 Java
I/O 技术及应用(第2部分)
【教学目标】
在本单元中希望您重点了解并掌握如下知识点:文件名及目录名的File类,本地文件读写,管道方式读写,随机文件读写,字符格式文件读写,标准数据类的数据读写,标准输入输出设备流等。
1.1.1随机文件读写
1、RandomAccessFile类
(1)为什么要提供RandomAccessFile类
对于InputStream 和OutputStream 来说,它们的实例都是顺序访问流,也就是说,只能对文件进行顺序地读/写。随机访问文件则允许对文件内容进行随机读/写。在java中,类RandomAccessFile 提供了随机访问文件的方法。
(2)RandomAccessFile类的继承关系
它直接继承于Object类而非InputStream或者OutputStream类,从而可以实现读写文件任何位置中的数据(只需要改变文件的读写位置的指针)。
public class RandomAccessFile extends Object implements
DataInput, DataOutput
接口DataInput 中定义的方法主要包括从流中读取基本类型的数据、读取一行数据、或者读取指定长度的字节数。如:readBoolean( )、readInt( )、readLine( )、readFully( ) 等。
接口DataOutput 中定义的方法主要是向流中写入基本类型的数据、或者写入一定长度的字节数组。如:writeChar( )、writeDouble( )、write( ) 等。
2、要求:指明读写类型并改变文件读写位置的指针。
3、编程步骤
生成流对象并且指明读写类型;
移动读写的数据位置;
读写文件内容;
关闭文件。
4、应用要点
由于RandomAccessFile类实现了DataOutput与DataInput接口,因而利用它可以读写Java 中的不同类型的基本类型数据。比如采用readLong()方法读取长整数,而利用readInt()方法可以读出整数值等。
5、实例讲解
(1)RandomFileRW.java
import java.io.*;
public class RandomFileRW
{
public static void main(String args[])
{
StringBuffer buf=new StringBuffer();
char ch;
try
{
while( (ch=(char)System.in.read()) !='\')
{
buf.append( ch);
} //读写方式可以为"r" or "rw" RandomAccessFile myFileStream=
new RandomAccessFile("RandomUserInput.txt","rw");
myFileStream . seek(myFileStream.length()) ;
myFileStream.writeBytes(buf.toString()); //将用户从键盘输入的内容添加到文件的尾部
myFileStream.close();
}
catch(IOException e)
{
}
}
}
(2)RWFile.java
import java.io.*;
public class RWFile
{
public static void main(String args[]) throws
FileNotFoundException,IOException
{
File file=new File("sendFile");
FileInputStream fis=new FileInputStream(file);
RandomAccessFile myFileStream=new
RandomAccessFile("UserInput.txt","rw");
int fileLength=(int)myFileStream.length();
myFileStream.seek(fileLength);
byte buffer[]=new byte[(int)file.length()];
int bytes;
while((bytes=fis.read(buffer))!=-1)
{
myFileStream.write(buffer,0,bytes); myFileStream.seek(fileLength+bytes);
}
fis.close();
myFileStream.close();
}
}
1.1.2过滤流--带格式的读写操作
1、过滤流
(1)过滤流的作用
过滤流在读/写数据的同时可以对数据进行处理,它提供了同步机制,使得某一时刻只有一个线程可以访问一个I/O流,以防止多个线程同时对一个I/O流进行操作所带来的意想不到的结果。
(2)要求
将一个过滤流连接到另一个流上,则在从原始的流读写数据的同时,利用过滤流可以对这些数据进行加工处理。
(3)有关的类
类FilterInputStream和FilterOutputStream分别作为所有过滤输入流和输出流的父类。2、几种常见的过滤流
(1)BufferedInputStream和BufferedOutputStream
缓冲流,用于提高输入/输出处理的效率。BufferedInputStream类提供了一种“预输入”功能,它把输入数据在其缓冲区内暂存,在适当的时候把较大块的数据提交出去。而BufferedOutputStream提供“缓输出”功能,把输出数据暂存后,在适当时候大批送出。(2)DataInputStream 和DataOutputStream
不仅能读/写数据流,而且能读/写各种的java语言的基本类型,如:boolean,int,float 等。
(3)LineNumberInputStream
除了提供对输入处理的支持外,LineNumberInputStream可以记录当前的行号。
(4)PushbackInputStream 提供了一个方法可以把刚读过的字节退回到输入流中,以便重新再读一遍。
(5)PrintStream
打印流的作用是把Java语言的内构类型以其字符表示形式送到相应的输出流。
3、DataInputStream 和DataOutputStream类的编程
它们分别为FilterInputStream和FilterOutputStream类的子类。同时DataInputStream 和DataOutputStream类由于分别实现了DataInput和DataOutput接口中定义的独立于具体机器的带格式的读写操作,从而可以实现对Java中的不同类型的基本类型数据的读写。
(1)DataInputStream 和DataOutputStream类的编程方法
为了使用过滤流,用户需要在创建过滤流的同时将过滤流连接到另一个输入(输出)流上。如可以将DataInputStream连接到标准的输入流上,然后用户就可以方便地使用DataInputStream类的readXXX()方法类实现从标准输入中读取数据。
(2)实例讲解一:dataTest.java
import java.io.*;
public class dataTest
{
public static void main(String[] args) throws IOException { //
将数据写入某一种载体
DataOutputStream out = new DataOutputStream(new
FileOutputStream("invoice1.txt"));
double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 }; //价格
int[] units = { 12, 8, 13, 29, 50 }; //数目
String[] descs = { "T-shirt","裤子","帽子","手套","袜子" }; //描述
for (int i = 0; i < prices.length; i ++)
{ //写入价格,使用tab隔开数据
out.writeDouble(prices[i]);
out.writeChar('\'); out.writeInt(units[i]); //写入数目
out.writeChar('\');
out.writeChars(descs[i]); //写入描述,行尾加入换行符
out.writeChar('\');
}
out.close();
DataInputStream in = new DataInputStream(new
FileInputStream("invoice1.txt")); // 将数据读出
double price;
int unit;
StringBuffer desc;
double total = 0.0;
try { //当文本被全部读出以后会抛出EOFException例外,中断循环
while (true)
{
price = in.readDouble(); //读出价格
in.readChar(); //跳过tab
unit = in.readInt(); //读出数目
in.readChar(); //跳过tab
char chr;
desc = new StringBuffer(); //读出描述
while ((chr = in.readChar()) != '\')
desc.append(chr);
System.out.println("您已经订购了" + unit + " units of " + desc
+ " at $" + price);
total = total + unit * price;
}
}
catch (EOFException e)