java流与文件实验

实验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.编译并运行程序

万向A123进阶“协奏曲” 万向快速成长背后的原因是什么?
三大运营商营收净利双降,积极布局物联网等新业务
5G对对LNA性能的要求及解决方案
三大因素齐发力,国产半导体设备迎发展良机
当前自动驾驶技术发展是否进入到“下半场”
java流与文件实验
气密检测生产制造过程中不可或缺的环节
WiFi理论知识与射频调试经验分享
国内工业机器人密度低于全球水平,自动化提升空间大
科技巨头争相入局,卫星通信领域将迎来怎样的发展?
关于光模块行业的并购组合动态
SUV即将推出,李想距离实现他的出行理想还远吗?
如何选购选择太阳能充电器
人工智能市场规模预计到2024年将达到20.157亿美元
云客服系统让企业客服中心真正实现利润中心转变
RSA306B USB频谱分析仪的功能特点及应用范围
NI推出功能强大的工业控制器产品
智能电表和普通电表的区别 智能电表的五个分类
传感器是未来改变人类十大颠覆技术之首
路由器是干什么用的 路由器怎样桥接另一个路由器