`
djun100
  • 浏览: 164848 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Java 创建指定格式的文件

 
阅读更多

两种思路:

1: 在创建IO流的时候,指定编码

  1. try{
  2. Filefile=newFile("C:/11.jsp");
  3. OutputStreamout=newFileOutputStream(file);
  4. BufferedWriterrd=newBufferedWriter(newOutputStreamWriter(out,"utf-8"));
  5. rd.write("hello");
  6. rd.close();
  7. out.close();
  8. }catch(IOExceptione){
  9. e.printStackTrace();
  10. }

2: 写入内容的时候指定编码格式

  1. publicstaticvoidmain(String[]agrs)throwsIOException{
  2. FileOutputStreamout=null;
  3. try{
  4. out=newFileOutputStream(newFile("a.txt"));
  5. //将数据,转换成某一个字符集的byte数组,然后直接写入文件即可
  6. out.write("你好hello".getBytes("UTF-8"));
  7. }catch(FileNotFoundExceptione){
  8. e.printStackTrace();
  9. }finally{
  10. out.close();
  11. }
  12. }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics