当前位置:网站首页>实验九 输入输出流(节选)
实验九 输入输出流(节选)
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();
}
}
边栏推荐
- Miscellaneous talk on May 27
- Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
- Custom RPC project - frequently asked questions and explanations (Registration Center)
- Difference and understanding between detected and non detected anomalies
- vector
- Set container
- C语言实现扫雷游戏(完整版)
- 【九阳神功】2017复旦大学应用统计真题+解析
- 2. First knowledge of C language (2)
- 重载和重写的区别
猜你喜欢
2. First knowledge of C language (2)
1.C语言初阶练习题(1)
The difference between cookies and sessions
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
FAQs and answers to the imitation Niuke technology blog project (I)
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
9. Pointer (upper)
3. Number guessing game
Write a program to simulate the traffic lights in real life.
The latest tank battle 2022 full development notes-1
随机推荐
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
TypeScript快速入门
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
MySQL锁总结(全面简洁 + 图文详解)
Zatan 0516
Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems
[modern Chinese history] Chapter V test
Leetcode.3 无重复字符的最长子串——超过100%的解法
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
MATLAB打开.m文件乱码解决办法
Relationship between hashcode() and equals()
Using spacedesk to realize any device in the LAN as a computer expansion screen
C language Getting Started Guide
2.C语言初阶练习题(2)
【九阳神功】2020复旦大学应用统计真题+解析
Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
C语言入门指南
3. C language uses algebraic cofactor to calculate determinant