当前位置: 移动技术网 > IT编程>开发语言>Java > java写入文件的几种方法分享

java写入文件的几种方法分享

2019年07月22日  | 移动技术网IT编程  | 我要评论

一,filewritter写入文件

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

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

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

复制代码 代码如下:

new filewriter(file,true);

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

abc hello追加新内容 new filewriter(file,true)

复制代码 代码如下:

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();
     }
    }
}

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

abc hello this content will append to the end of the file


二,bufferedwriter写入文件

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

复制代码 代码如下:

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();
  }
 }
}

三,fileoutputstream写入文件


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

复制代码 代码如下:

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例如,使用新的“尝试资源关闭”的方法来轻松处理文件。

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();
  }
 }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网