如何继续从控制台读取数据并使用bufferedreader保存到文件?

jdzmm42g  于 2021-08-20  发布在  Java
关注(0)|答案(3)|浏览(311)

我想制作一个小程序,用户可以通过控制台编写整个文件。这里我面临的问题是,我使用的是bufferedreader。使用它,我可以从控制台读取数据,但如果我按下enter按钮,它将无法读取,程序将结束。那么,即使用户按下enter按钮,如何继续从控制台读取数据呢。同时,请建议结束文件编写过程的正确方法。
在使用bufferedreader之前,我使用的是scanner,但也面临一些问题。在那之后,我试过这个,但不知道怎么读所有的台词?

Scanner s = new Scanner(System.in);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(fileName);
        System.out.println("When you press ctrl+z your file writing process will be end.");
        System.out.println("Now press enter to write a file...");
        s.nextLine();
        String fileStr = null;
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(System.in));
        while((fileStr = reader.readLine()) != null){
            byte[] data = fileStr.getBytes();
            fos.write(data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        s.close();
        if(fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
mspsb9vt

mspsb9vt1#

你应该
在流上使用try-with资源,因此java本身在任何情况下都会关闭流
不要使用fileinputstream,java为nio中的文件提供了一个很好的api。 Files.write(Path, Iterable<? extends CharSequence>) ,您可以一次写入整个文件,也可以只附加到文件中。
不要关闭连接到的扫描仪 System.in ,这也将关闭底层流(因此 System.in )
您目前无法中断while循环,它将始终等待用户输入,中断不会进入循环,因此您需要实现某种退出方式

vuv7lop3

vuv7lop32#

我们非常接近,我认为您应该像这样循环s.nextline():

Scanner s = new Scanner(System.in);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(fileName);
        System.out.println("When you press ctrl+z your file writing process will be end.");
        System.out.println("Now press enter to write a file...");
        s.nextLine();
        String fileStr = null;
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(System.in));
        fileStr = reader.readLine();
        while (fileStr != null) { // reader.readline() will return null if no caracter to read (last line of file)
            byte[] data = fileStr.getBytes();
            fos.write(data);
            fileStr = reader.readLine(); // Read next line before looping
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        s.close();
        if(fos != null) {
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
2exbekwf

2exbekwf3#

试试这个。。。

private static final String DOCUMENT_END_TOKEN = ">e";

    public static void main(String[] arg) {
            Scanner s = new Scanner(System.in);
            FileOutputStream fileOutputStream;
            try {
                    System.out.println("When you type '>e' your file writing process will be terminated.");
                    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                    System.out.println("File name?");
                    String fileName = reader.readLine();
                    fileOutputStream = new FileOutputStream(fileName);
                    System.out.println("Now write to the file: " + fileName);
                    StringBuilder fileContentBuilder = new StringBuilder();
                    String readDocLine = reader.readLine();
                    while (!readDocLine.equalsIgnoreCase(DOCUMENT_END_TOKEN)) {
                            fileContentBuilder.append(readDocLine).append("\n");
                            readDocLine = reader.readLine();
                    }
                    byte[] data = fileContentBuilder.toString().getBytes();
                    fileOutputStream.write(data);
            } catch (IOException e) {
                    e.printStackTrace();
            } finally {
                    s.close();
            }
    }

相关问题