Java 文件写入

fileWrite's time----------36
outputStreamTest's time----------167
bufferedOutputTest's time----------17
bufferedWriteTest's time----------14
bufferedWrite And FileWriterTest's time----------9
bufferedWrite And BufferedOutputStreamTest's time----------12


/**
*1 按字节写入 FileOutputStream
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void outputStreamTest(int count, String str) {
File f = new File("f:test1.txt");
OutputStream os = null;
try {
os = new FileOutputStream(f);
for (int i = 0; i < count; i++) {
os.write(str.getBytes());
}
os.flush();
System.out.println("file's long:" + f.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


/**
*2 按字节缓冲写入 BufferedOutputStream
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void bufferedOutputTest(int count, String str) {
File f = new File("f:test2.txt");
BufferedOutputStream bos = null;
try {
OutputStream os = new FileOutputStream(f);
bos = new BufferedOutputStream(os);
for (int i = 0; i < count; i++) {
bos.write(str.getBytes());
}
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


/**
*3 按字符写入 FileWriter
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void fileWriteTest(int count, String str) {
File f = new File("f:test.txt");
Writer writer = null;
try {
writer = new FileWriter(f);
for (int i = 0; i < count; i++) {
writer.write(str);
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}


/**
*4 按字符缓冲写入 BufferedWriter
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void bufferedWriteTest(int count, String str) {
File f = new File("f:test3.txt");
OutputStreamWriter writer = null;
BufferedWriter bw = null;
try {
OutputStream os = new FileOutputStream(f);
writer = new OutputStreamWriter(os);
bw = new BufferedWriter(writer);
for (int i = 0; i < count; i++) {
bw.write(str);
}
bw.flush();
if(f.exists()){
f.delete();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


/**
*5 按字符缓冲写入 BufferedWriter and BufferedOutputStream
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void bufferedWriteAndBufferedOutputStreamTest(int count, String str) {
File f = new File("f:test4.txt");
BufferedOutputStream bos=null;
OutputStreamWriter writer = null;
BufferedWriter bw = null;
try {
OutputStream os = new FileOutputStream(f);
bos=new BufferedOutputStream(os);
writer = new OutputStreamWriter(bos);
bw = new BufferedWriter(writer);
for (int i = 0; i < count; i++) {
bw.write(str);
}
bw.flush();
if(f.exists()){
f.delete();
System.out.println("delete---");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


/**
*6 按字符缓冲写入 BufferedWriter and FileWriter
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void bufferedWriteAndFileWriterTest(int count, String str) {
File f = new File("f:test5.txt");
FileWriter fw=null;
BufferedWriter bw = null;
try {
fw=new FileWriter(f);
bw = new BufferedWriter(fw);
for (int i = 0; i < count; i++) {
bw.write(str);
}
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
if(f.exists()){
f.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

http://wenku.baidu.com/view/d2bc9ff64693daef5ef73d1e.html
备份。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值