《第九章 软件1302 蔡朝阳 15》由会员分享,可在线阅读,更多相关《第九章 软件1302 蔡朝阳 15(8页珍藏版)》请在金锄头文库上搜索。
1、第一题package 第九章作业;import java.io.File;import java.util.Scanner;public class Test1 public static void main(String args) Scanner r = new Scanner(System.in);while(true)String mulu = null;String str1 = null ;String str2 = null ;String str3 =.txt;String str4 = null ; System.out.println(请输入文件目录,例如: D:test)
2、;mulu = r.nextLine();System.out.println(请输入文件名,例如 test.txt);str1 = r.nextLine();if(str1.equals(quit)System.out.println(程序已退出!);break;File oldfile = new File (mulu+/+str1);System.out.println(改名前文件名为:);str4 = oldfile.getName();System.out.println(str4);str2 = oldfile.getName().replaceAll(.+$, )+str3;Fi
3、le newfile = new File(mulu+/+str2);oldfile.renameTo(newfile);System.out.println(改名后文件名为:);str4 = newfile.getName();System.out.println(str4);改名前文件格式:改名:改名后文件格式第二题import java.io.*;import java.util.Scanner;public class Test2 public static void main(String args) Scanner r = new Scanner(System.in);System
4、.out.println(输入文件名:);String str = r.nextLine() ;tryBufferedReader bufferReader = new BufferedReader(new InputStreamReader(System.in);BufferedWriter bufferWriter = new BufferedWriter(new FileWriter(str);String keyin = null ;System.out.println(请输入文本内容,输入 quit 结束);while(!(keyin = bufferReader.readLine(
5、).equals(quit)bufferWriter.write(keyin);bufferWriter.newLine();bufferWriter.flush();bufferReader.close();bufferWriter.close(); catch(IOException e )第三题package 第九章作业;import java.io.*;import java.util.*;class Word implements Serializable private static final long serialVersionUID = 1L;private String w
6、ord ; /单词private String meaning; /解释public String getWord()return word ;public String getMeaning()return meaning ;public Word (String word , String meaning)this.word = word ;this.meaning = meaning ;class WordCollection /字典操作类private Collectioncollection = null ; public WordCollection()load(); /加载字典p
7、ublic void add(Word word) / 增添单词collection.add(word);public Word find (String word)Iterator iter = collection.iterator();while(iter.hasNext()Word w = iter.next();if(word.equals(w.getWord()return w ;return null;public boolean del (String word) /删除单词 Iterator iter = collection.iterator();while(iter.ha
8、sNext()Word w = iter.next();if(word.equals(w.getWord()collection.remove(w);return true ;return false ;public CollectionwordCollection() /返回单词集合return collection ;public void load () /加载词典File dictionary = new File (E:Eclipse第九章作业dictionary.txt );if(dictionary.exists()tryObjectInputStream si = new Ob
9、jectInputStream (new FileInputStream(dictionary);this.collection = (Collection)si.readObject();catch(Exception e)e.printStackTrace();else collection = new HashSet();public void save() /保存词典tryObjectOutputStream so = new ObjectOutputStream (new FileOutputStream(E:Eclipse第九章作业dictionary.txt);so.writeO
10、bject(this.collection);so.close();catch(IOException e )e.printStackTrace();public class Test3 / 测试类static WordCollection words = new WordCollection();static Scanner r = new Scanner (System.in); public static void main(String args) int number = 0; while(true)System.out.println(0:退出 1:添加 2:查找 3:删除 4:浏
11、览);System.out.println(请选择一个数字: );trynumber = r.nextInt();r.nextLine(); /吸收回车符catch(InputMismatchException e)System.out.println(输入的不是数字!);r.nextLine(); /吸收回车符continue ;switch (number)case 0 : words.save(); return ;case 1 : add(); break ;case 2 : find(); break ; case 3 : del(); break ;case 4 : print()
12、; break;public static void add() /添加单词System.out.println(-输入要添加的单词:);String word = r.nextLine().trim();System.out.println(-请输入单词的解释:);String meaning = r.nextLine().trim();words.add(new Word(word,meaning);public static void find() /查找单词System.out.println(-输入要查找的单词:);String word = r.nextLine().trim();
13、Word w = words.find(word);if(w=null)System.out.println(无此单词!);elseSystem.out.println(解释是:+ w.getMeaning();public static void del () /删除单词System.out.println(-输入要删除的单词:);String word = r.nextLine().trim();if(words.del(word)System.out.println(删除成功!);elseSystem.out.println(无此单词!); public static void print () /浏览词典Collectioncollection = words.wordCollection();if(collection.isEmpty()System.out.println(没有单词!);return ;Iterator ite = collection.iterator();while(ite.hasNext()Word w = ite.next();System.out.println(单词:+ w.getWord(); System.out.println(解释:+ w.getMeaning();在路径对应文件夹下创建文本文档