为什么BufferedWriter实例化不会写入Java中的txt文件?

时间:2015-01-31 04:47:03

标签: java bufferedwriter writer

我试图用BufferedWriter实例化一个对象,它不会工作。当我使用write函数时会发生问题。为什么不让我写信给文件? 错误是找不到符号。请帮我。我知道有人知道。当这是一个bufferedWriter方法时,为什么不能找到符号?

package ex5_abcd;

import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;

public class EX5_ABCD {

    public static void main(String[] args) {
        boolean go = true;
        String firstN, lastN;
        String lineWritten = "";
        int IdNum;
        Scanner input = new Scanner(System.in);
        Path file = Paths.get("C:\\Java\\empList.txt");
        try {
            OutputStream output = new BufferedOutputStream(Files.newOutputStream(file, CREATE));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
            while (go) {
                System.out.println("Please enter Employee's First Name");
                firstN = input.nextLine();
                System.out.println("Please enter Employee's Last Name");
                lastN = input.nextLine();
                System.out.println("Please enter " + firstN + " " + lastN);
                IdNum = input.nextInt();
                lineWritten = IdNum + " " + firstN + " " + lastN;
                int lineLength = lineWritten.length();
                char [] testChar  =  new char[1];
            testChar [0] = 'a';
            writer = write(testChar, 0, lineLength); // Why write error
                writer.flush();
                writer.newLine();

            }
            writer.close();
        } catch (Exception e) {
            System.out.println("Error Msg:" + e);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

writer = write(lineWritten, 0, lineLength);

应该改写为

writer.write(lineWritten, 0, lineLength); 

由于writer不是对象的引用,您应该调用writer对象的方法,而不是将writer设置为另一个值

回顾:

(=)设置一个值。

另外,既然你从来没有在你的循环中将go的值设置为false,那么你将继续在那里循环...永远......并且永远....我建议不要让它发生< / p>