Java:写入时的空白文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17144067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:07:54  来源:igfitidea点击:

Java: Blank File on writing

javafilefile-ioio

提问by nish

I am trying to write to a file in Java using the following function:

我正在尝试使用以下函数在 Java 中写入文件:

public void writeTextFile(String filename, double s){
  FileWriter output=null;
  try{
    output= new FileWriter(filename);
    BufferedWriter writer=new BufferedWriter(output);
    String ss = String.valueOf(s);
    writer.write(ss);
  } catch (Exception e) {
    throw new RuntimeException(e);
  } finally {
    if (output != null) {
      try {
        output.flush();
        output.close();
      } catch (IOException e) {

      }
    }
  }
}

I also tried using:

我也尝试使用:

FileOutputStream out;
PrintStream prt;
try{
  out= new FileOutputStream("result");
  prt=new PrintStream(out);
  prt.println(value);
  prt.flush();
  prt.close();
}
catch(Exception e){
  System.out.println("File write error");}

But i end up with a blank output file with both the methods. Need help!!:(

但我最终得到了一个带有这两种方法的空白输出文件。需要帮忙!!:(

回答by Ruchira Gayan Ranaweera

This will work,

这将工作,

public void writeTextFile(String filename, double s){
 FileWriter output=null;
 try{
  output= new FileWriter(filename);
  BufferedWriter writer=new BufferedWriter(output);
  String ss = String.valueOf(s);
  writer.append(ss);
  writer.close();
} catch (Exception e) {
  throw new RuntimeException(e);
} finally {
  if (output != null) {
  try {
    output.flush();
    output.close();
  } catch (IOException e) {

  }
}
}
}

回答by DarthCoder

Hey its simple the error is that you aren't flushing the bufferedwriter please do that before you flush and close the stream.

嘿,这很简单,错误是您没有刷新缓冲写入器,请在刷新和关闭流之前执行此操作。

add this to your first code

将此添加到您的第一个代码中

public static void writeTextFile(String filename, double s){
        FileWriter output=null;
        try{
            output= new FileWriter(filename);
            BufferedWriter writer=new BufferedWriter(output);
            String ss = String.valueOf(s);
            System.out.println("ss:"+ss);
            writer.write(ss);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.flush();
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

回答by dbkoren

try this

试试这个

use this link : Tutorial on file writing

使用此链接: 文件写入教程

Code from the link :

来自链接的代码:

public static void main(String[] args) {
        try {

            String content = "This is the content to write into file";

            File file = new File("/users/mkyong/filename.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

回答by Enrichman

What if you try:

如果你尝试:

File file = new File("c:/newfile.txt");
    String content = "This is the text content";

    try (FileOutputStream fop = new FileOutputStream(file)) {

        // if file doesn't exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        // get the content in bytes
        byte[] contentInBytes = content.getBytes();

        fop.write(contentInBytes);
        fop.flush();
        fop.close();

        System.out.println("Done");

    } catch (IOException e) {
        e.printStackTrace();
    }

Based on this tutorial: http://www.mkyong.com/java/how-to-write-to-file-in-java-fileoutputstream-example/

基于本教程:http: //www.mkyong.com/java/how-to-write-to-file-in-java-fileoutputstream-example/

回答by Sovanndy

You have to flush and close BufferedWriter not FileWriter.

您必须刷新并关闭 BufferedWriter 而不是 FileWriter。