java - Java 中的 BufferedReader

标签 java outputstream

我在使用 Java 中的 BufferReader 和 OutputStream 时遇到问题。我的目标:当你从键盘插入一些东西时 - 它会进入文件。我应该如何更正我的代码?

import java.io.*;

class IntoFile {
    public static void main(String[] args) throws Exception{
        try {
            BufferedReader sisse = new BufferedReader(new InputStreamReader(System.in));
            System.out.print ("Insert something: ");
            String s = sisse.readLine();

            byte[] buf = new byte[1024];
            OutputStream valja = new FileOutputStream(new File(args[0]));
            String line = null;
            while ((line = br.readLine()) != null) {

                }
            valja.close();
        } 
        catch (IOException e) {
            System.out.println ("I/O: " + e);
        }
    }
}

谢谢!

最佳答案

我会使用扫描仪和PrintWriter

C:\Documents and Settings\glowcoder\My Documents>javac Dmitri.java

C:\Documents and Settings\glowcoder\My Documents>java Dmitri
test
woohoo
quit

C:\Documents and Settings\glowcoder\My Documents>more out.txt
test
woohoo
quit

C:\Documents and Settings\glowcoder\My Documents>

代码:

import java.io.*;
import java.util.*;
class Dmitri {
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter("out.txt");
        while(in.hasNextLine()) {
            String line = in.nextLine();
            out.println(line);
            out.flush(); // not necessary every time, but simple to do so
            if(line.equalsIgnoreCase("QUIT")) break;
        }
        out.close();
    }
}

关于java - Java 中的 BufferedReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5763285/

相关文章:

java - for 循环计算用户数量

java - JPA:QueryCriteria where 子句中的谓词和表达式

java - Java 和多线程中的命名管道

java - androidcameraX API亮度问题

java - 将输入流连接到输出流

java - 是否可以检查函数是否被调用?

java - Jasper Report 生成 PDF,然后 Glassfish 崩溃/关闭

java - 我写入的读取的不同字节

c++ - outData.txt 没有输出正确的值

java - 保存数据+字符串