java向文件写数据的3种方式
下边列举出了三种向文件中写入数据的方式,当然还有其他方式,帮助自己理解文件写入类的继承关系。类的关系:
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种方式的更多相关文章
- 总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇
本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...
- Java字符流读写数据的两种方式
第一种方式:逐个字符进行读写操作(代码注释以及详细内容空闲补充) package IODemo; import java.io.FileReader; import java.io.FileWrite ...
- 用CSV文件读写数据的两种方式(转)
导读:有时候我们需要对收集的数据做统计,并在页面提供显示以及下载.除了对传统的excel存取之外,对CSV文件的存取也很重要.本文列出了这两种操作的详细代码. 代码: <?php $file = ...
- JAVA读取文件夹大小的几种方式
(一)单线程递归方式 package com.taobao.test; import java.io.File; public class TotalFileSizeSequential { publ ...
- JAVA一个文件写多个类
JAVA一个文件写多个类,并且是同级类,需注意: 在一个.java文件中可以有多个同级类, 其修饰符只可以public/abstract/final/和无修饰符 public修饰的只能有一个,且必须 ...
- 讨论HTTP POST 提交数据的几种方式
转自:http://www.cnblogs.com/softidea/p/5745369.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PU ...
- 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 ...
- Solr 删除数据的几种方式
原文出处:http://blog.chenlb.com/2010/03/solr-delete-data.html 有时候需要删除 Solr 中的数据(特别是不重做索引的系统中,在重做索引期间).删除 ...
- react之传递数据的几种方式props传值、路由传值、状态提升、redux、context
react之传递数据的几种方式 1.父子传值 父传值:<子的标签 value={'aaa'} index={'bbb'}></子的标签> 子接值:<li key={thi ...
随机推荐
- volatile的使用原则
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/4352802.html ...
- S-DES加密
Simplified Data Encryption Standard S-DES 是一个供教学的非安全的加密算法,它与DES的特性和结构类似,但参数小,明文分组为8位,主密钥分组为10位,采用两轮迭 ...
- hdu 2027 统计元音
统计元音 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- jQuery Ajax 实例 全解析
jQuery Ajax 实例 全解析 jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写JavaScript代码的习惯. 废话少说,直接进入正题,我 ...
- OnePlus One(一加1)刷机Kali Nethunter完整教程
设备信息: 设备名称:OnePlus One(一加1) OS:ColorOS 1.2 设备型号:A0001 目标: 在OnePlus One(一加1)上将 ColorOS 1.2 刷机为 Kali N ...
- 本机Font字体
void getFontList() { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); Str ...
- swing容器继承重绘问题解决
swing容器继承重绘问题解决 以JPanel为例,继承JPanel,想动态为器更换背景,这就涉及到重绘问题.一下是本人重写代码: package ui; import java.awt.Grap ...
- iOS 制作发布证书,发布到App Store
---恢复内容开始--- 1.登陆 iOS Dev Center 选择进入iOS Provisioning Portal. 2.在 iOS Provisioning Portal中,点击App IDs ...
- 10.08_逛逛OSC
(1)每天逛逛OSC是我的习惯了. JNative.JACOB.Shrinkwrap API? .Lua.WSO2 Identity Server .JBoss Forge.Bugzilla.Cou ...
- [USACO1.1.4]坏掉的项链Broken Necklace
P1203 [USACO1.1]坏掉的项链Broken Necklace 标签 搜索/枚举 USACO 难度 普及- 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N&l ...