实验目的:
理解文件和流的概念、Java 流的层次结构,掌握 Java 提供的各种字节流类和字符流类的功能和使用方法。
实验内容:
1、编写一个 Java 应用程序,将已存在的扩展名为.txt 的文本文件加密后存入另一个文本文件中。按模板要求,将【代码 1】~【代码 7】替换为 Java 程序代码。
(1)源代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50package experiment; import java.io.*; public class SecretExample { public static void main(String a[]) { File fileone=new File("E:\eplicse-workspace\experiment\src\experiment\hello.txt"); File filetwo=new File("E:\eplicse-workspace\experiment\src\experiment\hello.secret"); char b[]=new char[100]; try { FileReader in=new FileReader(fileone);//【代码1】创建指向fileone的字符输入流 FileWriter out=new FileWriter(filetwo);//【代码2】创建指向filetwo的字符输出流 int n=-1; while((n=in.read(b))!=-1) { for(int i=0;i<n;i++) { b[i]=(char)(b[i]^'a'); } out.write(b,0,n);//【代码3】out 将数组 b 的前 n 单元写到文件 } out.close();//【代码 4】out 关闭 in=new FileReader(filetwo);//【代码5】创建指向filetwo的字符输入流 System.out.println("加密后的文件内容:"); n=in.read(b); while(n!=-1) { String str=new String(b,0,n); System.out.println("dfjakfjd"+str); n=in.read(b); } in=new FileReader(filetwo); System.out.println("解密后的文件内容:"); while((n=in.read(b))!=-1) { for(int i=0;i<n;i++) { b[i]=(char)(b[i]^'a'); } String str=new String(b,0,n); System.out.println(str); } in.close();//【代码 6】in 关闭 } catch(IOException e) { System.out.println(e); } } }
2、编程完成下列功能:
1)首先建立两个文件 myfiel.txt 和 myfile2.txt。
2)从标准设备中输入多名学生信息,如学号,姓名,专业,班级,家庭住址等,待输入"bye"时结束,将输入内容保存在 myfile1.txt 文件中。
3)再将 myfile1.txt 文件中内容拷贝到 myfile2.txt。
(1)源代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126package experiment; import java.io.*; import java.util.*; class Student1 implements Serializable { private String sno; private String name; private String dep; private String class1; private String address; Student1(String s,String na,String d,String c,String a) { sno=s;name=na;dep=d;class1=c;address=a; } public String toString() { return "学号:"+sno+"tt姓名:"+name+"tt专业:"+dep+"tt班级:"+class1+"tt地址:"+address; } } public class Main62 { public static void main(String a[]) { //写入myfile1.txt String sno,name,dep,class1,address; Student1 stu[]=new Student1[999]; int i=0; Scanner scan=new Scanner(System.in); FileOutputStream fout; ObjectOutputStream objout; System.out.println("请输入学生的学号、姓名、专业、班级和地址,输入bye结束程序"); try { fout=new FileOutputStream("E:\myfile1.txt"); objout=new ObjectOutputStream(fout); while(true) { sno=scan.next(); if(sno.equals("bye"))break; name=scan.next(); dep=scan.next(); class1=scan.next(); address=scan.next(); stu[i]=new Student1(sno,name,dep,class1,address); objout.writeObject(stu[i]); i++; } objout.close(); fout.close(); } catch(Exception e) { System.out.println(e); System.exit(0); } //输出myfile1.txt内容 try{ FileInputStream fin=new FileInputStream("E:\myfile1.txt"); ObjectInputStream objin=new ObjectInputStream(fin); System.out.println("ttt学生信息表myfile1"); while(true) { try{ System.out.println( objin.readObject()); } catch(EOFException e) { break; } } objin.close(); fin.close(); } catch(Exception e ) { System.out.println(e); } //将myfile1.txt的内容复制到myfile2.txt try{ FileOutputStream fout1=new FileOutputStream("E:\myfile2.txt"); ObjectOutputStream objout1=new ObjectOutputStream(fout1); FileInputStream fin2=new FileInputStream("E:\myfile1.txt"); ObjectInputStream objin2=new ObjectInputStream(fin2); while(true) { try{ objout1.writeObject(objin2.readObject()); } catch(EOFException e) { break; } } objin2.close(); fin2.close(); objout1.close(); fout1.close(); } catch(Exception e ) { System.out.println(e); } //输出myfile2.txt内容 try{ FileInputStream fin=new FileInputStream("E:\myfile2.txt"); ObjectInputStream objin=new ObjectInputStream(fin); System.out.println("nttt学生信息表myfile2"); while(true) { try{ System.out.println( objin.readObject()); } catch(EOFException e) { break; } } objin.close(); fin.close(); } catch(Exception e ) { System.out.println(e); } } }
最后
以上就是粗犷草莓最近收集整理的关于JAVA实验六 输入输出流的全部内容,更多相关JAVA实验六内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复