java实验05.docx
- 文档编号:14144175
- 上传时间:2023-06-21
- 格式:DOCX
- 页数:23
- 大小:422.77KB
java实验05.docx
《java实验05.docx》由会员分享,可在线阅读,更多相关《java实验05.docx(23页珍藏版)》请在冰点文库上搜索。
java实验05
电子信息学院
实验报告书
课程名:
软件开发基础Java
(2)
题目:
实验05Java中的IO流类
实验类别:
【设计】
班级:
软件1412
学号:
141003330212
姓名:
张杨松
一、实验目的
(1)理解数据流的概念。
(2)了解Java流的层次结构。
(3)理解并掌握文件的读写操作过程和方法。
(4)掌握File、BufferedReader、FileInputStream、FileOutputStream、IOException、FileNotFoundException等类的基本使用方法。
(5)掌握对象的序列化和反序列化、随机访问文件类等常用I/O类的使用方法。
二、实验内容
1.编写程序:
以文本方式向文件result.txt写入你的学号,姓名,年龄,性别(字符'M'或'F'),身高(m为单位),每项信息单独占一行;读取该文件,并在屏幕上显示每一项信息。
2.编写程序:
以二进制方式向文件result.dat中写入你的学号,姓名,年龄,性别(字符'M'或'F'),身高(m为单位);读取该文件,并在屏幕上显示每一项信息。
注意:
姓名需要按照字符的形式用readChar()读写。
3.定义可序列化的类Student,具有学号,姓名,年龄,性别(字符'M'或'F'),身高(m为单位)等成员变量,初始化这些成员变量的构造方法及用于比较两个对象对应属性(成员变量)是否完全相同的equals()方法。
main()中用你的信息创建一个Student类对象s,将其序列化后写入文件student.dat中,再将对象从文件中反序列化并保存至Student类对象t中。
在屏幕上输出t的成员变量及t与s是否相同的比较结果。
4.编写程序:
将array.txt中的数据读为一个二维数组,并在屏幕上按行显示每个元素,再将所有元素写入arr.txt中,每个元素占一行。
array.txt的内容如下:
11121314
21222324
31323334
5.编写程序,提示用户输入一个mp3歌曲文件的文件名(可以带有路径),通过代码将该歌曲中的歌手名字修改为自己的学号和姓名,并通过音乐播放器进行播放验证。
提示:
需采用随机访问文件类(RandomAccessFile)
要求:
以上每个题目要单独定义一个类,类名不限,只需在main()方法中完成功能。
三、实验结果(关键代码和运行结果)
(1)
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.OutputStreamWriter;
/**
*测试信息的写入和读取
*
*@authorAdministrator
*
*/
publicclassDemoIO{
BufferedWriterbw=null;
BufferedReaderbr=null;
Filefile=null;
/**
*将学生信息写入到文件中
*/
publicvoidwriterMsg(){
Stringmsg[]={"学号:
141003330212","姓名:
张杨松","年龄:
19","性别:
男","身高:
165"};
Stringpath="D:
/java/result.txt";
StringparentPath="D:
/java";
file=newFile(parentPath);
if(!
file.exists()){
file.mkdir();
}
try{
bw=newBufferedWriter(newOutputStreamWriter(
newFileOutputStream(path)));
br=newBufferedReader(newInputStreamReader(newFileInputStream(
path)));
for(inti=0;i bw.write(msg[i]); bw.newLine(); } bw.flush(); System.out.println("学生信息写入完毕....."); System.out.println("---------------------"); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ //关闭并释放资源 bw.close(); }catch(IOExceptione){ e.printStackTrace(); } } } /** *从文件中中读取学生的信息 */ publicvoidreadMsg(){ Stringstr=null; try{ while((str=br.readLine())! =null){ System.out.println(str); } }catch(IOExceptione){ e.printStackTrace(); }finally{ //关闭并释放资源 try{ br.close(); }catch(IOExceptione){ e.printStackTrace(); } } } publicstaticvoidmain(String[]args){ DemoIOdio=newDemoIO(); dio.writerMsg(); dio.readMsg(); } } (2) importjava.io.DataInputStream; importjava.io.DataOutputStream; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; /** *测试数据流的使用 * *@authorAdministrator * */ publicclassDemoDataOuputStream{ Filefile=null; Stringpath="D: /java/r.dat"; DataOutputStreamdos=null; DataInputStreamdis=null; /** *将学生信息写入到文件中 */ publicvoidwriterMsg(){ StringparentPath="D: /java"; intclasno=1412; Stringname="张杨松"; intage=19; charsex='M'; doubleheight=1.65; file=newFile(parentPath); //如果文件夹不存在,就创建文件夹 if(! file.exists()){ file.mkdir(); } try{ dos=newDataOutputStream(newFileOutputStream(path)); dos.writeInt(clasno); dos.writeChars(name); dos.writeInt(age); dos.writeChar(sex); dos.writeDouble(height); dos.flush(); System.out.println("文件信息写入成功......"); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ dos.close(); }catch(IOExceptione){ e.printStackTrace(); } } } /** *从文件中中读取学生的信息 */ publicvoidreadMsg(){ intclasno; String_name; intage; charsex; doubleheight; charname[]=newchar[3]; try{ dis=newDataInputStream(newFileInputStream(path)); clasno=dis.readInt(); for(inti=0;i name[i]=dis.readChar(); } _name=newString(name); age=dis.readInt(); sex=dis.readChar(); height=dis.readDouble(); System.out.println("正在读取学生信息......"); System.out.println("学号: "+clasno+"\n姓名: "+_name+"\n年龄: "+age +"\n性别: "+sex+"\n身高: "+height+"m"); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ dis.close();//释放并关闭资源 }catch(IOExceptione){ e.printStackTrace(); } } } publicstaticvoidmain(String[]args){ DemoDataOuputStreamdio=newDemoDataOuputStream(); dio.writerMsg(); dio.readMsg(); } } (3) /** *测试可序列化的接口: Serializable * *@authorAdministrator * */ importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; importjava.io.ObjectInputStream; importjava.io.ObjectOutputStream; importjava.io.Serializable; publicclassStudentimplementsSerializable{ Stringsno; Stringname; intage; charsex; doubleheight; Stringpath="D: /java/student.dat"; publicStudent(Stringsno,Stringname,intage,charsex,doubleheight){ this.sno=sno; this.name=name; this.age=age; this.sex=sex; this.height=height; } /** *重写分类的toString()方法 */ publicStringtoString(){ return"学号: "+sno+"\n姓名: "+name+"\n年龄: "+age+"\n性别: "+sex +"\n身高: "+height; } /** *重写父类的equals()方法,判断两个学生对象是否相等 */ publicbooleanequals(Objectobj){ Students=(Student)obj; if(this.sno.equals(s.sno)&&this.name.equals(s.name) &&this.age==s.age&&this.sex==s.sex &&this.height==s.height){ returntrue; } returnfalse; } /** *将学生的信息写入到文件中 * *@paramobj */ publicvoidwriteStudent(Objectobj){ Filefile=null; StringparentPath="D: /java"; ObjectOutputStreamoos=null; file=newFile(parentPath); if(! file.exists()){ file.mkdir(); } try{ oos=newObjectOutputStream(newFileOutputStream(path)); oos.writeObject(obj); oos.flush(); System.out.println("学生对象写入成功......"); System.out.println("--------------------"); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ //关闭并释放资源 oos.close(); }catch(IOExceptione){ e.printStackTrace(); } } } /** *从文件中读取学生对象 * *@return */ publicStudentreadStudent(){ ObjectInputStreamois=null; Studentstudent=null; try{ ois=newObjectInputStream(newFileInputStream(path)); student=(Student)ois.readObject(); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }catch(ClassNotFoundExceptione){ e.printStackTrace(); }finally{ try{ ois.close();//关闭并释放资源 }catch(IOExceptione){ e.printStackTrace(); } } returnstudent; } publicstaticvoidmain(String[]args){ Students=newStudent("141003330212","张杨松",19,'男',1.65); s.writeStudent(s); Studentt=s.readStudent(); System.out.println(t); System.out.println("--------------------"); System.out.println("两个对象是否相等: "); System.out.println(s.equals(t)); } } (4) /** *测试BufferedReader类的文本的写入和读取 * *@authorAdministrator * */ importjava.io.BufferedReader; importjava.io.BufferedWriter; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; importjava.io.InputStreamReader; importjava.io.OutputStreamWriter; publicclassDemoBufferedReader{ Stringpath="D: /java/array.txt"; String_path="D: /java/arr.txt"; BufferedReaderbr=null; BufferedWriterbw=null; /* *读取文件中的信息,然后将其转换为二维数组 */ publicint[][]getArr(){ intindex=0; intarr[][]=newint[3][4]; Stringstr=null; Stringtmp[]=null; try{ br=newBufferedReader(newInputStreamReader(newFileInputStream( path))); while((str=br.readLine())! =null){ tmp=str.split(""); for(inti=0;i arr[index][i]=Integer.parseInt(tmp[i]); } index++; } System.out.println("信息读取成功......\n"); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ //关闭并释放资源 br.close(); }catch(IOExceptione){ e.printStackTrace(); } } returnarr; } /** *显示二维数组的信息,即遍历二维数组 * *@paramarr */ publicvoidshowInfo(intarr[][]){ for(inti=0;i for(intj=0;j System.out.print(arr[i][j]+""); } System.out.println(); } } /** *将二维数组写到文件中 * *@paramarr */ publicvoidwriteArr(int[][]arr){ try{ bw=newBufferedWriter(newOutputStreamWriter( newFileOutputStream(_path))); for(inti=0;i for(intj=0;j bw.write(String.valueOf(arr[i][j])); bw.newLine(); } } bw.flush(); System.out.println("信息写入成功......"); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ bw.close();//关闭并释放资源 }c
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 实验 05