1 使用FileWrite写文本文件
  2 
  3 
  4 public static void useFileWriter(String fileName) throws IOException {
  5     File file = new File(fileName);
  6     FileWriter fileWriter = new FileWriter(file);
  7 
  8     fileWriter.write("it is a test");
  9 
 10     fileWriter.close();
 11 }
 12 
 13 
 14 
 15 使用BufferedWrite写文本文件
 16 
 17 
 18 public static void useBufferedWriter(String fileName) throws IOException{
 19     File file = new File(fileName);
 20     BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName));
 21 
 22     bufferedWriter.write("hello bufferedwrite");
 23 
 24     bufferedWriter.flush();
 25     bufferedWriter.close();
 26 }
 27 
 28 
 29 
 30 使用Files写文件,最简单
 31 
 32 
 33 public static void useJdk8(String fileName) throws IOException {
 34     Files.write(Paths.get(fileName), "hello usejdk8".getBytes(), StandardOpenOption.CREATE);
 35 }
 36 
 37 
 38 私用FileOutputStream写文件
 39 
 40 
 41 public static void useFileOutputStream(String fileName) throws IOException{
 42     File file = new File(fileName);
 43 
 44     FileOutputStream fileOutputStream = new FileOutputStream(file);
 45     fileOutputStream.write("hello fileoutputstream".getBytes());
 46 
 47     fileOutputStream.flush();
 48 
 49     fileOutputStream.close();
 50 }
 51 
 52 
 53 
 54 使用BufferedFileOutputStream写文件,速度最快,数据cache在jvm中,容易丢数据
 55 
 56 
 57 public static void useBufferedFileOutputStream(String fileName) {
 58     File file = new File(fileName);
 59 
 60 
 61     BufferedOutputStream bufferedOutputStream = null;
 62     try {
 63         bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
 64 
 65         bufferedOutputStream.write("hello BufferedFileOutputStream".getBytes());
 66 
 67         bufferedOutputStream.flush();
 68     }catch(IOException e) {
 69 
 70 
 71     }finally {
 72         if(bufferedOutputStream!=null) {
 73             try {
 74                 bufferedOutputStream.close();
 75             } catch (IOException e1) {
 76                 //TODO  do something
 77             }
 78         }
 79     }
 80 
 81 }
 82 
 83 
 84 
 85 使用RandomAccessFile写文件,速度最慢,直接刷盘
 86 
 87 
 88 public static void useRandomAccessFile(String fileName) {
 89     RandomAccessFile randomAccessFile = null;
 90 
 91 
 92     try {
 93         randomAccessFile = new RandomAccessFile(fileName, "rw");
 94         randomAccessFile.seek(15); //从第15个byte位置开始写, 原文件的第15个之后的字符会被覆盖一部分
 95         randomAccessFile.write("useRandomAccessFile".getBytes());
 96     } catch (IOException e) {
 97         e.printStackTrace();
 98     }finally {
 99         if(randomAccessFile!=null) {
             try {
                 randomAccessFile.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
 
     }
 }
 
 
 
 使用FileChannel写文件
 
 
 public static void useFileChannel(String fileName) {
 
     FileChannel fileChannel = null;
     try {
         FileChannel channel = new FileOutputStream(fileName).getChannel();
         channel.write(ByteBuffer.wrap("useFileChannel".getBytes()));
     } catch (IOException e) {
         e.printStackTrace();
     } finally {
         if(fileChannel!=null) {
             try {
                 fileChannel.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 }
 
 
 
 使用MappedByteBuffer写文件,速度快,OS级别内存映射
 
 
 public static void useMappedByteBuffer(String fileName){
 
     RandomAccessFile randomAccessFile = null;
 
     try {
         randomAccessFile  = new RandomAccessFile(fileName, "rw");
         FileChannel fileChannel = randomAccessFile.getChannel();
 
 
         String content = "useMappedByteBuffer";
 
         MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, content.getBytes().length);
 
         mappedByteBuffer.put(content.getBytes());   //mappedByteBuffer大小不能小于content的字节数
 
 
     } catch (IOException e) {
         e.printStackTrace();
     }finally {
         if(randomAccessFile!=null) {
             try {
                 randomAccessFile.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 }         

java写文件的更多相关文章

  1. java写文件实现换行

    Java 写文件实现换行   第一种: 写入的内容中利用\r\n进行换行 File file = new File("D:/text"); try { if(!file.exist ...

  2. java写文件时,输出不完整的原因以及解决方法

    在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bo=new BufferedWriter(new FileWriter("sql语句.txt")) ...

  3. java 写文件解析

    import java.io.File; import java.io.FileOutputStream; import java.io.*; public class FileTest { publ ...

  4. java写文件时,输出不完整的原因以及解决方法close()或flush()

    在java的IO体系中,写文件通常会用到下面语句 BufferedWriter bw=new BufferedWriter(new FileWriter("sql语句.txt")) ...

  5. java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结

    代码如下: import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; public ...

  6. java写文件读写操作(IO流,字符流)

    package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...

  7. java写文件读写操作(IO流,字节流)

    package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...

  8. java写文件的基本操作

    import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOExce ...

  9. Java 写文件实现换行

    第一种: 写入的内容中利用\r\n进行换行 File file = new File("D:/text"); try { if(!file.exists()) file.creat ...

随机推荐

  1. ubuntu14.04安装了im-switch后系统设置中不见了语言支持

    sudo apt-get install language-selector-gnome

  2. Markdown 简明语法手册

    Markdown 简明语法手册 本文原文http://www.jianshu.com/p/fdb5cbdaf244 根据个人使用情况有所修改. Markdown是一种轻量级标记语言,简称md.创始人为 ...

  3. Centos系统查看CPU有关信息

    top命令按1,看到几个CPU就代表是几核的. 查看CPU有几颗逻辑cpu,4代表有4个逻辑CPU,同时CPU的型号也打印出了,服务器一般都是至强的CPU [root@svn ~]# cat /pro ...

  4. input的onkeyup效果 超级简短代码

    效果代码 title="请输入正确的十六位数字" onkeyup="javascript: return this.value = this.value.toUpperC ...

  5. xheditor编辑器的使用

    xheditor编辑器的使用 一个博客.cms网站都一定会用到一个html编辑器,刚好xmfdsh在做网站时候需要用到这类编辑器,在对比了之后,发现其实差不了多少,刚好一个不错的friend在用xhe ...

  6. zabbix进程构成

    了解完zabbix特性之后,本该进入zabbix安装教程,但是我觉得在安装之前我们很有必要了解一下zabbix进程组成结构,默认情况下zabbix包含5个程序:zabbix_agentd.zabbix ...

  7. zend stuido 12.5的插件安装和xdebug调试器的配置和和配置注意

    参考: zend stuido 12.5的插件安装 zend 12.5 安装插件是按类别进行分类了的, 而且是在欢迎 界面就可以直接安装, 安装后,要重启zend才能生效 版式设计的一个基本点就是: ...

  8. TeX — Beauty and Fun

    我是初学者,你推荐使用什么发行的 TeX? 我应该用 LaTeX 吗? 我认为最好的发行是 TeXLive CD,它不但包含了所有操作系统需要的程序,而且有许许多多宏包,如果你不是特别特殊的用户,有了 ...

  9. 11个Visual Studio代码性能分析工具

    软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行代码分析和 ...

  10. Oracle卸载

    用Oracle自带的卸载程序不能从根本上卸载Oracle,从而为下次的安装留下隐患,所以要想完全卸载Oracle就必须要直接将注册表清除. 步骤如下: 1. 开始->设置->控制面板-&g ...