小学期——文件操作

题目——

 

代码——

package zy;

import java.io.*;
import java.util.Scanner;


public class main {
public static void main(String[] args) throws IOException {

Boolean inout1 = true;
while(inout1) {
Scanner scanner = new Scanner(System.in);
//功能选择
//1 按章节录入习题
//2 按章节查询习题
//3 按要求生成试卷
Tools.funorders(); //输出功能目录
String choice = scanner.next();
//处理功能需求
if("1".equals(choice) || "2".equals(choice)) {
//2 按章节查询习题
Tools.printorder(); //输出目录
Tools.choisess(choice); //区分功能1、2
} else if ("3".equals(choice)) {
//3 按要求生成试卷
Tools.choises3(choice); //执行功能3
} else {
break;
}
}

}
}


class Tools{
//前期功能区分
public static void funorders(){ //输出功能
System.out.println("");
System.out.println("-----------功能-----------");
System.out.println("1 按章节录入习题");
System.out.println("2 按章节查询习题");
System.out.println("3 按要求生成试卷");
System.out.println("任意键 退出系统");
System.out.println("请输入将要进行的功能:");
}

public static void printorder(){ //输出目录
System.out.println("-----------目录-----------");
System.out.println("1 绪论");
System.out.println("2 线性表");
System.out.println("3 栈和队列");
System.out.println("4 串、数组和广义表");
System.out.println("5 树和二叉树");
System.out.println("6 图");
System.out.println("7 查找");
System.out.println("8 排序");
}

public static void choisess(String cho) throws IOException { //输出需求,跳转
if ("1".equals(cho)) {
//1 按章节录入习题
System.out.println("请输入需要录入习题的章节:");
Todo.choises1();
} else if ("2".equals(cho)) {
//2 按章节查询习题
System.out.println("请输入需要查询习题的章节:");
Todo.choises2();
}
}

public static void choises3(String cho) throws IOException { //输出需求,跳转
//3 按要求生成试卷
System.out.println("请输入各章节需求");
int[] a = new int[9];
Todo.needs(a);
Todo.choises3(a);
}

}


class Todo{
//具体操作

public static void choises1() throws IOException {
//1 按章节录入习题
Scanner scanner = new Scanner(System.in);
String choice = scanner.next();

if("1".equals(choice)){
//1 绪论
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt1.txt";// 文件名
FileWriter fw = new FileWriter(file+"\\" + filename,true);
System.out.println("请输入你要记录的数据:");
choice = scanner.next();
choice += choice + "\r\n";
System.out.println("正在记录数据");
fw.write(choice);
fw.close();
}else{
System.out.println("创建目录------False");
}
} catch (Exception e) {
System.out.println("代码执行异常:"+e);
}
} else if ("2".equals(choice)) {
//2 线性表
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt2.txt";// 文件名
FileWriter fw = new FileWriter(file+"\\" + filename,true);
System.out.println("请输入你要记录的数据:");
choice = scanner.next();
choice += choice + "\r\n";
System.out.println("正在记录数据");
fw.write(choice);
fw.close();
}else{
System.out.println("创建目录------False");
}
} catch (Exception e) {
System.out.println("代码执行异常:"+e);
}
} else if ("3".equals(choice)) {
//3 栈和队列
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt3.txt";// 文件名
FileWriter fw = new FileWriter(file+"\\" + filename,true);
// FileWriter 如果文件名 的文件不存在,先创建再读写;存在的话直接追加写,关键字true表示追加
System.out.println("请输入你要记录的数据:");
choice = scanner.next();
choice += choice + "\r\n";
System.out.println("正在记录数据");
//写入内容
fw.write(choice);
// 关闭写文件
fw.close();
}else{
System.out.println("创建目录------False");
}
} catch (Exception e) {
System.out.println("代码执行异常:"+e);
}
} else if ("4".equals(choice)) {
//4 串、数组和广义表
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt4.txt";// 文件名
FileWriter fw = new FileWriter(file+"\\" + filename,true);
// FileWriter 如果文件名 的文件不存在,先创建再读写;存在的话直接追加写,关键字true表示追加
System.out.println("请输入你要记录的数据:");
choice = scanner.next();
choice += choice + "\r\n";
System.out.println("正在记录数据");
//写入内容
fw.write(choice);
// 关闭写文件
fw.close();
}else{
System.out.println("创建目录------False");
}
} catch (Exception e) {
System.out.println("代码执行异常:"+e);
}
} else if ("5".equals(choice)) {
//5 树和二叉树
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt5.txt";// 文件名
FileWriter fw = new FileWriter(file+"\\" + filename,true);
// FileWriter 如果文件名 的文件不存在,先创建再读写;存在的话直接追加写,关键字true表示追加
System.out.println("请输入你要记录的数据:");
choice = scanner.next();
choice += choice + "\r\n";
System.out.println("正在记录数据");
//写入内容
fw.write(choice);
// 关闭写文件
fw.close();
}else{
System.out.println("创建目录------False");
}
} catch (Exception e) {
System.out.println("代码执行异常:"+e);
}
} else if ("6".equals(choice)) {
//6 图
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt6.txt";// 文件名
FileWriter fw = new FileWriter(file+"\\" + filename,true);
// FileWriter 如果文件名 的文件不存在,先创建再读写;存在的话直接追加写,关键字true表示追加
System.out.println("请输入你要记录的数据:");
choice = scanner.next();
choice += choice + "\r\n";
System.out.println("正在记录数据");
//写入内容
fw.write(choice);
// 关闭写文件
fw.close();
}else{
System.out.println("创建目录------False");
}
} catch (Exception e) {
System.out.println("代码执行异常:"+e);
}
} else if ("7".equals(choice)) {
//7 查找
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt7.txt";// 文件名
FileWriter fw = new FileWriter(file+"\\" + filename,true);
// FileWriter 如果文件名 的文件不存在,先创建再读写;存在的话直接追加写,关键字true表示追加
System.out.println("请输入你要记录的数据:");
choice = scanner.next();
choice += choice + "\r\n";
System.out.println("正在记录数据");
//写入内容
fw.write(choice);
// 关闭写文件
fw.close();
}else{
System.out.println("创建目录------False");
}
} catch (Exception e) {
System.out.println("代码执行异常:"+e);
}
} else if ("8".equals(choice)) {
//8 排序
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt8.txt";// 文件名
FileWriter fw = new FileWriter(file+"\\" + filename,true);
// FileWriter 如果文件名 的文件不存在,先创建再读写;存在的话直接追加写,关键字true表示追加
System.out.println("请输入你要记录的数据:");
choice = scanner.next();
choice += choice + "\r\n";
System.out.println("正在记录数据");
//写入内容
fw.write(choice);
// 关闭写文件
fw.close();
}else{
System.out.println("创建目录------False");
}
} catch (Exception e) {
System.out.println("代码执行异常:"+e);
}
}
}

public static void choises2() {
//2 按章节查询习题
Scanner scanner = new Scanner(System.in);
String choice = scanner.next();

if("1".equals(choice)){
//1 绪论
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt1.txt";// 文件名
// 按行读取
BufferedReader reader = new BufferedReader(new FileReader(file+"/"+filename));
while ((choice = reader.readLine()) != null) {
System.out.println(choice);
}
reader.close();
System.out.println("");
}
} catch (Exception e) {
System.out.println("文件不存在");
}
} else if ("2".equals(choice)) {
//2 线性表
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt2.txt";// 文件名
// 按行读取
BufferedReader reader = new BufferedReader(new FileReader(file+"/"+filename));
while ((choice = reader.readLine()) != null) {
System.out.println(choice);
}
reader.close();
System.out.println("");
}
} catch (Exception e) {
System.out.println("文件不存在");
}
} else if ("3".equals(choice)) {
//3 栈和队列
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt3.txt";// 文件名
// 按行读取
BufferedReader reader = new BufferedReader(new FileReader(file+"/"+filename));
while ((choice = reader.readLine()) != null) {
System.out.println(choice);
}
reader.close();
System.out.println("");
}
} catch (Exception e) {
System.out.println("文件不存在");
}
} else if ("4".equals(choice)) {
//4 串、数组和广义表
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt4.txt";// 文件名
// 按行读取
BufferedReader reader = new BufferedReader(new FileReader(file+"/"+filename));
while ((choice = reader.readLine()) != null) {
System.out.println(choice);
}
reader.close();
System.out.println("");
}
} catch (Exception e) {
System.out.println("文件不存在");
}
} else if ("5".equals(choice)) {
//5 树和二叉树
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt5.txt";// 文件名
// 按行读取
BufferedReader reader = new BufferedReader(new FileReader(file+"/"+filename));
while ((choice = reader.readLine()) != null) {
System.out.println(choice);
}
reader.close();
System.out.println("");
}
} catch (Exception e) {
System.out.println("文件不存在");
}
} else if ("6".equals(choice)) {
//6 图
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt6.txt";// 文件名
// 按行读取
BufferedReader reader = new BufferedReader(new FileReader(file+"/"+filename));
while ((choice = reader.readLine()) != null) {
System.out.println(choice);
}
reader.close();
System.out.println("");
}
} catch (Exception e) {
System.out.println("文件不存在");
}
} else if ("7".equals(choice)) {
//7 查找
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt7.txt";// 文件名
// 按行读取
BufferedReader reader = new BufferedReader(new FileReader(file+"/"+filename));
while ((choice = reader.readLine()) != null) {
System.out.println(choice);
}
reader.close();
System.out.println("");
}
} catch (Exception e) {
System.out.println("文件不存在");
}
} else if ("8".equals(choice)) {
//8 排序
try {
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
String filename = "tt8.txt";// 文件名
// 按行读取
BufferedReader reader = new BufferedReader(new FileReader(file + "/" + filename));
while ((choice = reader.readLine()) != null) {
System.out.println(choice);
}
reader.close();
System.out.println("");
}
} catch (Exception e) {
System.out.println("文件不存在");
}
}
}

public static void needs(int[] a) {
Scanner scanner = new Scanner(System.in);

System.out.print("1 绪论 需要的习题数量为:");
a[1] = scanner.nextInt();
System.out.print("2 线性表 需要的习题数量为:");
a[2] = scanner.nextInt();
System.out.print("3 栈和队列 需要的习题数量为:");
a[3] = scanner.nextInt();
System.out.print("4 串、数组和广义表 需要的习题数量为:");
a[4] = scanner.nextInt();
System.out.print("5 树和二叉树 需要的习题数量为:");
a[5] = scanner.nextInt();
System.out.print("6 图 需要的习题数量为:");
a[6] = scanner.nextInt();
System.out.print("7 查找 需要的习题数量为:");
a[7] = scanner.nextInt();
System.out.print("8 排序 需要的习题数量为:");
a[8] = scanner.nextInt();

}

public static void choises3(int[] a) {
//3 按要求生成试卷
String choice = "";
for (int i = 1; i < 9; i++) {
if ( a[i] != 0) {
//1 绪论
try {
//读取习题
File file = new File("D:/IDEA/progrem/xxq");
file.mkdirs();
if (file.getParentFile().exists()) {
//试卷
String needname = "text_need.txt";// 文件名
FileWriter fw = new FileWriter(file + "\\" + needname, true);
//题目
String filename = "tt" + i + ".txt";// 文件名
// 读取题目写入试卷
BufferedReader reader = new BufferedReader(new FileReader(file + "/" + filename));
while ((choice = reader.readLine()) != null && a[i] != 0) {
choice += "\r\n";
fw.write(choice);
a[i]--;
}
reader.close();
fw.close();
}
System.out.println("章节" + i + "题目录入完成");
} catch (Exception e) {
System.out.println("操作失败");
}
}

}
}
}

 

posted @   椰子灰  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示