JAVA--U5 输入输出流复习

实习一 文件内容合并

题目描述

题解
public class FileMerge {
	public static void main(String args[]) throws FileNotFoundException {
		File file1 = new File("D:\\eclipse\\qimoExercise\\QiMo\\src\\poem\\1.txt");
		File file2 = new File("D:\\eclipse\\qimoExercise\\QiMo\\src\\poem\\2.txt");
		//目录项
		File file = new File("D:\\eclipse\\qimoExercise\\QiMo\\src\\poem");
	    //建立在工程目录下
		File fileLi = new File("李白诗集.txt");
		if(!fileLi.exists()) {
			try {
				fileLi.createNewFile();
			   //若文件不存在就创建
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		File list[] = file.listFiles();
		FileReader freader=null;
		FileWriter fwriter=null;
		//利用BufferedWriter和BufferedReader来进行读写 --上层流
		BufferedReader Breader = null;
		BufferedWriter Bwriter=null;
		try {
			fwriter = new FileWriter(fileLi);
			Bwriter = new BufferedWriter(fwriter);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		for(int i =0;i<list.length;i++) {		
				freader = new FileReader(list[i]);	
				Breader = new BufferedReader(freader);
				String str =null;
				try {
					while((str=Breader.readLine())!=null) {
							Bwriter.append(str);
							Bwriter.newLine();
					}
				}catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
			    }			
		}
		try {
			Breader.close();
			Bwriter.close();
		} catch (IOException e) {
          //TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  文件存储结构如下图

实习二 单文件拷贝

题目描述

题解 
public class Copy {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		// 目录文件和内容文件创建
		File source = new File("source");
		File dest = new File("dest");
        //根目录下src中source下的file1.txt
		File s_file1 = new File(".\\source\\file1.txt");
		File s_file2 = new File(".\\source\\file2.txt");
		File d_file1 = new File(".\\dest\\file1.txt");
		File d_file2 = new File(".\\dest\\file2.txt");

		if (!source.exists() || !dest.exists() || !s_file1.exists() || !s_file2.exists() || !d_file1.exists()
				|| !d_file2.exists()) {
			source.mkdir();
			dest.mkdir();
			try {
				s_file1.createNewFile();
				s_file2.createNewFile();
				d_file1.createNewFile();
				d_file2.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// source文件夹下file1、file2输入内容
		try {
			FileOutputStream out1 = new FileOutputStream(s_file1);
			FileOutputStream out2 = new FileOutputStream(s_file2);
			byte b[] = "跨年最好身边有一个天秤座".getBytes();
			byte b2[] = "20234,祝你天天开心,称心如意".getBytes();
			out1.write(b);
			out2.write(b2);
			out1.close();
			out2.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		FileInputStream in1 = null;
		FileInputStream in2 = null;
		FileOutputStream out1 = null;
		FileOutputStream out2 = null;
		byte b[] = null;
		try {
			in1 = new FileInputStream(s_file1);
			in2 = new FileInputStream(s_file2);
			out1 = new FileOutputStream(d_file1);
			out2 = new FileOutputStream(d_file2);
			b = new byte[1024];
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		while (true) {
			System.out.println("请输入操作命令");
            //鉴于输入的命令中存在空格 所以这里不用next()方法转而nextLine()方法
			String command = scanner.nextLine();
			String []name = source.list();
			if (command.equals("list")) {
				File s_file[] = source.listFiles();
				for (int i = 0; i < s_file.length; i++) {
					System.out.println(s_file[i].getName() + "\n");
				}
			} else if (command.equals("Copy file1.txt")) {
				try {
					in1.read(b);
					out1.write(b);
					out1.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("Copy file1.txt successfully");
			} else if (command.equals("Copy file2.txt")) {
				try {
					in2.read(b);
					out2.write(b);
					out2.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("Copy file1.txt successfully");
			} else if (command.equals("exit")) {
				System.exit(0);
			} else {
				System.out.println("Command couldn't be understand");
			}
		}

	}

}

实习三 对象序列化

题目描述

题解
//Student类
//实现序列化
public class Student implements Serializable{
	int stuID;
	String name;
	String address;
	public Student(int id,String name,String address) {
		// TODO Auto-generated constructor stub
        this.stuID = id;
        this.name = name;
        this.address = address;
	}
    //便于输出 重写方法
	public String toString() {
		return this.name+"学号是"+this.stuID+",籍贯是"+this.address;
	}

}
//Test测试类
public class Test {
	public static void main(String args[]) throws FileNotFoundException {
		Student a = new Student(20, "中国青年", "北京");
		Student b = new Student(8, "中国宝宝", "西安");
		File stu = new File("student.txt");
		if (!stu.exists()) {
			try {
				stu.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		//写进文件
		try {
			FileOutputStream out = new FileOutputStream(stu);
			ObjectOutputStream Oout = new ObjectOutputStream(out);
			Oout.writeObject(a);
			Oout.writeObject(b);
			Oout.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			FileInputStream in = null;
			in = new FileInputStream(stu);
			ObjectInputStream Iin = new ObjectInputStream(in);
		    try {
                //对象流读取后强制类型转换(Student)
				Student aa = (Student) Iin.readObject();
				Student bb = (Student) Iin.readObject();
				System.out.println(aa);
				System.out.println("\n"+bb);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值