当前位置:网站首页>实验九 输入输出流(节选)
实验九 输入输出流(节选)
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();
}
}边栏推荐
- 撲克牌遊戲程序——人機對抗
- 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
- 【九阳神功】2016复旦大学应用统计真题+解析
- 1.C语言矩阵加减法
- TypeScript快速入门
- 5月14日杂谈
- 2. C language matrix multiplication
- MySQL中count(*)的实现方式
- Service ability of Hongmeng harmonyos learning notes to realize cross end communication
- MySQL lock summary (comprehensive and concise + graphic explanation)
猜你喜欢

Relationship between hashcode() and equals()

仿牛客技术博客项目常见问题及解答(三)
![[面試時]——我如何講清楚TCP實現可靠傳輸的機制](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[面試時]——我如何講清楚TCP實現可靠傳輸的機制
![[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP

2. Preliminary exercises of C language (2)

1.C语言矩阵加减法

QT meta object qmetaobject indexofslot and other functions to obtain class methods attention

深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning

优先队列PriorityQueue (大根堆/小根堆/TopK问题)

2022 Teddy cup data mining challenge question C idea and post game summary
随机推荐
Write a program to simulate the traffic lights in real life.
稻 城 亚 丁
【九阳神功】2016复旦大学应用统计真题+解析
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
Detailed explanation of redis' distributed lock principle
Mortal immortal cultivation pointer-2
简单理解ES6的Promise
4.分支语句和循环语句
Redis实现分布式锁原理详解
【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
Implementation of count (*) in MySQL
[面试时]——我如何讲清楚TCP实现可靠传输的机制
自定义RPC项目——常见问题及详解(注册中心)
2. C language matrix multiplication
受检异常和非受检异常的区别和理解
2. First knowledge of C language (2)
View UI plus releases version 1.1.0, supports SSR, supports nuxt, and adds TS declaration files
深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning
更改VS主题及设置背景图片
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis