1

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

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

4

5 回答 5

2

这将起作用,

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

  }
}
}
}
于 2013-06-17T09:51:28.650 回答
1

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

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

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();
                }
            }
        }
    }
于 2013-06-17T09:26:42.610 回答
0

试试这个

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

链接中的代码:

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();
        }
    }
于 2013-06-17T09:19:33.407 回答
-2

如果你尝试:

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/

于 2013-06-17T09:18:31.597 回答
-2

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

于 2014-02-06T01:51:09.300 回答