一、实验目的
1.了解流式输入输出的基本概念和基本原理。
2. 掌握InputStream、OutputStream抽象类的基本使用方法;
3. 掌握FileInputStream、FileOutputStream抽象类的基本使用方法。
4.掌握理解类File、FileReader、FileWriter的使用方法。
二、上机内容
1.编写程序将从键盘上输入的一行内容写入到文件copyfile.txt中,文件的保存位置自己确定。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
//编写程序将从键盘上输入的一行内容写入到文件copyfile.txt中,文件的保存位置自己确定。
public class Test01 {public static void main(String[] args) {Scanner scanner =new Scanner(System.in);String string = scanner.next();//将字符串转化成byte数组byte[] bytes = string.getBytes();//创建输出流FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream("C:\\Users\\JavaSE\\javase作业\\src\\javase\\上机7\\copyfile");//开始写fileOutputStream.write(bytes);//输出流最后要刷新fileOutputStream.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {//关闭流try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}
}
2.选择一个含有中文注释的.java文件,使用FileInputStream和FileReader分别读取文件,并把内容显示在控制台上。然后使用FileOutputStream和FileWriter将文件分别将文件复制到另一个文件copyfile1.txt和 copyfile2.txt中,文件的保存位置自己确定。
import java.io.*;
//2.选择一个含有中文注释的.java文件,使用FileInputStream和FileReader分别读取文件,并把内容显示在控制台上。
// 然后使用FileOutputStream和FileWriter将文件分别将文件复制到另一个文件copyfile1.txt和 copyfile2.txt中,文件的保存位置自己确定。
public class Test02 {public static void main(String[] args) {//先创建流FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;try {fileInputStream = new FileInputStream("C:\\Users\\JavaSE\\javase作业\\src\\javase\\上机7\\Student.java");fileOutputStream = new FileOutputStream("C:\\Users\\JavaSE\\javase作业\\src\\javase\\上机7\\copyfile1");//边读边拷贝//准备byte数组byte[] bytes = new byte[4];int readCount = 0;while((readCount = fileInputStream.read(bytes)) != -1){System.out.print(new String(bytes,0,readCount));fileOutputStream.write(bytes);}//最后要记得刷新输出流fileOutputStream.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fileInputStream != null){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}if (fileOutputStream != null){try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
//2.选择一个含有中文注释的.java文件,使用FileInputStream和FileReader分别读取文件,并把内容显示在控制台上。
// 然后使用FileOutputStream和FileWriter将文件分别将文件复制到另一个文件copyfile1.txt和 copyfile2.txt中,文件的保存位置自己确定。
public class Test03 {public static void main(String[] args) {//先创建流FileReader fileReader = null;FileWriter fileWriter = null;try {fileReader = new FileReader("C:\\Users\\JavaSE\\javase作业\\src\\javase\\上机7\\Student.java");fileWriter = new FileWriter("C:\\Users\\JavaSE\\javase作业\\src\\javase\\上机7\\copyfile2");//准备char数组char[] chars = new char[4];int readCount = 0;//边读边写while((readCount = fileReader.read(chars)) != -1){System.out.print(new String(chars,0,readCount));fileWriter.write(chars);}//刷新fileWriter.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {if(fileReader != null){try {fileReader.close();} catch (IOException e) {e.printStackTrace();}}if (fileWriter != null){try {fileWriter.close();} catch (IOException e) {e.printStackTrace();}}}}
}
3(*).利用上机6中第3题 的Student类,创建10个相应的对象,写入到文件student2.dat中;再从文件student2.dat中读出copy到文件student3.dat中。
//利用上机6中第3题 的Student类,创建10个相应的对象,写入到文件student2.dat中;
// 再从文件student2.dat中读出copy到文件student3.dat中。import caopeng.javase.上机6.Student;
import caopeng.javase.上机6.StudentException;import java.io.*;
import java.util.ArrayList;
import java.util.List;public class Test04 {public static void main(String[] args) {Student student1 = null;Student student2 = null;Student student3 = null;Student student4 = null;Student student5 = null;Student student6 = null;Student student7 = null;Student student8 = null;Student student9 = null;Student student10 = null;//创建十个对象try {student1 = new Student(116,"钢铁侠",30,98);student2 = new Student(120,"蜘蛛侠",20,80);student3 = new Student(111,"美国队长",60,95);student4 = new Student(112,"雷神",999,85);student5 = new Student(115,"绿巨人",20,60);student6 = new Student(114,"黑寡妇",20,90);student7 = new Student(117,"鹰眼",20,85);student8 = new Student(113,"绯红女巫",20,85);student9 = new Student(119,"星爵",20,59);student10 = new Student(118,"奇异博士",20,100);} catch (StudentException e) {e.printStackTrace();}//一次序列化多个对象,需要集合List<Student> arrayList = new ArrayList<>();arrayList.add(student1);arrayList.add(student2);arrayList.add(student3);arrayList.add(student4);arrayList.add(student5);arrayList.add(student6);arrayList.add(student7);arrayList.add(student8);arrayList.add(student9);arrayList.add(student10);//创建流ObjectOutputStream objectOutputStream = null;ObjectInputStream objectInputStream = null;try {objectOutputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\JavaSE\\javase作业\\src\\javase\\上机7\\student2.dat"));objectOutputStream.writeObject(arrayList);//刷新流objectOutputStream.flush();objectInputStream = new ObjectInputStream(new FileInputStream("C:\\Users\\JavaSE\\javase作业\\src\\javase\\上机7\\student2.dat"));//从dat文件中输出对象List<Student> list = (List<Student>) objectInputStream.readObject();for (Object object : list){System.out.println(object);}//从读出来的copy到student3.dat中objectOutputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\JavaSE\\javase作业\\src\\javase\\上机7\\student3.dat"));objectOutputStream.writeObject(list);} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {if (objectOutputStream != null){try {objectOutputStream.close();} catch (IOException e) {e.printStackTrace();}}if (objectInputStream != null){try {objectInputStream.close();} catch (IOException e) {}}}}
}