首页 > 试题广场 >

编写仿认Windows记事本的小程序。

[问答题]
编写仿认Windows记事本的小程序。
package IOPractice; 

import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.Scanner;  
/** * @program: java_project 
* @description: 字符流实现记事本 
* @author: liuliang 
* @create: 2019-04-21 10:52  
**/ 
public class Practice03 { 
    private static final int BUFFER_SIZE = 1024;   
        /** 
         * @param args  
        */  public static void main(String[] args) {
        FileWriter fw = null;   
        StringBuilder sb = new StringBuilder();   
        String str = null;  
        Scanner in = new Scanner(System.in);  
        in.useDelimiter("\n");//设置分隔符----一行以此作为分隔符  
        
        System.out.println("请输入,按Enter键换行输入:wq结束输入保存");  
        while(in.hasNext()){//判断是否继续有输入  
            str = in.nextLine(); //输入行可以包含空格、空行、且首行也可以输入是空行  
            if(str.equals(":wq")) { 
                break; 
            }
            sb.append(str);  
            str = null;  
        }
        in.close();  
        System.out.println("输入结束,正在保存...");  
        try {
            fw = new FileWriter("E:\\test.txt"); 
            char buf[] = sb.toString().toCharArray();  
            fw.write(buf, 0, buf.length);  
            System.out.println("保存成功");  
        } catch (Exception e) { 
            // TODO: handle exception  
            e.printStackTrace();  
        } finally { 
            if (fw != null) { 
                try {
                    fw.close();  
                } catch (IOException e) {
                    System.out.println("保存失败");  
                }
            }
        }
    }
}

发表于 2019-04-21 12:19:33 回复(0)
链接:https://www.nowcoder.com/questionTerminal/5b9308ff14fd480cadeaf42d93777e9c?orderByHotValue=1&mutiTagIds=570&page=1&onlyReference=false
来源:牛客网
来源:牛客网
packageIOPractice;
 
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.Scanner; 
/** * @program: java_project
* @description: 字符流实现记事本
* @author: liuliang
* @create: 2019-04-21 10:52 
**/
publicclassPractice03 {
    privatestaticfinalintBUFFER_SIZE =1024;  
        /**
         * @param args 
        */ publicstaticvoidmain(String[] args) {
        FileWriter fw =null;  
        StringBuilder sb =newStringBuilder();  
        String str =null; 
        Scanner in =newScanner(System.in); 
        in.useDelimiter("\n");//设置分隔符----一行以此作为分隔符 
         
        System.out.println("请输入,按Enter键换行输入:wq结束输入保存"); 
        while(in.hasNext()){//判断是否继续有输入 
            str = in.nextLine();//输入行可以包含空格、空行、且首行也可以输入是空行 
            if(str.equals(":wq")) {
                break;
            }
            sb.append(str); 
            str =null; 
        }
        in.close(); 
        System.out.println("输入结束,正在保存..."); 
        try{
            fw =newFileWriter("E:\\test.txt");
            charbuf[] = sb.toString().toCharArray(); 
            fw.write(buf,0, buf.length); 
            System.out.println("保存成功"); 
        }catch(Exception e) {
            // TODO: handle exception 
            e.printStackTrace(); 
        }finally{
            if(fw !=null) {
                try{
                    fw.close(); 
                }catch(IOException e) {
                    System.out.println("保存失败"); 
                }
            }
        }
    }
}
发表于 2019-04-25 23:39:02 回复(0)
链接:https://www.nowcoder.com/questionTerminal/5b9308ff14fd480cadeaf42d93777e9c?orderByHotValue=1&mutiTagIds=570&page=1&onlyReference=false
来源:牛客网
packageIOPractice;
 
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.Scanner; 
/** * @program: java_project
* @description: 字符流实现记事本
* @author: liuliang
* @create: 2019-04-21 10:52 
**/
publicclassPractice03 {
    privatestaticfinalintBUFFER_SIZE =1024;  
        /**
         * @param args 
        */ publicstaticvoidmain(String[] args) {
        FileWriter fw =null;  
        StringBuilder sb =newStringBuilder();  
        String str =null; 
        Scanner in =newScanner(System.in); 
        in.useDelimiter("\n");//设置分隔符----一行以此作为分隔符 
         
        System.out.println("请输入,按Enter键换行输入:wq结束输入保存"); 
        while(in.hasNext()){//判断是否继续有输入 
            str = in.nextLine();//输入行可以包含空格、空行、且首行也可以输入是空行 
            if(str.equals(":wq")) {
                break;
            }
            sb.append(str); 
            str =null; 
        }
        in.close(); 
        System.out.println("输入结束,正在保存..."); 
        try{
            fw =newFileWriter("E:\\test.txt");
            charbuf[] = sb.toString().toCharArray(); 
            fw.write(buf,0, buf.length); 
            System.out.println("保存成功"); 
        }catch(Exception e) {
            // TODO: handle exception 
            e.printStackTrace(); 
        }finally{
            if(fw !=null) {
                try{
                    fw.close(); 
                }catch(IOException e) {
                    System.out.println("保存失败"); 
                }
            }
        }
    }
}
发表于 2019-04-25 22:34:46 回复(0)