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

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

测试代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter; /**
* 测试向文件中写文件
*
* @author lenovo
*
*/
public class TestWirteFile { /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String sContent = "2015年的双十一真的是非常火爆!";
String sDestFile = "F:/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;
} }
} }

java向文件写数据的3种方式的更多相关文章

  1. 总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇

    本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...

  2. Java字符流读写数据的两种方式

    第一种方式:逐个字符进行读写操作(代码注释以及详细内容空闲补充) package IODemo; import java.io.FileReader; import java.io.FileWrite ...

  3. 用CSV文件读写数据的两种方式(转)

    导读:有时候我们需要对收集的数据做统计,并在页面提供显示以及下载.除了对传统的excel存取之外,对CSV文件的存取也很重要.本文列出了这两种操作的详细代码. 代码: <?php $file = ...

  4. JAVA读取文件夹大小的几种方式

    (一)单线程递归方式 package com.taobao.test; import java.io.File; public class TotalFileSizeSequential { publ ...

  5. JAVA一个文件写多个类

    JAVA一个文件写多个类,并且是同级类,需注意: 在一个.java文件中可以有多个同级类,  其修饰符只可以public/abstract/final/和无修饰符 public修饰的只能有一个,且必须 ...

  6. 讨论HTTP POST 提交数据的几种方式

    转自:http://www.cnblogs.com/softidea/p/5745369.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PU ...

  7. Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式

    Linux就这个范儿 第15章 七种武器  linux 同步IO: sync.fsync与fdatasync   Linux中的内存大页面huge page/large page  David Cut ...

  8. Solr 删除数据的几种方式

    原文出处:http://blog.chenlb.com/2010/03/solr-delete-data.html 有时候需要删除 Solr 中的数据(特别是不重做索引的系统中,在重做索引期间).删除 ...

  9. react之传递数据的几种方式props传值、路由传值、状态提升、redux、context

    react之传递数据的几种方式 1.父子传值 父传值:<子的标签 value={'aaa'} index={'bbb'}></子的标签> 子接值:<li key={thi ...

随机推荐

  1. oracle根据pid查询出正在执行的执行语句

    今天数据库访问突然很慢,通过top命令发现oracle的cpu使用率很高.同事建议查询一下看看是什么语句导致的oracle运行变慢.于是从网上查了一下,可以根据pid查询出正在执行的查询语句,发现是一 ...

  2. java.sql.SQLException: ORA-28001: the password has expired。

    java.sql.SQLException: ORA-28001: the password has expired. Oracle11g的密码过期. 原因:是由于oracle11g中默认在defau ...

  3. Bootstrap--组件之下拉菜单

    用于显示链接列表的可切换.有上下文的菜单. 对齐 B默认情况下,下拉菜单自动沿着父元素的上沿和左侧被定位为 100% 宽度. 为 .dropdown-menu 添加 .dropdown-menu-ri ...

  4. java内存分块

     运行时数据区域 Java虚拟机在执行Java的过程中会把管理的内存划分为若干个不同的数据区域.这些区域有各自的用途,以及创建和销毁的时间,有的区域随着虚拟机进程的启动而存在,而有的区域则依赖线程的启 ...

  5. web开发学习之旅---html第二天

    一.转义符 一些字符在html中拥有特殊的含义,比如小于号(<)用于定义 HTML 标签的开始.如果我们希望浏览器正确地显示这些字符,我们必须在 HTML 源码中插入转义符. 分类 二.html ...

  6. SignalR: The new old thing

    As you can see, this is my first blog posted in cnblog. If you find any mistake, don’t hesitate to t ...

  7. 20160418javaweb之 Filter过滤器

    Servlet规范中 Servlet Listener Filter 1.开发Filter 想要开发一个过滤器需要如下两个步骤: (1)写一个类实现特定的接口Filter 生命周期:当服务器启动时,w ...

  8. 利用Merge生成或更新新记录

    -- ============================================= -- Author: <华仔> -- Create date: <2016,6,7& ...

  9. 获取汉字拼音 Java

    两种方法:一个是使用btye数组,一个是引入jar包进行操作. 1. public class CharacterParser { private static int[] pyvalue = new ...

  10. javee学习-通过ServletContext对象实现数据共享

    1,设置值. ServletContext context = this.getServletConfig().getServletContext();//获得ServletContext对象 // ...