简体   繁体   English

用Java编写文件不起作用

[英]Writing file in java does not work

I am trying to write some content into file inside my spring controller. 我正在尝试将一些内容写入spring控制器内的文件中。 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); 我拥有此文件文件的最后一部分= new File(fileName); is what is having issues. 是什么问题。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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