集美大学诚毅学院实验报告
课程名称: Java程序设计 实验地点: 实验项目名称:实验4 实验项目编号:IO
一、目的(本次实验所涉及并要求掌握的知识点)
1、数字存取:通过随机数函数产生50个正整数,逐个将这些数写入文件file.dat中;然后读出这些数,在内存中对它们进行排序,再将排序后的数据逐个写入file.out文件中。
2、文件内容加行号:给定一个文件,通过文件读写,给文件内容加行号。 要求:使用缓冲流操作。
3、学生对象操作:设计学生类,把学生对象存储进文件,并能再从文件中读取。 要求:使用对象流操作。
二、实验内容与设计思想(设计思路、主要数据结构、主要代码结构、主要代码段分析、输出结果图)
题目一代码
package ioshiayan1;
import java.io.*; import java.util.*;
public class io_one {
public static void main(String[] args) throws IOException {
File a=new File(\File b=new File(\a.createNewFile(); b.createNewFile(); BufferedReader in=null;
BufferedWriter out1=null,out2=null; Random rand=new Random(47); int[] array=new int[50]; try{
班级: 姓名: 学号: 组号: 实验成绩: 上机实践日期: 上机实践时间: }
}
out1=new BufferedWriter(new FileWriter(a)); out2=new BufferedWriter(new FileWriter(b)); for(int i=0;i
out1.close();
Scanner sc = new Scanner(a); for(int i=0;sc.hasNextInt();i++) { }
Arrays.sort(array);
for(int i=0;i
out2.write(String.valueOf(array[i])); out2.write(\if(i==9)
out2.newLine(); array[i]=sc.nextInt();
out1.write(String.valueOf(rand.nextInt(20))); out1.write(\if(i==9)
out1.newLine();
}
}finally{ }
if(in!=null){ }
if(out2!=null){ }
out2.close(); in.close();
题目二代码
package ioshiyan2; import java.io.*; import java.util.*; public class io_two { }
public static void main(String[] args) throws IOException { }
List
File f=new File(\); Scanner sc=new Scanner(f); int k=0;
while(sc.hasNextLine()){
list.add(++k+\+sc.nextLine()); }
BufferedWriter out=new BufferedWriter(new FileWriter(f)); for(int i=0;i System.out.println(\操作已经成功完成!\); }finally{} 题目三代码 io_three package ioshiyan3; import java.io.*; public class io_three { public static void main(String[] args) throws IOException, ClassNotFoundException { } stu[] st={new stu(\张三\,45,\男\), new stu(\李四\,56,\男\), new stu(\王五\,67,\女\)}; File file=new File(\); // if(!file.exists()) file.createNewFile(); } ObjectOutputStream out=null; try{ } ObjectInputStream in=null; try{ } in=new ObjectInputStream(new FileInputStream(file)); stu[] st2={null,null,null}; for(int i=0;i st2[i]=(stu)in.readObject(); System.out.println(st2[i]); out=new ObjectOutputStream(new FileOutputStream(file)); for(int i=0;i out.close(); out.writeObject(st[i]); }finally{ } in.close(); }finally{ Stu package ioshiyan3; import java.io.Serializable; public class stu implements Serializable{ String stu_name; int stu_no; String stu_age; public stu(String stu_name,int stu_no,String stu_age) { } public String toString() { return \姓名:\+stu_name+\学号:\+stu_no+\性别:\+stu_age; this.stu_name=stu_name; this.stu_no=stu_no; this.stu_age=stu_age; } } 三、实验使用环境(本次实验所使用的平台和相关软件) eclipse 四、实验小结(实验中遇到的问题及解决过程、实验中产生的错误及原因分析、实验体会和收获) 学会用Java语言实现文本文件的创建、提取和使用