缓冲
来源:互联网 发布:数据库数字类型 编辑:程序博客网 时间:2024/04/28 12:44
package org.java.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
/**
* Buffered是流的一个缓冲技术,主要用于来提高, 流的读写的效率,
* 底层的实现,也就是封装了一个数组的对象来操作
* ,既然是对流的一个高效,性能的优化,所以要先有流,后又缓冲
*/
public static void main(String[] args) {
copy1("d:\\test.txt", "f:\\tt.txt");
}
public static void copy(String srcFileName, String targetFileName) {
BufferedReader reader=null;
BufferedWriter writer=null;
int len=0;
char[] chars=new char[1024];
File srcFile=new File(srcFileName);
if(!srcFile.exists())
return;
try {
reader=new BufferedReader(new FileReader(srcFile));
File targetFile=new File(targetFileName);
if(!targetFile.exists())
try {
targetFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer=new BufferedWriter(new FileWriter(targetFile));
while((len=reader.read(chars))!=-1){
writer.write(chars, 0, len);
//注意在用缓存技术的时候,每次必须刷新
writer.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(writer!=null)
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void copy1(String srcFileName, String targetFileName) {
BufferedReader reader=null;
BufferedWriter writer=null;
String buf=null;
File srcFile=new File(srcFileName);
if(!srcFile.exists())
return;
try {
reader=new BufferedReader(new FileReader(srcFile));
File targetFile=new File(targetFileName);
if(!targetFile.exists())
try {
targetFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer=new BufferedWriter(new FileWriter(targetFile));
while((buf=reader.readLine())!=null){
writer.write(buf);
writer.newLine();
//注意在用缓存技术的时候,每次必须刷新
writer.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(writer!=null)
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
/**
* Buffered是流的一个缓冲技术,主要用于来提高, 流的读写的效率,
* 底层的实现,也就是封装了一个数组的对象来操作
* ,既然是对流的一个高效,性能的优化,所以要先有流,后又缓冲
*/
public static void main(String[] args) {
copy1("d:\\test.txt", "f:\\tt.txt");
}
public static void copy(String srcFileName, String targetFileName) {
BufferedReader reader=null;
BufferedWriter writer=null;
int len=0;
char[] chars=new char[1024];
File srcFile=new File(srcFileName);
if(!srcFile.exists())
return;
try {
reader=new BufferedReader(new FileReader(srcFile));
File targetFile=new File(targetFileName);
if(!targetFile.exists())
try {
targetFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer=new BufferedWriter(new FileWriter(targetFile));
while((len=reader.read(chars))!=-1){
writer.write(chars, 0, len);
//注意在用缓存技术的时候,每次必须刷新
writer.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(writer!=null)
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void copy1(String srcFileName, String targetFileName) {
BufferedReader reader=null;
BufferedWriter writer=null;
String buf=null;
File srcFile=new File(srcFileName);
if(!srcFile.exists())
return;
try {
reader=new BufferedReader(new FileReader(srcFile));
File targetFile=new File(targetFileName);
if(!targetFile.exists())
try {
targetFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer=new BufferedWriter(new FileWriter(targetFile));
while((buf=reader.readLine())!=null){
writer.write(buf);
writer.newLine();
//注意在用缓存技术的时候,每次必须刷新
writer.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(reader!=null)
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(writer!=null)
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- 缓冲
- 缓冲
- 缓冲
- 缓冲
- 缓冲
- 缓冲与非缓冲
- 缓冲与非缓冲
- php缓冲--ob缓冲
- 行缓冲、全缓冲、无缓冲
- 后备缓冲
- 文件缓冲
- 缓冲机制
- 顶点缓冲
- 缓冲输出
- 缓冲策略
- 缓冲流
- 帧缓冲
- 页面缓冲
- 用OleDbParameter参数类要注意参数顺序必须和SQL文中参数顺序一致
- 设计模式之15组合模式(笔记)
- io拷贝
- 试译雷神的微软平台安全宝典第二章 简介和RSA章节
- ant和yuicompressor 压缩css、js方案
- 缓冲
- 设计模式之16观察者模式(笔记)
- SQL Server 2005 导入数据 请注意事项
- 每天一本书不再是问题——快速阅读
- 开发snmp rddtools
- 嵌入式Linux的GDB调试环境建立
- windows 进程间通信(共享内存)
- typedef struct _DRIVER_OBJECT
- 设计模式之17门面模式(笔记)