[英]Packing a Java application with jpackage: Writing to a file does not work
[英]Writing file in java does not work
我正在嘗試將一些內容寫入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;
}
我擁有此文件文件的最后一部分= new File(fileName); 是什么問題。
首先不要吞下異常,也要清空緩沖的編寫器,這是您應該使用的代碼:
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;
}
請看下面的代碼,
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.