当前位置:网站首页>实验九 输入输出流(节选)
实验九 输入输出流(节选)
2022-07-06 09:22:00 【文文喜欢郭子吖】
实验九 输入输出流
实验目的
1.理解Java技术体系中“流”的概念。
2.掌握System.in和System.out的基本使用方法
3.掌握字节流和字符流的常用类及使用方法。
4.掌握Java程序对文件的基本操作。
实验学时 4学时
实验内容
1.编写程序使用System.in对象的read()方法读取键盘输入,当程序运行时,首先询问需要输入的数据个数,然后依次输入指定个数的整数,输入完毕后,输出所有输入整数的和。
package code91;
import java.io.BufferedReader; //带缓冲功能的字符输入流
import java.io.IOException;
import java.io.InputStreamReader; //字节流向字符流转化的流
public class code91 {
public static void main(String[] args)throws NumberFormatException,Exception {
// TODO 自动生成的方法存根
int a[]=new int[512]; //创建一个接收键盘输入数据的输入流
int sum=0;
InputStreamReader c = new InputStreamReader(System.in); //输入流放入缓冲流
BufferedReader d = new BufferedReader(c);
System.out.println("请输入数的总数:");
int n=Integer.parseInt( d.readLine()); //转换为基本数据类型int
System.out.println("这些数分别是:");
for(int i=0;i<n;i++) {
int b=Integer.parseInt(d.readLine());
a[i]=b;
sum+=a[i];
}
System.out.println("所有输入的数和为:"+sum);
}
}
2.在电脑D盘创建一个名为FileList的文本文件,将D盘下所有文件及文件夹名称保存到这个文件中。
package code92;
import java.io.File;
import java.io.FileWriter;
public class code92 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
try {
File f=new File("D:\\FileList");
if(!f.exists()){
f.createNewFile(); //创建一个文本文件
}
else {
System.out.println("文件已存在");
}
File file=new File("D:\\");
File[] fs=file.listFiles();
FileWriter out = new FileWriter(f);
for(File fs1 : fs) {
out.write(fs1.getPath()+'\r'+'\n');
}
out.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
5.对于重要文件通常需要进行加密处理,请编写程序实现对文本文件的加密,密码表如下,明文如果是大写字母则密文也为密码表对应大写字母,明文如果是小写字母则密文也为密码表对应小写字母。
明文 | A | B | C | D | E | F | G | H | I | J | K | L | M |
密文 | T | O | I | A | N | D | E | G | H | Z | B | K | F |
明文 | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
密文 | J | M | C | L | P | Y | V | X | Q | R | W | U | S |
package code95;
import java.io.File;
import java.io.FileReader; //文件字符输入流
import java.io.FileWriter; //文件字符输出流
import java.util.Scanner;
public class code95 {
public static void main(String[] args) throws Exception {
char reality[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
char fake[] = { 'T', 'O', 'I', 'A', 'N', 'D', 'E', 'G', 'H', 'Z', 'B', 'K', 'F', 'J', 'M', 'C', 'L', 'P', 'Y',
'V', 'X', 'Q', 'R', 'W', 'U', 'S' }; //创建两个数组,分别填入字母以及加密后的密文
FileWriter fw = new FileWriter(new File("D:\\FileList"));
FileReader fr = new FileReader(new File("D:\\FileList")); //对实验2中创建的文件进行加密
Scanner in = new Scanner(System.in);
String str = in.nextLine();
char[] a = str.toCharArray(); //将字符串转换为字符数组
for (int n = 0; n < a.length; n++) {
for (int i = 0; i < 26; i++) {
if (a[n] == reality[i]) { //加密字母
a[n] = fake[i];
break;
} else if (a[n] <= 'z' && a[n] >= 'a') { //小写字母转换为大写字母
if (reality[i] == (char) (a[n] - 32)) { //强制类型转换
a[n] = (char) (fake[i] + 32); //将大写密文转换成小写
break;
}
}
}
}
fw.write(a);
in.close();
fw.close();
fr.close();
}
}
边栏推荐
猜你喜欢
Write a program to simulate the traffic lights in real life.
【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
6.函数的递归
The latest tank battle 2022 full development notes-1
[hand tearing code] single case mode and producer / consumer mode
4. Branch statements and loop statements
7. Relationship between array, pointer and array
6. Function recursion
一段用蜂鸣器编的音乐(成都)
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
随机推荐
受检异常和非受检异常的区别和理解
[the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
5. Function recursion exercise
5月27日杂谈
【九阳神功】2022复旦大学应用统计真题+解析
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
【九阳神功】2021复旦大学应用统计真题+解析
Aurora system model of learning database
【毕业季·进击的技术er】再见了,我的学生时代
3.猜数字游戏
C语言实现扫雷游戏(完整版)
PriorityQueue (large root heap / small root heap /topk problem)
5.函数递归练习
Safe driving skills on ice and snow roads
. Net6: develop modern 3D industrial software based on WPF (2)
Differences among fianl, finally, and finalize
C语言入门指南