java向文件写数据的3种方式
2010-06-18 19:53
330 查看
下边列举出了三种向文件中写入数据的方式,当然还有其他方式,帮助自己理解文件写入类的继承关系,分享出来供初学者使用。类的关系:
file->fileoutputstream->outputstreamWriter(FileWriter继承outputstreamWriter对象)
测试代码:
package com.test.csdn;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* 测试向文件中写文件
*
* @author rey
*
*/
public class TestWirteFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String sContent = "市场调研公司Forrester Research(以下简称“Forrester”)的分析师分析师莎拉·罗特曼-埃普斯(Sarah Rotman Epps)周四发布报告称,今年美国市场平板电脑销量将达到350万台。到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;
}
}
}
}
file->fileoutputstream->outputstreamWriter(FileWriter继承outputstreamWriter对象)
测试代码:
package com.test.csdn;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* 测试向文件中写文件
*
* @author rey
*
*/
public class TestWirteFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String sContent = "市场调研公司Forrester Research(以下简称“Forrester”)的分析师分析师莎拉·罗特曼-埃普斯(Sarah Rotman Epps)周四发布报告称,今年美国市场平板电脑销量将达到350万台。到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;
}
}
}
}
相关文章推荐
- java向文件写数据的3种方式
- java向文件写数据的3种方式
- java加载文件的3种方式
- java对象数据转xml文件(加<![CDATA[]]>)[注解方式]
- JAVA xml 流方式读取。数据挖掘大文件预处理。
- Java基础---Java---网络编程---TCP、UDP、UDP-键盘录入方式数据、Socket、TCP复制文件、UDP-聊天
- 【Java基础知识】IO类--字符流读写数据以及复制文件的几种方式
- Java基础---Java---网络编程---TCP、UDP、UDP-键盘录入方式数据、Socket、TCP复制文件、UDP-聊天
- 【Java基础知识】IO流--字节流读写数据以及复制文件的几种方式
- java海量大文件数据处理方式
- java 读取文件——按照行取出(使用BufferedReader和一次将数据保存到内存两种实现方式)
- 大数据Java-交换变量的3种方式
- Android数据保存的3种方式 SharedPreferences存储、文件存储、数据库存储SQLite 未完
- Java对象数据存取的3种方式
- java存取二进制数据到xml文件
- logstash快速入门 (这篇文章很不错 ) | 两种方式往logstash传输数据实例:Apache 日志(从文件获取)、Syslog方式
- 五种方式让你在java中读取properties文件内容不再是难题
- Java进阶(四十二)Java中多线程使用匿名内部类的方式进行创建3种方式
- resetlogs方式打开数据库,某数据文件需要recover,恢复失败的处理方式
- Java解析Xml文件—判断Xml文件的节点是否存在子节点_以及对节点下不同子节点的内容解析方式