1

I am trying to write some content into file inside my spring controller. Before writing i am creating the directory. But the file is not getting written. I am really confused. here is the code

public String storeText(String title, String description) {

    String randomName = null;
    try {

        String baseDir = "C:/MyProjects/eclipse/DreamFolder/";
        randomName = Long.toHexString(Double.doubleToLongBits(Math.random()));
        String folderName = baseDir + randomName;
        String fileName = folderName + "/textCon.txt";

        File fileFolder = new File(folderName);
        fileFolder.mkdir();
        boolean exists = fileFolder.exists();
      if (!exists) {
        System.out.println("storeText folder does not exist");
      }
        System.out.println("storeText folderName - " + folderName);
        System.out.println("storeText fileName - " + fileName);

        File file = new File(fileName);
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write(title);
        out.newLine();
        out.write(randomName);
        out.newLine();
        out.write(description);
        out.close();
    } catch (Exception ex) {
    }
    return randomName;
}

The last portion where i have this File file = new File(fileName); is what is having issues.

3
  • 1
    Are you getting any errors? Catching and discarding Exception ex seems like a bad idea here... Commented Feb 22, 2012 at 2:58
  • Without reading the code, the obvious first step is: don't swallow exceptions. If you simply catch (Exception e) {}, you won't even know where your code is breaking, if it is breaking at all! Commented Feb 22, 2012 at 2:58
  • Sorry, just added System.out.println(ex.getMessage()); in exception. But i dont see any exception. Commented Feb 22, 2012 at 3:01

2 Answers 2

1

Start by not swallowing the exception and also flushing the buffered writer, here's the code you should be using:

public String storeText(String title, String description) {

    String randomName = null;
    try {

        String baseDir = "C:/MyProjects/eclipse/DreamFolder/";
        randomName = Long.toHexString(Double.doubleToLongBits(Math.random()));
        String folderName = baseDir + randomName;
        String fileName = folderName + "/textCon.txt";

        File fileFolder = new File(folderName);
        fileFolder.mkdir();
        boolean exists = fileFolder.exists();
      if (!exists) {
        System.out.println("storeText folder does not exist");
      }
        System.out.println("storeText folderName - " + folderName);
        System.out.println("storeText fileName - " + fileName);

        File file = new File(fileName);
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        out.write(title);
        out.newLine();
        out.write(randomName);
        out.newLine();
        out.write(description);
        out.flush();
        out.close();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    return randomName;
}
Sign up to request clarification or add additional context in comments.

2 Comments

But when you close the BufferedWriter, java is supposed to flush it right?
This solution did work but i am still skeptical since when you close the BufferedWriter, java is supposed to flush it
0

Please look at to following code,

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class MainClass {
  public static void main(String[] args) {
    String phrase = new String("www.java2s.com\n");

    File aFile = new File("test.txt");   
    FileOutputStream outputFile = null;  
    try {
      outputFile = new FileOutputStream(aFile, true);
      System.out.println("File stream created successfully.");
    } catch (FileNotFoundException e) {
      e.printStackTrace(System.err);
    } 

    FileChannel outChannel = outputFile.getChannel();

    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println("New buffer:           position = " + buf.position()
                       + "\tLimit = " + buf.limit() + "\tcapacity = "
                       + buf.capacity());

    // Load the data into the buffer
    for (char ch : phrase.toCharArray()) {
      buf.putChar(ch);
    }
    System.out.println("Buffer after loading: position = " + buf.position()
                       + "\tLimit = " + buf.limit() + "\tcapacity = "
                       + buf.capacity());
    buf.flip();
    System.out.println("Buffer after flip:    position = " + buf.position() 
                       + "\tLimit = " + buf.limit() + "\tcapacity = " 
                       + buf.capacity());

    try {
      outChannel.write(buf);
      outputFile.close();
      System.out.println("Buffer contents written to file.");
    } catch (IOException e) {
      e.printStackTrace(System.err);
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.