一、实验目的:
【目的要求】
1.了解输入/输出类的种类及方法
2.掌握对File类、字节流及字符流的使用
【注意事项】
1.注意电源插座的用电安全;
2.遵守计算机的使用注意事项;
3.防范病毒。
二、实验环境
【使用工具】
1.电脑
2.window系统
3.JDK环境
4.eclipse开发环境
三、实验内容
【相关知识】
【主要内容】
1.模拟老师批改作业的程序。将指定的目录中docx文档找出来,并写到score.txt文件中,docx文档需要有,文件名和路径,并用tab键分隔。
程序提示:遍历指定的文件夹,遍历出来的文件进行判断,文件是docx的,将其写入score.txt中,否则,将不执行任何操作。
代码如下:
package Text8_1;
import java.io.*;
import java.util.ArrayList;
public class FileDoc {
public static void main(String[] args) throws IOException {
//根据给定的路径创建一个File对象
// File srcFile = new File("E:\\itcast");
File srcFile = new File("C:\\Users\\庆\\Desktop\\word集合");
//调用方法
getAllFilePath(srcFile);
}
//定义一个方法,用于获取给定目录下的所有内容,参数为第1步创建的File对象
public static void getAllFilePath(File srcFile) throws IOException {
//获取给定的File目录下所有的文件或者目录的File数组
File[] fileArray = srcFile.listFiles();
ArrayList<String> array=new ArrayList<String>();
//遍历该File数组,得到每一个File对象
if(fileArray != null) {
for(File file : fileArray) {
//判断该File对象是否是目录
if(file.isDirectory()) {
//是:递归调用
getAllFilePath(file);
}else{
String a=file.getAbsolutePath();
array.add(a);
System.out.println(file.getAbsoluteFile());
}
}
}
// System.out.println("");
// String [] strArray = srcFile.list(new FilenameFilter(){
// public boolean accept(File dir, String name){
// return new File(dir,name).isFile() && name.endsWith(".docx");
// }
// });
// for(String f: strArray){
// System.out.println(f);
// }
BufferedWriter bw=new BufferedWriter(new FileWriter("score.tx"));
//遍历集合
for(String s:array){
//按照指定字符串形式拼接
StringBuffer sb=new StringBuffer();
sb.append(s);
//集合每一个字符写到文件里面
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
}
}
2.制作一个简单的日记本,要求有菜单,创建日记、打开日记、编辑日记、保存、退出等
在这里插入代码片package Text8_2;
import java.io.*;
import java.util.Scanner;
public class DiaryDemo {
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\Ideaproject\\Text89\\Dairy.txt", true));
System.out.println("----日记菜单--------");
System.out.println("1.新建文件");
System.out.println("2.打开文件");
System.out.println("3.修改文件");
System.out.println("4.保存");
System.out.println("5退出");
while (true) {
System.out.println("请输入操作指令:");
int a = s.nextInt();
switch (a) {
case 1:
add(bw);
break;
case 2:
Open();
break;
case 3:
read();
break;
case 4:
System.out.println("保存成功!");
break;
case 5:
System.out.println("结束");
System.exit(0);
default:
System.out.println("您的输入有误,请重新选择!");
break;
}
}
}
//新建文件
public static void add(BufferedWriter bw) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入内容,停止编写输入stop");
String dairy = sc.nextLine();
while (!dairy.equals("stop")) {
bw.write(dairy + "\r\n");
dairy = sc.nextLine();
}
bw.close();
}
//打开文件
public static void Open() throws IOException {
System.out.println("输入路径·");
Scanner s = new Scanner(System.in);
String path = s.nextLine();
BufferedReader br = new BufferedReader(new FileReader(path));
System.out.println("文件已经打开");
char[] chs = new char[1024];
int len;
while ((len = br.read(chs)) != -1) {
System.out.println(new String(chs, 0, len));
}
br.close();
}
//修改文件
public static String read() throws IOException {
System.out.println("输入路径·");
Scanner s = new Scanner(System.in);
String filePath= s.nextLine();
BufferedReader br = null;
String line = null;
StringBuffer buf = new StringBuffer();
try {
// 根据文件路径创建缓冲输入流
br = new BufferedReader(new FileReader(filePath));
// 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
while((line = br.readLine()) != null){
if(line.startsWith("亲爱")){
// buf.append(line).append(" 郭浩康");
System.out.println("修改为");
String b=s.nextLine();
buf.delete(0,line.length());
buf.append(b);
System.out.println("修改成功");
}else{
buf.append(line);
}
buf.append(System.getProperty("line.separator"));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buf.toString();
}
}
,
3.学生成绩单的存储和管理。要求有输入某位同学的成绩(可以连续输入,可以多次启动程序继续输入),保存到文件中,并可以随时查询所有同学的成绩,可以检查是否重复输入成绩,以及每次运行都有前面已经录入的同学成绩信息。
提示:
(1)建立student类(序列化),其中属性包括:id(学号)、name(姓名)、course(课程)、score(成绩)
(2)主程序需要创建这个对象集合,并提供菜单,来操作查询和录入操作(录入操作注意要检查重复)
(3)在程序进入后,需要通过对象反序列化将文件中存储的对象一一读取出来,退出之前,需要将对象序列化,将数据保存。
//学生类
package Text8_3;
// id(学号)、name(姓名)、course(课程)、score(成绩)
public class Student {
private String num;
private String name;
private int score;
private String course;
public Student() {
}
public Student(String num, String name, int score, String course) {
this.num = num;
this.name = name;
this.score = score;
this.course = course;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return this.score;
}
public void setScore(int score) {
this.score = score;
}
public String getCourse() {
return this.course;
}
public void setCourse(String course) {
this.course = course;
}
}
//主函数
package Text8_3;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentDemo {
public static void main(String[] args) throws IOException {
Scanner temp = new Scanner(System.in);
ArrayList<Student> array = new ArrayList<Student>();
while (true) {
System.out.println("--欢迎使用学生管理系统");
System.out.println("1.添加学生成绩");
System.out.println("2.查询所有学生成绩");
System.out.println("3.退出系统");
System.out.println("输入选项");
String choice = temp.nextLine();
switch (choice) {
case "1":
add(array);
break;
case "2":
aquire(array);
break;
case "3":
System.out.println("感谢您的使用,再见!");
System.exit(0);
default:
System.out.println("您的输入有误,请重新选择!");
}
}
}
private static void add(ArrayList<Student> array){
Scanner temp = new Scanner(System.in);
String num;
while(true){
System.out.println("请输入新同学学号:");
num = temp.nextLine();
//判断是否重复学号
boolean flag = false;//默认未重复
for(int i=0;i<array.size();i++){
Student s = array.get(i);
if(s.getNum().equals(num)){
System.out.println("该学号已经存在,请重新输入!");
flag = true;
break;
}
}
if(!flag){
break;
}
}
System.out.println("请输入新同学姓名:");
String name = temp.nextLine();
System.out.println("请输入新同学科目:");
String course = temp.nextLine();
System.out.println("请输入新同学成绩:");
int score = temp.nextInt();
Student s = new Student();
s.setNum(num);
s.setName(name);
s.setScore(score);
s.setCourse(course);
array.add(s);
System.out.println("添加学生信息成功!");
}
private static void aquire(ArrayList<Student> array) throws IOException {
if(array.size() == 0){
System.out.println("暂时未存储任何学生信息,请重新选择。");
return;
}
BufferedWriter bw=new BufferedWriter(new FileWriter("java.txt"));
//遍历集合
bw.write("学号\t姓名\t科目\t成绩");
bw.newLine();
for(Student s:array){
//按照指定字符串形式拼接
StringBuffer sb=new StringBuffer();
sb.append(s.getNum()).append("\t\t").append(s.getName()).append("\t").append(s.getCourse()).append("\t").append(s.getScore()).append("\t");
//集合每一个字符写到文件里面
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
System.out.println("学号\t姓名\t科目\t成绩");
for(int i=0;i<array.size();i++){
Student s = array.get(i);
System.out.println(s.getNum()+"\t\t"+s.getName()+"\t"+s.getCourse()+"\t"+s.getScore());
}
}
}