简体   繁体   English

Java:写作时空白文件

[英]Java: Blank File on writing

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!!:( 需要帮忙!!:(

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) {

  }
}
}
}

Hey its simple the error is that you aren't flushing the bufferedwriter please do that before you flush and close the stream. 嘿,这很简单,错误是您没有刷新bufferedwriter,请先刷新然后关闭流。

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();
                }
            }
        }
    }

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();
        }
    }

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/

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

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

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