字节型文件流&字符型文件流

file对象不能操作文件的内容---->通过流I/O的方式来完成

  • 1.流按照方向(功能)来区分
    in(读取)out(写入)
  • 2.操作的目标来区分
    文件流 数组流 字符串流 数据流 对象流 网络流…

一、文件流

顾名思义:读取文件中的信息in;将信息写入文件中out。
文件流按照读取或写入的单位(字节数)大小来区分:分为:
字节型文件流(1字节):FileInputStream/FileOutputStream
字符型文件流(2字节——1字符):ileReader/FileWriter

为什么一定要将数据存在文件中?

  • 1.变量 只能存一份
  • 2.数组 存储好多个 数据类型统一
  • 3.集合 存储好多个 存储后个数还能改变 泛型—数据类型统一
    如上三个都是java中的类型(对象——>内存)
    都存储在内存中,程序执行完毕,虚拟机停止的时候,内存空间就回收啦。
    数据都是临时性存储的。
  • 4.文件,存储好多信息。
    文件是存储在硬盘上的——>永久性保存
    数据虽然是安全了,
    文件毕竟不在内存中,需要通过IO操作文件。

二、字节型文件输入流 FileInputStream

  • 1.包 java.io。
  • 2.了解一下继承关系 InputStream类 字节型输入流的父类。
  • 3.创建对象
    调用一个带File类型的构造方法
    调用一个带String类型的构造方法
  • 4.常用方法
    int code=read(); 每次从流管道中读取一个字节,返回字节的code码。
    **int count=read(byte[] );**每次从流管道中读取若干个字节存入数组内,返回有效元素个数。
    int count=available(); 返回柳管道中还有多少缓存的字节数。
    xkip(long n); 跳过几个字节,再读取
    多线程——>利用几个线程同时读取文件
    10000字节,5个小人同时读取。
    1-2000 2001-4000 4001-6000 6001-8000 8001-10000
    D当做服务器 E当做客户端
    close(); 将流管道关闭——必须要做 最好放在finally里 注意代码的健壮性 判断严谨
File  file=new File("D://test//Test.txt");//创建一个文件对象
//只是创建一个对象  ,所以就算路径错误,对象也能创建出来,只不过是无法和硬盘对应而已。
FileInputStream fis=new  FileInputStream(File);//真实去读文件
//但是这句代码要求你真实去读文件,所以这一步就必须真实存在那个文件了。
//系统认为,你所写的文件可能并不是真实存在的,或者路径写错了,所以这一步必须处理异常,改写下面的代码。

处理文件需要改写为:

try{
	//创建一个字节型的文件输入流  读取一个文件中的内容
	File  file=new  File("D://test//Test.txt");
	FileInputStream  fis=new  FileInputStream(file);//真实去读文件
}catch(fileNotFoundException e){//编译时异常
	e.printStackTrace();
}

真正处理一个文件试一下:

try{//理解为  文件是一个仓库  fis对象搬运工  推一个平板车
	//创建一个字节型的文件输入流  读取一个文件中的内容
	File  file=new  File("D://test//Test.txt");
	FileInputStream  fis=new  FileInputStream(file);//真实去读文件
	int code=fis.read();//读取一个字节返回字节  没有读到就返回-1
	while(code!=-1){
		System.out.println((char)code);//读取的字节对应的unicode码  0---65535
		code=fis.read();//读取一个字节
	}
}catch(FileNotFoundException e){//编译时异常
	e.printStackTrace();
}catch(IOException e){
	e.printStackTrace();
}
//这是一个字节一个字节的读  有点慢  我们可以用小推车去读取文件

利用第二种方式读取文件

try{//流---管道
	FileInputStream  fis=new  FileInputStream("D://test//Test.txt");
	int v=fis.available();//流管道中有多少缓存的字节  读取网络数据可能会有问题
	System.out.println(v);
	//创建一个空的数组---->小推车
	byte[]  b=new  byte[5];  //数组多大自己定
	int count=fis.read(b);//去文件里读取东西  装入数组内  读取到的有效字节个数
	while(count!=-1){
		String value=new  String(b,0,count);//第一次  abcde
		System.out.println(value);//第二次  fg  \r  \n  h
		count=fis.read(b);  //第三次   ijklm    //第四次  n  \r  \n  o  p   //第五次 q  \r  \n  o  p   count---->1  此时的数组5个元素
	}
}catch(IOException e){
	e.printStackTrace();
}

关闭文件的例子:

FileInputStream  fis=null;
	try{
		fis=new  FileInputStream(new  File("D://test//Test.txt"));
		fis.skip(5);
		int code=fis.read();
		System.out.println((char)code);
	}catch(IOException  e){
		e.printStackTrace();
	}finally{
		try{
			if(fis!=null){
				fis.close();//关闭的是流通道  不是file对象  关闭这件事情必须要做
			}
		}catch(IOException  e){
			e.printStackTrace();
		}
	}

三、字节型文件输出流

FileOutputstream,将数据写入文件中。

  • 1.java.io
  • 2.继承OutputStream 所有字节型输出流的父类
  • 3.创建对象
    调用一个带File参数 还有File boolean重载
    调用一个带String参数 还有String boolean重载
    File file=new File(“D://test//aaa.txt”);
    创建的是文件输入流,若文件路径有问题,则抛出异常 FileNotFoundException;
    创建的是文件输出流,若文件路径有问题,则直接帮我们创建一个新的文件。
    4.常用方法
    write(int code); 将给定code对应的字符写入文件‘=’
    write(byte[] ); 将数组中的全部字节写入文件 getByte();
    flush(); 将管道内的字节推入(刷新)文件
    close(); 注意在finally中关闭
try{
	//创建一个字节型文件输出流
	File  file=new File("D://test//aaa.txt");
	//创建的是文件输入流  若文件路径有问题  则抛出异常  FileNotFoundException
	//创建的是文件输出流  若文件路径有问题  则直接帮我们创建一个新的文件
	//将内容写入文件
	FileOutputstream  fos=new  FileOutputStream(file, true);
	//true表示append追加是true
	fos.write(49);//a   a是写入在哪里了?  1+1=2
	fos.write('+');
	fos.write(49);
	fos.write('=');
	fos.write(50);
	fos.flush(0);//刷新  将管道中的字节  推入文件中
	System.out.println("写入完毕");
}catch(IOException  e){
	e.printStackTrace();
}

四、小任务

  • 1.设计一个方法 文件的复制
    C://test//aaa.txt------>D://某一个位置
    aaa.txt中的内容是:abcdefghigk。
    找到C盘中的源文件;内容读一遍;
    去D盘中创建一个新文件;将内容写进去。
//设计一个方法  文件的复制
public void copyFile(File file,String  path){
	FileInputStream fis=null;
	FileoutputStream fos=null;
	try{
		//创建一个输入流
		fis=new  FileInputStream(file);
		//创建一个新的file对象
		File  newFile=new File(path+"//"+file.getName());
		fos=new FileOutputStream(newFile);
		//读取文件 中的信息
		byte[]  b=new byte[1024];//通常创建的数组  1kb--8kb之间
		int  count=fis.read(b);//1024  21有效的
		while(count!=-1){
			//做点手脚
			fos.write(b,0,count);
			fos.flush();
			count=fis.read(b);
		}
		System.out.println("复制完毕");
	}catch(IOException  e){
		e.printStackTrace();
	}finally{
		//关闭
		try{
			if(fis!=null){
				fis.close();
			}
		}catch(IOException  e){
			e.printStackTrace();
		}
		try{
			if(fos!=null){
				fos.close();
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	public static void main(String[] args){
		OperateFile  of=new  OperateFile();
		of.copyFile(new  File("c://js实现照片墙.zip"),"D://test");
	}
}
  • 2.扩展设计一个方法 文件的加密/解密
    读取内容
    中间做一个小算法(加密) 可逆的
    写进去
//设计一个方法   文件的复制
    public void jiaMiFile(File file,String path){//当做file是一个文件  C://Test.txt
        FileInputStream fis = null; //为了关闭文件处理需要
        FileOutputStream fos = null;//为了关闭文件处理需要
        try {
            //创建一个输入流
            fis = new FileInputStream(file);
            //创建一个新的file对象
            File newFile = new File(path+"//"+file.getName());
            fos = new FileOutputStream(newFile);
            //读取文件中的信息
            byte[] b = new byte[1024];//通常创建的数组  1kb--8kb之间
            int count = fis.read(b);//1024  21有效的
            while(count!=-1) {
                //做点手脚   每一次数组的前两个元素位置互换 1024
                byte temp = b[0];
                b[0] = b[1];
                b[1] = temp;
                fos.write(b,0,count);//将读取到的有效字节 写入
                fos.flush();
                count = fis.read(b);
            }
            System.out.println("加密完毕");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭
            try {
                if(fis!=null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fos!=null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //deleteFile(file);

	public static void main(String[] args){
        OperateFile of = new OperateFile();      
        of.jiaMiFile(new File("C://js实现照片墙.zip"),"D://test");   
        }
        //加密之后可能D盘中文件就可能打不开了,在运行一遍就交互回来了,就解密了。
    }

五、字符型文件流

  • FileReader ; FileWriter
  • 只能操作纯文本文件(文件右键打开方式 记事本打开 能看懂)
  • .txt
        try {
            //演示字节型文件输入流 读取中文可能会产生问题
            //字节流好处在于什么类型的文件都可以处理  不好在于处理纯文本的文件可能会产生乱码的问题
            FileInputStream fis = new FileInputStream("D://test//Test.txt");
            byte[] b = new byte[5];
            int count = fis.read(b);// 16bit
            while(count!=-1){
                System.out.print(new String(b,0,count,"GBK"));
                count = fis.read(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Test.txt中的内容是汉字:我abc
但是如果汉字在中间,读取中文的时候是可能产生问题的。

和字节型文件流处理一样类似。

  • FileReader
    1.java.io包
    2.继承 InputStreamReader Reader
    3.构造方法
    4.常用
    read()
    read(char[])
  • FileWriter
    1.java.io包
    2.继承 OutputStreamWriter Writer
    3.构造方法
    带file参数 带file,boolean参数
    带String参数 带String,boolean参数
    4.常用
    write(int)
    write(char[])
    write(string)
    flush close
  • 举个例子:
try {
            //字符型文件输入流
            File file = new File("D://test//Test.txt");
            FileReader fr = new FileReader(file);
            String str = "abc";
            char[] c = str.toCharArray();
            FileWriter fw = new FileWriter(file);
            fw.write(97);
            fw.write(c);
            fw.write(str);
            fw.flush();
             
//            int code = fr.read();
//            System.out.println(code);
//            char[] c = new char[1024];
//            int count = fr.read(c);
//            while(count!=-1){
//                System.out.print(new String(c,0,count));
//                count = fr.read(c);
//            }

	} catch (IOException e) {
            e.printStackTrace();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值