我正在尝试以UTF_8格式保存.txt文件,但我不明白我应该将其添加到我的代码中。这就是我得到的。
String fileStringName = userHome + "/wordsearch directory/Puzzle.txt";
File filePath = new File(userHome + "/wordsearch directory/Puzzle.txt");
File dirPath = new File (userHome + "/wordsearch directory");
//new method just made
//make pop up jpane displaying puzzle ID.
boolean filePathExists = false;
if(!filePath.exists())
{
filePathExists = dirPath.mkdirs();
try {
//BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),"UTF-8"));
writer = new PrintWriter(new BufferedWriter(new FileWriter(fileStringName, true)));
// Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileStringName), "UTF-8"));
} catch (IOException e2) {
writer = null;
e2.printStackTrace();
}
writer.printf(savePuzzle.toString());
writer.printf("\n");
writer.close();
答案 0 :(得分:0)
试试这个:
File dirPath = new File (userHome + "/wordsearch directory");
File filePath = new File(dirPath, "Puzzle.txt");
try {
if (!dirPath.exists()) {
dirPath.mkdirs();
}
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8));
writer.write(savePuzzle.toString());
writer.write("\n"); // or writer.newLine()
writer.close();
} catch (IOException e) {
e.printStackTrace();
}