1

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!!:(

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
nish
  • 6,952
  • 18
  • 74
  • 128

5 Answers5

2

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

  }
}
}
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

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();
                }
            }
        }
    }
DarthCoder
  • 354
  • 1
  • 11
0

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();
        }
    }
dbkoren
  • 1,305
  • 1
  • 11
  • 16
-2

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/

Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • You don't need to call createNewFile(), or flush before closing. Poor quality tutorial. – user207421 Feb 06 '14 at 02:21
  • @EJP no need to downvote, they're just patterns (it's not a poor qualit tutorial). Read here http://stackoverflow.com/questions/9620683/java-fileoutputstream-create-file-if-not-exists and here http://stackoverflow.com/questions/9858495/using-flush-before-close – Enrichman Feb 06 '14 at 10:06
-2

You have to flush and close BufferedWriter not FileWriter.

László Papp
  • 51,870
  • 39
  • 111
  • 135