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. Excel_replace

    有时候我们需要对单元格中的数据需要一些精确的处理,比如将部分以70开的工号升为706,这时简单的查找替换就不能满足我的需求,因为这样会替换掉工号中末尾或者中间位的70,造成工号的错误. 如何实现这种精 ...

  2. ubuntu下安装TexLive和Texmaker

    也可以参考ubuntu14.04配置中文latex完美环境(texlive+texmaker+lyx) 设置中文字体的时候参考ubuntu 下安装 texlive 并设置 ctex 中文套装 1.首先 ...

  3. JavaWeb学习笔记——XML和SAX解析区别

  4. hasClass addClass removeClass

    //函数有class function hasClass(ele,cls){ return -1<(" "+ele.className+" ").inde ...

  5. Visual Studio 2012中Visual Assist破解办法

    本工具有两种破解方式 1.破解方式一:Visual_Assist_X_DiE.exe 运行Visual_Assist_X_DiE.exe,勾选安装的VC版本,点击“Patch”按钮即可. 2.破解方式 ...

  6. bash: ifconfig: command not found解决方法

    1.问题: #ifconfig bash: ifconfig: command not found 2.原因:非root用户的path中没有/sbin/ifconfig ,其它的命令也可以出现这种情况 ...

  7. (转载)ecshop制作成手机网站的方法

    ecshop用手机访问的时候,会自动跳转到  /mobile 目录下,ecshop自带的wap模板是用wml制作的,如果按这种情况,又需要制作一套模板,太麻烦,现在都是智能手机时代,wml模板已经不能 ...

  8. [c#基础]AutoResetEvent

    摘要 AutoResetEvent:msdn的描述是通知正在等待的线程已发生事件.此类不能被继承.也就是说它有那么一个时间点,会通知正在等待的线程可以做其它的事情了. AutoResetEvent 该 ...

  9. UCMA设置lync在线状态

    摘要 UCMA全称Microsoft Unified Communications Managed API,主要用来构建工作在Microsoft Lync Server上的中间层应用程序.开发人员可以 ...

  10. ng指令之 ng-repeat 篇

    1>数据绑定     ng-repeat可以绑定数组和JSON对象数据.从下图可以看出控制器的scope()函数得到的对象与controller('ctrlName',['$scope',fun ...