Java--IO流详解

目录

一、IO流的介绍

二、IO流的具体详解

(1)字节流

1:FileOutputStream

2:FileInputStream

(2)字符流

1:解码编码

 2:FileReader

3:FileWriter

三、IO流的使用

(1)文件拷贝

(2)拷贝文件夹

(3)加密和解密文件


一、IO流的介绍


IO流是用于读写文件中的数据(可以读写文件,或网络中的数据...),IO流中流的方向分为输入流(读取文件)和输出流(往文件中写入数据);操作文件类型分为字节流(操作所有类型的文件)
和字符流(操作纯文本文件),不仅仅如此,字节流的读取都是一个字节一个字节的读取,所以一般用它读取英文,字符流是一次读取多个字节,具体几个字节看是英文还是中文以及编码方式了(对于字节这些不是很理解的同学可以看一下这篇文章  :  字符集详解

二、IO流的具体详解

(1)字节流

FileInputStream:字节输入流(读取文件中的数据)
FileOutputStream:字节输出流(往文件中写入数据)

1:FileOutputStream

字符输出流的使用:
1、创建对象
2、写入数据
3、释放资源(如果不释放资源的话,对于操作的文件会被Java给占用,那么你想对他做一些其它的操作就无法进行,如删除文件操作)

字符输出流的细节:
1:参数是路径或者是File对象
2:如果文件不存在则会创建一个新的文件,但是要保证父级路径存在
3:如果文件存在的话,会清空文件内容,不想清空,在创建对象时第二个参数可以写true

4:write方法的参数是整数,但实际写到文件上的是整数对应的ASCII字符



如果先要换行的话再次写出一个换行符就可以了
windows:\r\n
Linux:\n
Mac:\r
public class Fu{
	public static void main(String[] args) throws IOException {
		FileOutputStream s=new FileOutputStream("D:\\java\\7.txt");
		s.write(97);
		s.close();
	}
}

这里我们一般都不清楚ASCII值对应的字符,所以能不能想写什么不需要知道对应的ASCII呢(这里说的是英文,中文使用字符流)

	public static void main(String[] args) throws IOException {
		FileOutputStream s=new FileOutputStream("D:\\java\\7.txt",true);
		String k="klkl";
		byte[] b=k.getBytes();
		String kk="\r\n";
        byte[] bb=kk.getBytes();
        s.write(bb);//换行
       	s.write(b);
		s.close();
		
		
	}

2:FileInputStream

输入流就是查看文件中的数据

public static void main(String[] args) throws IOException {
	  FileInputStream s=new FileInputStream("D:\\java\\7.txt");
	  int c;
	  while((c=s.read())!=-1) {
		  System.out.print((char)(c));
	  }
	  s.close();
   }



(2)字符流

1:解码编码

编码:
public byte[] getBytes()                                使用默认方式进行编码,使用idea默认是 
                                                        用的方式是utf-8,使用eclipse默认是用 
                                                        的是gbk
public byte[] getbytes(String charsetName)              使用指定方式进行编码
解码:
String(byte[] bytes)                                    使用默认方式进行解码
String(byte[] bytes,String charsetName)                 使用指定方式进行解码
public static void main(String[] args) throws IOException {
		String s="as你好啊,牛牛";
		byte[] b=s.getBytes();
		System.out.println(Arrays.toString(b));
		
		String ss=new String(b);
		System.out.println(ss);
	}

 2:FileReader

1:创建对象
2:读取数据
3:释放资源
public static void main(String[] args) throws IOException {
		FileReader s=new FileReader("D:\\java\\7.txt");
		int c;
		while((c=s.read())!=-1) {
			System.out.print((char)(c));
		}
		s.close();
	}

3:FileWriter

public static void main(String[] args) throws IOException {
		FileWriter s=new FileWriter("D:\\java\\7.txt");
		s.write("你是是的呢");
		s.close();
	}

三、IO流的使用

(1)文件拷贝

核心思想就是边读边写,这个代码适合小文件。

public static void main(String[] args) throws IOException {
		FileInputStream s=new FileInputStream("D:\\java\\7.txt");
		FileOutputStream ss=new FileOutputStream("D:\\java\\8.txt");
		
		int c;
		while((c=s.read())!=-1) {
		  ss.write(c);
		}
	}

大文件可以这样写:

public static void main(String[] args) throws IOException {
		FileInputStream s=new FileInputStream("D:\\java\\7.txt");
		FileOutputStream ss=new FileOutputStream("D:\\java\\8.txt");
		int len;//每一次读取的数据的长度
		byte[] b=new byte[1024*1024*5];
		while((len=s.read(b))!=-1) {
			ss.write(b,0,len);//从0位置读取,读取len个数据
		}
		ss.close();//注意释放资源的顺序哦,先开的后关
		s.close();
	}

(2)拷贝文件夹

注意哈,这里是拷贝文件夹,不是拷贝文件,所以文件夹里的文件和文件夹也是要拷贝的哦!

public static void main(String[] args) throws IOException {
		File src=new File("D:java\\a");
		File des=new File("D:java\\bbb");
		cop(src,des);
	}
	public static void cop(File src,File des) throws IOException {
		File[] file=src.listFiles();
		
		for(File f:file) {
			if(f.isFile()) {
				//是文件
				FileInputStream fin=new FileInputStream(f);
				FileOutputStream fout=new FileOutputStream(new File(des,f.getName()));
				int len;
				byte[] b=new byte[1024];
				while((len=fin.read(b))!=-1) {
					fout.write(b,0,len);
				}
				fout.close();
				fin.close();
			}
			else {
				//是文件夹
				cop(f,new File(des,f.getName()));
			}
		}
	}

(3)加密和解密文件

加密解密,加密其实就是如果你想对一个文件加密的话,其实就是在创建一个文件(这就是加密过的文件),然后你想解密的话就是对于加密文件在创建一个文件(解密文件),将数据在传到解密文件中。

FileInputStream fin=new FileInputStream("D:java\\6.webp");
	   FileOutputStream fout=new FileOutputStream("D:java\\7.webp");
	   int b;
	   while((b=fin.read())!=-1) {
		   fout.write(b^2);
	   }
	   fout.close();
	   fin.close();

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜到极致就是渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值