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

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. maven项目在tomcat中运行遇到的问题

    在使用maven构建项目,并在tomcat容器中运行的时候遇到了一些问题,现做一下记录 maven项目中jdk版本会自动恢复 maven项目的编译jdk即使在window -> java -&g ...

  2. C# Quartz.Net 定时任务的简单使用

    最近做了一个定时执行任务的软件. 执行任务时,会使用log4net记录日志,如果执行任务有异常,则发送邮件给指定的对象. 我做的是每天的9点和16点执行一次任务,以此记录: 首先,获得Quartz.N ...

  3. How to fix “The program can’t start because MSVCR110.dll is missing from your computer.” error on Windows

    原文出处 How to fix “The program can’t start because MSVCR110.dll is missing from your computer.” error ...

  4. ACM——圆柱体的表面积

    lems 1092 圆柱体的表面积 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:2697            测试通过:414 ...

  5. cordova 创建ios项目

    cordova create fmscmsios1023 com.weilian.fmscms fmscms cd fmscms cd fmscmsios1023/ cordova platforms ...

  6. 是么是 API 和 SDK

    API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码 ...

  7. java新手笔记18 类比较

    1.Shap类 package com.yfs.javase; public class Shape /*extends Object */{ //默认继承object object方法全部继承 // ...

  8. html页面布局 第8节

    页面布局: <html> <head> <title>页面布局</title> <style type="text/css"& ...

  9. (转)IOS学习笔记-2015-03-29 int、long、long long取值范围

    unsigned - - unsigned - - unsigned __int64的最大值: __int64的最小值:- unsigned __int64的最大值:  

  10. Headfirst设计模式的C++实现——工厂方法(Factory Method)

    引用原书的一句话:所有的工厂模式都用来封装对象的创建,工厂方法模式通过让子类决定该创建的对象是什么来达到封装的目的. Pizza类及其派生类与上一例相同 PizzaStore.h #ifndef _P ...