简体   繁体   English

创建文件并写入文件

[英]Creating a file and writing to it

I created a file named 'test.txt' and then took input from the user to write the input to the file. 我创建了一个名为“ test.txt”的文件,然后从用户那里获取输入,以将输入写入该文件。 Everything runs fine. 一切运行正常。 The program doesn't show any error at all. 该程序根本不显示任何错误。 The file is created and the program takes input from the user but when I checked the content of the file, it was empty. 文件已创建,程序从用户处获取输入,但是当我检查文件内容时,它为空。 Can anyone figure out what is wrong with my code? 谁能弄清楚我的代码有什么问题? The code is as follows. 代码如下。

package InputOutput;

import java.io.*;

public class CharacterFileReaderAndFileWriter{

private BufferedReader br = null;
private BufferedWriter bw = null;
private PrintWriter pw = new PrintWriter(System.out, true);

public File createFile() throws IOException{
    File f = new File("test.txt");
    return f;
}

public void writeToFile() throws IOException{
    try{
        bw = new BufferedWriter(new FileWriter(createFile()));
    }
    catch(FileNotFoundException ex){
        ex.printStackTrace();
    }

    //take input from the console (user)
    br = new BufferedReader(new InputStreamReader(System.in));
    String s;

    pw.println("Please enter something");
    pw.println("To stop the program, enter 'stop'");

    do{
        s = br.readLine();
        if(s.compareTo("stop")==0)
            break;
        s+= "\r\n";//adding an new line to the string s         
        bw.write(s);            
    }
    while(s.compareTo("stop")!=0);

    br.close();
    bw.close();

}

public static void main(String[] args) throws IOException{

    CharacterFileReaderAndFileWriter cfr = new CharacterFileReaderAndFileWriter();
    cfr.writeToFile();
}

} }

Most example programs show that you have to call .flush() on your BufferedWriter before the .close() . 大多数示例程序表明,您必须在.close()之前在BufferedWriter上调用.flush() .close() This should not be required, .close() should call .flush() automatically, but it doesn't hurt. 这不是必需的, .close()应该自动调用.flush() ,但这并不.flush() Also you should call all the Stream / Writer objects .close() methods in reverse order as well, again correctly written classes should call .close() on all the object they wrap, but it doesn't hurt to do it anyway. 同样,您还应该以相反的顺序调用所有Stream / Writer对象.close()方法,同样,正确编写的类也应该在它们包装的所有对象上都调用.close() ,但是这样做也无妨。

Other things that might catch you out later: 其他可能在以后吸引您的事情:

if(s.compareTo("stop")==0)

should be 应该

if ("stop".equalsIgnoreCase(s))

it is more efficient, eliminates the possibility of a NullPointerException on s , handles any case of stop and most importantly more idiomatic Java than using .compareTo() 与使用.compareTo() ,它更有效,消除了s上发生NullPointerException的可能性,处理任何stop情况,最重要的s ,它处理了更多惯用的Java。

    s+= "\r\n";//adding an new line to the string s         
    bw.write(s);        

should be 应该

    bw.write(System.getProperty("line.separator"));        
    bw.write(s);        

The s+= creates intermediate objects and garbage that needs to be collected. s+=创建需要收集的中间对象和垃圾。 Hard coding line endings is bad as well. 硬编码的行尾也很糟糕。

You need close the outputstream. 您需要关闭输出流。 file.close(); file.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM