java写文件

简介:   1 使用FileWrite写文本文件  2   3   4 public static void useFileWriter(String fileName) throws IOException {  5     File file = new File(fileName);  6     ...
  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) {
100              try {
101                 randomAccessFile.close();
102             }  catch (IOException e) {
103                 e.printStackTrace();
104             }
105         }
106 
107     }
108 }
109 
110 
111 
112 使用FileChannel写文件
113 
114 
115  public  static  void useFileChannel(String fileName) {
116 
117     FileChannel fileChannel =  null;
118      try {
119         FileChannel channel =  new FileOutputStream(fileName).getChannel();
120         channel.write(ByteBuffer.wrap("useFileChannel".getBytes()));
121     }  catch (IOException e) {
122         e.printStackTrace();
123     }  finally {
124          if(fileChannel!= null) {
125              try {
126                 fileChannel.close();
127             }  catch (IOException e) {
128                 e.printStackTrace();
129             }
130         }
131     }
132 }
133 
134 
135 
136 使用MappedByteBuffer写文件,速度快,OS级别内存映射
137 
138 
139  public  static  void useMappedByteBuffer(String fileName){
140 
141     RandomAccessFile randomAccessFile =  null;
142 
143      try {
144         randomAccessFile  =  new RandomAccessFile(fileName, "rw");
145         FileChannel fileChannel = randomAccessFile.getChannel();
146 
147 
148         String content = "useMappedByteBuffer";
149 
150         MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, content.getBytes().length);
151 
152         mappedByteBuffer.put(content.getBytes());    // mappedByteBuffer大小不能小于content的字节数
153 
154 
155     }  catch (IOException e) {
156         e.printStackTrace();
157     } finally {
158          if(randomAccessFile!= null) {
159              try {
160                 randomAccessFile.close();
161             }  catch (IOException e) {
162                 e.printStackTrace();
163             }
164         }
165     }
166 }         
若转载请注明出处!若有疑问,请回复交流!
目录
相关文章
|
1月前
|
Java
有关Java发送邮件信息(支持附件、html文件模板发送)
有关Java发送邮件信息(支持附件、html文件模板发送)
31 1
|
1月前
|
Java
java中替换文件内容
java中替换文件内容
14 1
|
1月前
|
Java API
Java中文件与输入输出
Java中文件与输入输出
|
1月前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
10 0
|
1月前
|
Java
java程序导出堆文件
java程序导出堆文件
|
1月前
|
SQL Oracle Java
sql文件批处理程序-java桌面应用
sql文件批处理程序-java桌面应用
25 0
|
1月前
|
存储 Java 文件存储
如何用 Java 压缩 ZIP 文件?
【2月更文挑战第21天】
35 1
|
1月前
|
Java
Java实现文件和目录的管理
Java实现文件和目录的管理
29 0
|
5天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
33 3
|
1月前
|
Java 数据库连接 API
Java 学习路线:基础知识、数据类型、条件语句、函数、循环、异常处理、数据结构、面向对象编程、包、文件和 API
Java 是一种广泛使用的、面向对象的编程语言,始于1995年,以其跨平台性、安全性和可靠性著称,应用于从移动设备到数据中心的各种场景。基础概念包括变量(如局部、实例和静态变量)、数据类型(原始和非原始)、条件语句(if、else、switch等)、函数、循环、异常处理、数据结构(如数组、链表)和面向对象编程(类、接口、继承等)。深入学习还包括包、内存管理、集合框架、序列化、网络套接字、泛型、流、JVM、垃圾回收和线程。构建工具如Gradle、Maven和Ant简化了开发流程,Web框架如Spring和Spring Boot支持Web应用开发。ORM工具如JPA、Hibernate处理对象与数
94 3