当前位置:网站首页>实验九 输入输出流(节选)
实验九 输入输出流(节选)
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();
}
}
边栏推荐
- C语言入门指南
- [hand tearing code] single case mode and producer / consumer mode
- 9. Pointer (upper)
- [the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
- [the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
- [the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
- Custom RPC project - frequently asked questions and explanations (Registration Center)
- Miscellaneous talk on May 14
- Beautified table style
- 4.分支语句和循环语句
猜你喜欢
强化学习系列(一):基本原理和概念
Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems
[hand tearing code] single case mode and producer / consumer mode
ABA问题遇到过吗,详细说以下,如何避免ABA问题
2.C语言初阶练习题(2)
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
4. Binary search
. Net6: develop modern 3D industrial software based on WPF (2)
The latest tank battle 2022 - Notes on the whole development -2
C语言实现扫雷游戏(完整版)
随机推荐
撲克牌遊戲程序——人機對抗
Beautified table style
5.MSDN的下载和使用
string
1.C语言矩阵加减法
4. Branch statements and loop statements
View UI plus released version 1.3.0, adding space and $imagepreview components
Programme de jeu de cartes - confrontation homme - machine
[modern Chinese history] Chapter 9 test
[the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
Redis cache obsolescence strategy
1. C language matrix addition and subtraction method
5.函数递归练习
渗透测试学习与实战阶段分析
1. Preliminary exercises of C language (1)
【九阳神功】2016复旦大学应用统计真题+解析
简述xhr -xhr的基本使用
7-6 矩阵的局部极小值(PTA程序设计)
Reinforcement learning series (I): basic principles and concepts
Safe driving skills on ice and snow roads