输出UTF-8-BOM格式的文件

        File localDir = new File(localPath);
        if(!localDir.exists()){
            localDir.mkdirs();
        }
        
        File file = new File(localPath + fileName);
        
        if(!file.exists()){
            OutputStreamWriter writer = null;
            FileOutputStream fos = null;
            try {
                file.createNewFile();
                fos = new FileOutputStream(file);
                //需要UTF-8-BOM编码
                byte[] uft8bom={(byte)0xef,(byte)0xbb,(byte)0xbf};
                fos.write(uft8bom);
                writer = new OutputStreamWriter(fos, "UTF-8");
                writer.write(content);
                writer.flush();
                
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(fos!=null){
                        fos.close();
                    }
                    if(writer!=null){
                        writer.close();
                    }
                    
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

 

上一篇:new FileOutputStream和openFileOutput区别


下一篇:Android保存多张图片到本地