• java——巨简陋文本编辑器


    String :equals()方法是进行内容比较,而不是引用比较。

        “==”比较两个变量本身的值,即两个对象在内存中的首地址。

    Scanner :用Scanner实现字符串的输入有两种方法,一种是next(),一种nextLine()。

      next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符。

      nextLine()方法的结束符只是Enter键。

    String[]list<String>的区别:

      String[]:容量是固定的,只能一次获取或设置一个元素的值;可以具有多个维度。

      List<String>:容量可根据需要自动扩充、修改、删除或插入数据;只能有一个维度。

      String[]数组里面是存放String型的,List<String>是存放String类型的对象。特定类型(Object 除外)的数组的性能优于List的性能。 这是因为 List的元素属于 Object 类型;所以在存储或检索值类型时通常发生装箱和取消装箱操作。不过,在不需要重新分配时(即最初的容量十分接近列表的最大容量),List< T> 的性能与同类型的数组十分相近。

    import java.util.*;
    import java.io.*;
    public class Txt{
        private static String message = "";
        //filePath=""会判断filePath非空!
        private static String filePath;
        public static void main(String[] args)throws Exception{
            
            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("~~~~~~~~~~~~~~~~~~");
            //主循环
            while(true){
           Scanner scan = new Scanner(System.in);  System.out.println(
    "请输入操作指令:"); try{ int command = scan.nextInt(); switch(command){ case 1: createFile(); break; case 2: openFile(); break; case 3: editFile(); break; case 4: saveFile(); break; case 5: exit(); break; default: System.out.println("指令有误,请重新输入:"); break; } }catch(InputMismatchException e){ System.out.println("请输入一个数字哦~"); } } } private static void createFile(){ StringBuffer stb = new StringBuffer(); Scanner scan = new Scanner(System.in); System.out.println("请输入内容,停止编写请输入“stop”:"); String inputMessage = ""; // 这里不能用== // String :equals()方法是进行内容比较,而不是引用比较。 //“==”比较两个变量本身的值,即两个对象在内存中的首地址。 while(!(inputMessage.equals("stop"))){ stb.append(inputMessage); inputMessage = scan.nextLine(); } //将内容暂存在message里 message = stb.toString(); System.out.println(message); } private static void openFile() throws Exception{ Scanner scan = new Scanner(System.in); System.out.println("请输入要打开的文件名:"); filePath = scan.nextLine(); if((filePath != null) && !(filePath.endsWith(".txt"))){ System.out.println("请输入文本文件!"); return; } FileReader fr = new FileReader(filePath); StringBuffer sb = new StringBuffer(); int in; char[] charArray = new char[1024]; //此-1非彼-1,这个-1是字节转换成int后得到-1则跳出while,而文本的-1是int转换成字节再转换成int while((in = fr.read(charArray)) != -1){ sb.append(charArray); } //将打开文件的内容暂存在message里 message = sb.toString(); System.out.println("打开文件内容:" + " " + message); fr.close(); } private static void saveFile() throws Exception{ FileWriter fw = null; if(filePath != null){ fw = new FileWriter(filePath); }else{ Scanner scan = new Scanner(System.in); System.out.println("请输入要保存的文件名:"); filePath = scan.nextLine(); //将大写换成小写 if(!filePath.toLowerCase().endsWith(".txt")){ filePath += ".txt"; } fw = new FileWriter(filePath); } System.out.println(message); fw.write(message); fw.close(); message = ""; filePath = null; } private static void editFile(){ Scanner scan = new Scanner(System.in); if(filePath == null && message == ""){ System.out.println("请先新建文件或打开文件"); return; } String str = ""; System.out.println("请输入要修改的内容(以"修改的目标文字:修改后的文字"格式),停止修改输入stop:"); while(!str.equals("stop")){ str = scan.nextLine(); if(str != null && str.length() >0){ String[] editMessage = str.split(":"); if(editMessage != null && editMessage.length >1){ message = message.replace(editMessage[0], editMessage[1]); } } } } private static void exit(){ System.out.println("退出喽~"); System.exit(0); } }
  • 相关阅读:
    [转载]Delphi 2009 (Tiburon) 新特性之 Exit 函数的新用法
    [转载][转]Delphi 2009 泛型+闭包能带来什么?
    [转载]现有 Delphi 项目迁移到 Tiburon 中的注意事项 (中)
    [转载]现有 Delphi 项目迁移到 Tiburon 中的注意事项 (上)
    [转载]现有 Delphi 项目迁移到 Tiburon 中的注意事项 (下)
    [转载]Delphi 2009 (Tiburon) 新特性之 Unicode 支持!
    [转载]Tiburon 支持 Unicode 的 LoadFromFile, SaveToFile
    GdiPlus[37]: IGPGraphicsPath (四) 路径变换
    GdiPlus[36]: IGPGraphicsPath (三) 路径中的数据
    GdiPlus[43]: IGPGraphics (二) 关于文本绘制
  • 原文地址:https://www.cnblogs.com/gaoquanquan/p/9640810.html
Copyright © 2020-2023  润新知