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. python数据库操作常用功能使用详解(创建表/插入数据/获取数据)

    实例1.取得MYSQL版本 复制代码 代码如下: # -*- coding: UTF-8 -*-#安装MYSQL DB for pythonimport MySQLdb as mdbcon = Non ...

  2. javascript Date format(js日期格式化)

    这个用这比较爽,记录一下// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年( ...

  3. 自然语言13_Stop words with NLTK

    https://www.pythonprogramming.net/stop-words-nltk-tutorial/?completed=/tokenizing-words-sentences-nl ...

  4. Iframe 在项目中的使用总结

    参考:http://www.cnblogs.com/MaxIE/archive/2008/08/13/1266597.html 问题一:首先我们用iframe加载页面,第一个需要解决的问题是高度自适应 ...

  5. Python之路【第七篇】:初识Socket

    What is Socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制, ...

  6. css3d总结

    3d舞台 设置 perspective(景深): length, 可以设置overflow:hidden 3d舞台下 -> 3d元素容器  设置 transform-style: preserv ...

  7. yii2 数据库操作(转)

    开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的" Web 应用是 config/web.php),DSN( Data Source Name ...

  8. 基于密度聚类的DBSCAN和kmeans算法比较

    根据各行业特性,人们提出了多种聚类算法,简单分为:基于层次.划分.密度.图论.网格和模型的几大类. 其中,基于密度的聚类算法以DBSCAN最具有代表性.  场景 一 假设有如下图的一组数据, 生成数据 ...

  9. oracle中时间运算

    Oracle两个函数相减,默认得到的是天数,按日期格式,精准到响应的精度,如用sysdate(2015/12/7 10:17:52),时间差精确到秒. 在此基础上,oracle两个时间相减默认天数*2 ...

  10. 一行代码解决各种IE兼容问题IE8,IE9,IE10

    一:一.指定文件兼容性模式(Xee:因为我已经放弃IE6,7了,所以以后设计的网页最低支持IE8;) 要为你的网页指定文件模式,需要在你的网页中使用meta元素放入X-UA-Compatible ht ...