教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 java写入文件的几种方法小结

java写入文件的几种方法小结

发布时间:2016-11-25   编辑:jiaochengji.com
教程集为您提供java写入文件的几种方法小结等资源,欢迎您收藏本站,我们将为您提供最新的java写入文件的几种方法小结资源
在java中我常用的向文件中写入新内容的三种方法,分别是FileWritter,BufferedWriter ,FileOutputStream下面我分别给各位介绍三个实例希望对你有所帮助。

一,FileWritter写入文件

FileWritter, 字符流写入字符到文件。默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函数的第二个参数,它会保留现有的内容,并追加新内容在文件的末尾。

1. 替换所有现有的内容与新的内容。

new FileWriter(file);2. 保留现有的内容和附加在该文件的末尾的新内容。

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy6181')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy6181" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">new FileWriter(file,true);</td> </tr> </tbody> </table>

追加文件示例
一个文本文件,命名为“javaio-appendfile.txt”,并包含以下内容。

ABC Hello追加新内容 new FileWriter(file,true)

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy2866')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy2866" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">package com.yiibai.file;
 
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
 
public class AppendToFileExample
{
    public static void main( String[] args )
    { 
     try{
      String data = " This content will append to the end of the file";
 
      File file =new File("javaio-appendfile.txt");
 
      //if file doesnt exists, then create it
      if(!file.exists()){
       file.createNewFile();
      }
 
      //true = append file
      FileWriter fileWritter = new FileWriter(file.getName(),true);
             BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
             bufferWritter.write(data);
             bufferWritter.close();
 
         System.out.println("Done");
 
     }catch(IOException e){
      e.printStackTrace();
     }
    }
}</td> </tr> </tbody> </table>

结果
现在,文本文件“javaio-appendfile.txt”内容更新如下:

ABC Hello This content will append to the end of the file


二,BufferedWriter写入文件

缓冲字符(BufferedWriter )是一个字符流类来处理字符数据。不同于字节流(数据转换成字节),你可以直接写字符串,数组或字符数据保存到文件。

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy3621')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy3621" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">

package com.yiibai.iofile;
 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
public class WriteToFileExample {
 public static void main(String[] args) {
  try {
 
   String content = "This is the content to write into file";
 
   File file = new File("/users/mkyong/filename.txt");
 
   // if file doesnt exists, then create it
   if (!file.exists()) {
    file.createNewFile();
   }
 
   FileWriter fw = new FileWriter(file.getAbsoluteFile());
   BufferedWriter bw = new BufferedWriter(fw);
   bw.write(content);
   bw.close();
 
   System.out.println("Done");
 
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

</td> </tr> </tbody> </table>

三,FileOutputStream写入文件


文件输出流是一种用于处理原始二进制数据的字节流类。为了将数据写入到文件中,必须将数据转换为字节,并保存到文件。请参阅下面的完整的例子。

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy7108')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy7108" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">

package com.yiibai.io;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class WriteFileExample {
 public static void main(String[] args) {
 
  FileOutputStream fop = null;
  File file;
  String content = "This is the text content";
 
  try {
 
   file = new File("c:/newfile.txt");
   fop = new FileOutputStream(file);
 
   // if file doesnt exists, then create it
   if (!file.exists()) {
    file.createNewFile();
   }
 
   // get the content in bytes
   byte[] contentInBytes = content.getBytes();
 
   fop.write(contentInBytes);
   fop.flush();
   fop.close();
 
   System.out.println("Done");
 
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (fop != null) {
     fop.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}

更新的JDK7例如,www.jiaochengji.com使用新的“尝试资源关闭”的方法来轻松处理文件。

package com.yiibai.io;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class WriteFileExample {
 public static void main(String[] args) {
 
  File file = new File("c:/newfile.txt");
  String content = "This is the text content";
 
  try (FileOutputStream fop = new FileOutputStream(file)) {
 
   // if file doesn't exists, then create it
   if (!file.exists()) {
    file.createNewFile();
   }
 
   // get the content in bytes
   byte[] contentInBytes = content.getBytes();
 
   fop.write(contentInBytes);
   fop.flush();
   fop.close();
 
   System.out.println("Done");
 
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

</td> </tr> </tbody> </table>

 

您可能感兴趣的文章:
Java学习之踏上旅途的第一步
Java开发环境的过去、现在和将来
Java布局管理器深入讨论
Java入门笔记1_HelloWorld
淘宝是php开发的么?
利用 Java 平台的特性建造一个令人瞩目的系统
python与java用途区别有哪些
php和java哪个好?
Java中常用正则表达式
经典Java线程面试题70道

[关闭]
~ ~