java流与文件实验

编程实验

72人已加入

描述

实验10 流与文件
一、实验目的
1. 理解数据流的概念
2. 理解Java流的层次结构
3. 理解文件的概念
二、实验要求
1. 掌握字节流的基本使用方法
2. 掌握字符流的基本使用方法
3. 能够创建、读写、更新文件   
三、实验内容
(一)使用标准数据流的应用程序
标准数据流指在字符方式下(如DOS 提示符)程序与系统进行输入输出的方式,键盘和显示器屏幕是标准输入输出设备,数据输入的起点为键盘,数据输出的终点是屏幕,输出的数据可以在屏幕上显示出来。
1. 程序功能:将键盘上输入的字符在屏幕上显示出来
2. 编写KY10_1.java 程序文件,源代码如下。
class KY10_1{
public static void main(String[] args) throws java.io.IOException {
byte buffer[]=new byte[10];
System.out.println("从键盘输入不超过10 个字符,按回车键结束输入:");
int count =System.in.read(buffer);//读取输入的字符并存放在缓冲区buffer 中
System.out.println("保存在缓冲区buffer 中元素的个数为:"+count);
System.out.println("buffer 中各元素的值为:");
for (int i=0;iSystem.out.print(" "+ buffer[i]);//在屏幕上显示buffer 元素的值
}
System.out.println();
System.out.println("输出buffer 字符元素:");
System.out.write(buffer, 0, buffer.length);
}
}
3. 编译、运行KY10_1.java 文件。
(二)使用文件输入输出流的应用程序
1. 程序功能:将保存在本地机当前文件夹中的KY10_2.HTML 文本文件的内容在屏幕上显示出来,然后将其另存为KY10_2.txt 文件。
2. 编写KY10_2.java 程序文件,源代码如下
import java.io.*;
public class KY5_4 {
public static void main(String[] args) throws IOException {
FileReader in=new FileReader("KY5_1.HTML");//建立文件输入流
BufferedReader bin=new BufferedReader(in);//建立缓冲输入流
FileWriter out=new FileWriter(" KY5_1.txt",true);//建立文件输出流
String str;
while ((str=bin.readLine())!=null) {
//将缓冲区内容通过循环方式逐行赋值给字符串str
System.out.println(str);//在屏幕上显示字符串str
out.write(str+"\n");//将字符串str 通过输出流写入KY5_1.txt 中
}
in.close();
out.close();
}
}
3. 编译、运行程序
(三)使用随机文件类的应用程序
使用文件输入类FileReader 只能将文件内容全部读入。如果要选择读入文件的内容,可使用随机文件类RandomAccessFile。
1. 程序功能:建立数据流,通过指针有选择的读入文件内容。
2. 编写KY10_3.java 程序文件,源代码如下。
import java.io.*;
class KY10_3 {
public static void main(String args[]) {
String str[]={"First line\n","Second line\n","Last line\n"};
try {
RandomAccessFile rf=new RandomAccessFile("KY5_5.txt", "rw");
System.out.println("\n 文件指针位置为:"+rf.getFilePointer());
System.out.println("文件的长度为:"+rf.length());
rf.seek(rf.length());
System.out.println("文件指针现在的位置为:"+rf.getFilePointer());
for (int i=0; i<3; i++)
rf.writeChars(str[i]); // 字符串转为字节串添加到文件末尾87
rf.seek(10);
System.out.println("\n 选择显示的文件内容:");
String s;
while ((s=rf.readLine())!=null)
System.out.println(s);
rf.close();
}
catch (FileNotFoundException fnoe) {}
catch (IOException ioe) {}
}
}
3. 编译并运行程序
(四)使用数据输入输出流与文件输入输出流类的应用程序
使用数据输入流DataOutputStream 和数据输出流DataInputStream 可以读取或写入任何Java 类型的数据,不用关心它们的实际长度是多少字节。一般与文件输入流FileInputStream 和输出流类
FileOutputStream 一起使用。
1. 程序功能:将整型数据和字符串对象通过数据输出流写到文件中。将文件中的整型数据和字符串对象通过数据输出流读出,并在屏幕上显示文件中的内容。
2. 编写KY10_4.java 程序文件,源代码如下。
import java.io.*;
public class KY10_4
{
public static void main(String arg[])
{
try
{ //添加方式创建文件输出流
FileOutputStream fout = new FileOutputStream("KY5_6.txt",true);
DataOutputStream dout = new DataOutputStream(fout);
dout.writeInt(1);
dout.writeChars("罗马"+"\n");
dout.writeInt(2);
dout.writeChars("北京"+"\n");
dout.close();
}
catch (IOException ioe){}
try
{
FileInputStream fin = new FileInputStream("KY5_6.txt");
DataInputStream din = new DataInputStream(fin);
int i = din.readInt();
while (i!=-1) //输入流未结束时,输入流结束时i 为-1
{
System.out.print(i+" ");
char ch ;
while ((ch=din.readChar())!='\n') //字符串未结束时
System.out.print(ch);
System.out.println();
i = din.readInt();
}
din.close();
}
catch (IOException ioe){}
}
}
3. 编译并运行程序
(五)使用对象输入输出流的应用程序
使用对象流可以直接写入或读取一个对象。由于一个类的对象包含多种信息,为了保证从对象流中能够读取到正确的对象,因此要求所有写入对象流的对象都必须是序列化的对象。一个类如果实现了Serializable 接口,那么这个类的对象就是序列化的对象。Serializable 接口没有方法,实现该接口的类不需要实现额外的方法。
1. 程序功能:保存对象信息到文件,并将文件中的对象信息显示出来。
2. 编写KY10_5.java 程序文件,源代码如下。
import java.io.*;
public class KY5_7 implements Serializable //序列化接口
{
int bh=1;int nl=21;
String xm;
KY5_7(int bh,String xm,int nl)//构造方法
{
this.bh = bh;
this.xm = xm;
this.nl = nl;
}
KY10_5()//构造方法
{
this(0,"",21);
}
void save(String fname)//保存到文件中的方法
{
try
{
FileOutputStream fout = new FileOutputStream(fname);//输出文件流
ObjectOutputStream out = new ObjectOutputStream(fout);//输出对象流
out.writeObject(this); //写入对象
out.close();
}
catch (FileNotFoundException fe){}
catch (IOException ioe){}
}
void display(String fname)//显示文件中对象信息的方法
{
try
{
FileInputStream fin = new FileInputStream(fname);//输入文件流
ObjectInputStream in = new ObjectInputStream(fin);//输入对象流
KY10_5 OO = (KY5_7)in.readObject(); //读取对象
System.out.println(" 类名: "+OO.getClass().getName()+"
"+OO.getClass().getInterfaces()[0]);
System.out.println(" "+OO.bh+" "+OO.xm+" "+OO.nl);
in.close();
}
catch (FileNotFoundException fe){}
catch (IOException ioe){}
catch (ClassNotFoundException ioe) {}
}
public static void main(String arg[])
{
String fname = "KY5_7.obj";
KY10_5 O1= new KY5_7(1,"张驰",14);
O1.save(fname);
O1.display(fname);
}
}
3. 编译并运行程序
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分