1,BufferedInputStream字符输入缓冲流
* BufferedInputStream:字符输入缓冲流,成为处理流(高级流)
* 它是字节流包装,所以不能直接的去操作数据源,只能封装对应的低级流
* 读取数据的时候不会直接的去读取对应的文件,首先会到对应的缓存中去找数据
* 如果缓存中没有数据然后去调用被它封装的低级流的read()方法去获取文件的数据
1-1声明输入流
static BufferedInputStream bis = null;
bis = new BufferedInputStream(is1);
1-2,读取:read()
int d = 0;
while((d = bis.read())!=-1){
System.out.print((char)d);
}
1-3,关闭输入流close()
Bis.close();
2,BufferedOutputStream输出缓冲流
2-1,复制文件
File file = new File("e:"+File.separator+"a.txt");
File file1 = new File("e:"+File.separator+"复制a.txt");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//用字节缓冲流复制对象
public void copyFile(){
//创建一个字节输出流
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(new FileOutputStream(file1));
//跳过多少个字节,一个汉字两个字节
bis.skip(1);
//定义一个字节数组,用来装数据
byte[] b = new byte[1024];
int len = 0;
while((len = bis.read(b))!=-1){
bos.write(b, 0, len);
//刷新缓存,只要用到buffer的都要进行刷新
bos.flush();
}
}
2-2,测试手动的控制文件的指针mark()
BufferedInputStream bis = new BufferedInputStream(newFileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
int b =0;
int c = 0;
int k = 0;
bis.mark(0);//把文件的指针记录到文件的开始处
while((b = bis.read())!=-1){
//总共重复指向mark()20次
if(k<20){
if(c<=5){
bos.write(b);
bos.flush();
c++;
}else{
//重置文件的指针为上一次记录处
bis.reset();
k++;
c=0;
}
}
}
3,输入流InputStream
public class TestInputStreanDemo {
//声明一个文件输入流
InputStream is = null;
//创建一个文件对象
File file = new File("e:"+File.separator+"a.txt");
//获取文件数据的方法
public void getDate(){
//创建一个字节文件输入流
is = new FileInputStream(file);
//读取一个字节,返回是int类型数据
int d = is.read();
//读取汉字会乱码,因为一个汉字是两个字节
System.out.println(d);
if(is !=null){
//关闭资源
is.close();
}
}
//获取一个文件全部数据
public void getAllDate(){
//创建一个文件输入流
is = new FileInputStream(file);
//定义一个int型数据接收
int data = 0;
//如果文件没有读则data就对应那个字符的编码,如果读取到结尾,则返回-1
while((data = is.read())!=-1){
System.out.print((char)data);
}
//转码后的输出,在这个平台上可以打印出汉字,不会是乱码
byte[] b = new byte[1024];
while((data = is.read(b))!=-1){
//一次读出1024个字节放在数组b[]内
System.out.print(new String(b,0,data));
}
if(is!=null){
//关闭资源
is.close();
}
}
//使用read(byte[] b)方法读取
public void getAllDate01(){
try {
//创建一个字符流文件
is = new FileInputStream(file);
//定义一个byte数组存放一次读取的字节数
byte [] b = new byte[1024];
//len 记录当前流的指针指向的位置,读取完毕后为-1
int len = 0;
while((len = is.read(b))!=-1){
//打印字符
String s = new String(b,0,len);
System.out.print(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
//以后把释放资源的代码都放到finally块内最安全
}finally{
try {
if(is!=null){
//关闭资源
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4,输出流OutputStream
* 默认的字节输出流对象每次会把指定的文本的数据覆盖
* 如果在创建字节输出流的时候,为该构造器指定第二个参数,如果为true则在文本最后追加
* 如果为false则覆盖.默认时为false.
4-1,在文件内写九九乘法表
public void Test(File file){
try {
//创建一个文件输出流
os = new FileOutputStream(file);
//往指定的文件中写入数据
StringBuffer str = new StringBuffer();
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
//添加进缓存中去
str.append(j+"*"+i+"="+(j*i)+" ");
}
//添加换行
str.append("\r\n");
}
//把stringbuffer转变成string
String msg = str.toString();
//把string转换成数组
byte[] b = msg.getBytes();
//把字节数组写入输出流中
os.write(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//判断该资源文件是否为空
if(os!=null){
//关闭资源
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
5,copy文件方法
* copy文件
* 步骤
* 1.创建源文件对象
* 2.创建目标文件对象
* 3.创建字节输入流
* 4.创建字节输出流
* 5.关闭资源
注释:获取中文和英文的方法
*如果只要中文
* if(data>128){
* os.write(data);}
* 如果只要英文
* if(data<=128){
* os.write(data);}
//创建源文件
static File strfile = new File("e:"+File.separator+"116.jpg");
//创建目标文件
static File copyfile = new File("e:"+File.separator+"100.jpg");
//创建字节输入流
InputStream is = null;
//创建字节输出流
OutputStream os = null;
//创建方法体
public void CopyAllFile(File strfile,File copyfile){
try {
//创建文件输入流
is = new FileInputStream(strfile);
//创建文件输出流
os = new FileOutputStream(copyfile);
//操作指定的文件
//定义一个int类型变量接受每次输入的字节
int data = 0;
//循环读出源文件中的字节
while((data = is.read())!=-1){
//把每次读出的字节写入到指定的文件
os.write(data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//关闭输入流资源
if(is!=null){
is.close();
}
//关闭输出量资源
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
6,拷贝某个文件夹内的文件,以及内容
* 拷贝目录和目录内的文件
public class copy {
InputStream is = null;
OutputStream os = null;
//创建寻找文件的方法
public void test(File file,File copyfile){
//判断是不是目录
if(file.isDirectory()){
//创建该目录
copyfile .mkdirs();
//获取当前目录下的子目录或子文件
File[] list = file.listFiles();
//判断该数组的长度是否大于0
if(list.length>0){
int i = 0;
//判断数组长度,使的数组内的元素都可以被遍历到
while(i<list.length){
//遍历list数组
for(File f:list){
if(f.isDirectory()){
//如果是目录,就先创建此目录
File f1 = new File(copyfile,f.getName());
f1.mkdirs();
//递归检测是否子集还是目录或文件
test(f,f1);
}else{
try {
//如果不是目录,创建文件
File f2 = new File(copyfile,f.getName());
f2.createNewFile();
//复制文件内的内容
CopyAllFile(f,f2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
i++;
}
}
}
}
//创建拷贝文件的方法体
public void CopyAllFile(File strfile,File copyfile){
try {
is = new FileInputStream(strfile);
os = new FileOutputStream(copyfile);
int data = 0;
byte[] b = new byte[is.available()];
while((data = is.read(b))!=-1){
os.write(b,0,data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//关闭输入流资源
if(is!=null){
is.close();
}
//关闭输出量资源
if(os!=null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//创建主函数
public static void main(String[] args) {
File file = new File("e:"+File.separator+"abc");
//创建目标文件
File copyfile = new File("e:"+File.separator+"abc001");
copy dre = new copy();
//dre.testDelete(file);
dre.test(file,copyfile);
}
}
1,BufferedReader字符缓冲流
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter bw = new BufferedWriter(new FileWriter("e:"+File.separator+"aaaaa.txt"));
//readLine()一次读取一行,不换行打乱原来的样子
String msg = null;
while((msg = br.readLine())!=null){
bw.write(msg);
//每写一次就换行
bw.newLine();
//都达不到换行的目的
//bw.write("\n");
//System.out.println();
}
2,输入/输出流体系
3,DataOutputStream
//声明一个数据输出字节流
DataOutputStream dos = null;
DataInputStream dis = null;
File file = new File("e:"+File.separator+"data.txt");
dos = new DataOutputStream(new FileOutputStream(file));
dis = new DataInputStream(new FileInputStream(file));
dos.writeInt(12);
dos.writeInt(13);
dos.writeDouble(12.3);
dos.writeFloat(2.3f);
dos.writeUTF("呵呵");
dos.flush();
//读取文件必须按顺序获取,要不然不是想要的数据
int i = dis.readInt();
int i1 = dis.readInt();
double b = dis.readDouble();
System.out.println(i+"----"+i1);
4,转换流
public class Testget {
File file = new File("e:"+File.separator+"西雅图001.txt");
//声明一个转换器
InputStreamReader isr = null;
//声明一个字符输入缓冲流
BufferedWriter bw = null;
BufferedReader br = null;
//获取指定格式的日期
public String getFormatDate(){
String str = null;
SimpleDateFormat simple = new SimpleDateFormat("[yyyy-MM-dd HH-mm-ss ] ");
str = simple.format(new Date());
return str;
}
public void getDate(){
//创建一个指向控制台的字符输入转换器
isr = new InputStreamReader(System.in);
//创建一个字符输入流
br = new BufferedReader(isr);
//创建一个文件字符输出缓冲流
try {
String msg = null;
bw = new BufferedWriter(new FileWriter(file));
while((msg = br.readLine())!=null){
//判断用户是否退出系统
if("quit".equalsIgnoreCase(msg)){
System.exit(0);
}
//拼接格式
msg = getFormatDate()+msg;
//把文件写入文件
bw.write(msg);
//换行
bw.newLine();
//刷新
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//关闭资源
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Testget().getDate();
}
}
5,writer
* 输出流会在文件不存在时自动创建前提是父类文件存在
* 如果该文件的父目录也不存在就会抛出异常,也就是不会自动创建父目录
Writer w = new FileWriter(file);
char [] c = {'美','女','你','好','喔'};
//把字符数组写入文件内
w.write(c);
//把数字写入文件内
w.write(65);