How do I add encoding option "UTF-8" to my JFileChooser save dialog box.
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter1 = new FileNameExtensionFilter("TEXT", ".txt");
fileChooser.setFileFilter(filter1);
fileChooser.setDialogTitle("Specify a file to save");
int returnVal = fileChooser.showSaveDialog(null);
if ( returnVal == JFileChooser.APPROVE_OPTION )
{
File file = fileChooser.getSelectedFile();
//do something
}
Using standard Java:
String aString = "Test\nWithNewline";
try {
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("outfilename"), "UTF-8"));
out.write(aString);
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
Using Apache Commons:
File f = new File("outfilename");
FileUtils.writeStringToFile(f, aString, "UTF-8");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.