在java中编写文件不起作用

时间:2012-02-22 02:56:45

标签: java io

我正在尝试将一些内容写入我的spring控制器内的文件中。在写之前我正在创建目录。但该文件没有写入。我真的很困惑。这是代码

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

我有这个文件的最后一部分文件=新文件(fileName);是有问题的。

2 个答案:

答案 0 :(得分:1)

首先不要吞下异常并刷新缓冲的编写器,这是你应该使用的代码:

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

答案 1 :(得分:0)

请查看以下代码,

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