java写文件的三种方式

转自:http://blog.csdn.net/wgw335363240/article/details/5678965


下边列举出了三种向文件中写入数据的方式,当然还有其他方式,帮助自己理解文件写入类的继承关系,分享出来供初学者使用。类的关系:

file->fileoutputstream->outputstreamWriter(FileWriter继承outputstreamWriter对象)

注意:可以参照jdk-API,见http://tool.oschina.net/apidocs/apidoc?api=jdk-zh的java.io包下面的文件读写方法

 

测试代码:

 

 

package com.test.csdn;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.OutputStreamWriter;

 

/**

 * 测试向文件中写文件


 * 

 */

public class TestWirteFile {

 

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

String sContent = "市场调研,2013年,美国平板电脑销售总量将超越台式机";

String sDestFile = "/home/rey/temp/myWrite.txt";

File destFile = new File(sDestFile);

if (!destFile.exists()) {

destFile.createNewFile();

}

 

// 1.向文件写入内容

// writeByFileWrite(sDestFile, sContent);

 

// 2.FileOutputStream向文件写入内容

// writeByFileWrite(sDestFile, sContent);

 

// 2.OutputStreamWriter向文件写入内容

writeByOutputStreamWrite(sDestFile, sContent);

}

 

/**

* 用FileWrite向文件写入内容

* @param _destFile

* @throws IOException

*/

public static void writeByFileWrite(String _sDestFile, String _sContent)throws IOException {

       FileWriter fw = null;

       try {

            fw = new FileWriter(_sDestFile);

            fw.write(_sContent);

       } catch (Exception ex) {

            ex.printStackTrace();

       } finally {

            if (fw != null) {

               fw.close();

               fw = null;

            }

       }

}

 

/**

* 用FileOutputStream向文件写入内容

* @param _destFile

* @throws IOException

*/

public static void writeByFileOutputStream(String _sDestFile, String _sContent) throws IOException {

       FileOutputStream fos = null;

       try {

             fos = new FileOutputStream(_sDestFile);

             fos.write(_sContent.getBytes());

        } catch (Exception ex) {

             ex.printStackTrace();

        } finally {

             if (fos != null) {

                   fos.close();

                   fos = null;

              }

        }

}

 

/**

* 用OutputStreamWrite向文件写入内容

* @param _destFile

* @throws IOException

*/

public static void writeByOutputStreamWrite(String _sDestFile, String _sContent) throws IOException {

           OutputStreamWriter os = null;

           FileOutputStream fos = null;

           try {

                  fos = new FileOutputStream(_sDestFile);

                  os = new OutputStreamWriter(fos, "UTF-8");

                  os.write(_sContent);

             } catch (Exception ex) {

                   ex.printStackTrace();

             } finally {

                   if (os != null) {

                         os.close();

                         os = null;

                    }

                   if (fos != null) {

                        fos.close();

                        fos = null;

                   }

 

               }

}

 

}//end class


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值