提示用户开始输入,并将此后的键盘输入内容保存至文件userinput.txt中,直到用户在键盘上输入^z(control键+z键)结束输入,显示userinput.txt全部内容介绍程序运行。关键是^z结束输出怎么实现???

解决方案 »

  1.   

    是指这种?
    ctrl+z在命令行就是结束输入流的意思,但是必须换行单独使用
    import java.util.Scanner;/*
     * 注意:必须先回车再ctrl+z结束输入,否则当前行不会保存
     */
    public class Test {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    StringBuffer sb = new StringBuffer();
    while(in.hasNextLine()){
    String str = in.nextLine();
    sb.append("\n");
    sb.append(str);

    }
    System.out.println(sb.toString());
    //把sb写入文件....
    }
    }
      

  2.   


    import java.io.*;
    import java.util.*;public class A { public static void main(String[] args) { System.out.println("请输入:");
    try {
    Scanner sc = new Scanner(System.in); File file = new File("E:/userinput.txt"); FileWriter fout = new FileWriter(file); String str; while (sc.hasNext()) {
    str = sc.nextLine();
    str = str + "\r\n";
    fout.write(str);
    }
    fout.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
      

  3.   

    Scanner 貌似无法扫描一些特殊的组合键,如果要求不是很严格的话可以考虑以下代码:import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;public class ReadAndWrite {
    private InputStream is ;
    private OutputStream os;

    private void init(File inputFile,File outputFile) throws FileNotFoundException{
    if(inputFile==null)
    this.is = System.in;
    else
    this.is = new FileInputStream(inputFile);
    if(outputFile==null)
    this.os = System.out;
    else
    this.os = new FileOutputStream(outputFile);
    } private void readAndWrite() throws IOException{
    do{
    int c = this.is.read();
    //this way is only used temporary, it can be used to break the 
    //process when the user pressing "Ctrl^Z";
    if(c == -1)break;
    this.os.write(c);
    }while(true);
    }

    private void print() throws IOException{
    int c = -1;
    do{
    c = this.is.read();
    this.os.write(c);
    }while(c > 0);

    }
    private void close(InputStream is,OutputStream os){
    if(os!=null){
    try {
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if(is!=null){
    try {
    is.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    static String fileSep = System.getProperty("file.separator");
    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
    String fileName = "useInput.txt";
    File f = new File("E:"+fileSep+"temp"+fileSep);
    if(!f.exists()){
    f.mkdirs();
    }
    f = new File(f.getAbsolutePath()+fileSep+fileName);
    f.createNewFile();
    // if(true)return;
    ReadAndWrite raw = new ReadAndWrite();
    try {
    raw.init(null, f);
    raw.readAndWrite();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    raw.close(raw.is, raw.os);
    }
    //print on Console
    raw.init(f, null);
    try{
    raw.print();
    }finally{
    raw.close(raw.is, raw.os);
    }

    }}
      

  4.   

    四楼朋友用是javaBean思想很不错!但视乎跟楼主的要求有点相悖了