代码很简单如下:
public static void createFile(String filePath) throws IOException {
FileOutputStream out=null;
try {
out = new FileOutputStream(new File(filePath));
System.out.println("创建文件成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
out.close();
}
}
我想要这样效果:本来这样就可以成功创建一个文件,但文件编码格式不是我想要的(好像windows系统 默认 US-ASCII 编码格式保存文件)能不能在程序指定文件保存的编码格式,例如:utf-8 ,文件自动就以utf-8编码保存了。
==================================================================
将你的数据,转换成某一个字符集的byte数组,然后直接写入文件即可
- public static void main(String[] agrs) throws IOException {
- FileOutputStream out = null;
- try {
- out = new FileOutputStream(new File("a.txt"));
- // 将你的数据,转换成某一个字符集的byte数组,然后直接写入文件即可
- out.write("你好hello".getBytes("UTF-8"));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } finally {
- out.close();
- }
- }