简体   繁体   中英

Java: Blank File on writing

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

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.

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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