【问题标题】:Java: Blank File on writingJava:写入时的空白文件
【发布时间】:2013-06-13 04:52:30
【问题描述】:

我正在尝试使用以下函数在 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) {

      }
    }
  }
}

我也尝试过使用:

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

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

【问题讨论】:

  • 它工作正常...你在分配“价值”吗??
  • 尝试在第一个代码中添加 writer.flush 和 writer.close。
  • 加上System.out.println("value: '" + value + "'");会打印什么?
  • @DarthCoder : value=esa.getRelatedness(args[0], args[1]);

标签: java file file-io io


【解决方案1】:

这行得通,

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

  }
}
}
}

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 添加 close() 有效。关闭前刷新是多余的。
    【解决方案3】:

    试试这个

    使用此链接: Tutorial on file writing

    链接中的代码:

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

    【讨论】:

    • 使用文件名直接实例化FileWriter 没有任何问题。不需要使用File
    • 你是对的,它的另一个选项不是必需的......已编辑。
    • 您不需要调用 createNewFile()。质量差的教程。
    【解决方案4】:

    如果你尝试怎么办:

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

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

    【讨论】:

    【解决方案5】:

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

    【讨论】:

    • 虽然这是正确的,但其他人已经说过(并接受)了。只有当它添加了其他人没有说过的内容时,您才应该回答。
    • 而且您不必冲洗,只需关闭即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    相关资源
    最近更新 更多