IO流: 方向分为:输入流:读取数据
输出流:写输出数据
数据类型分为:先有的字节流,然后有了字符流.
针对文本文件操作:优先字符流.用记事本打开,能看懂
字节流:图片文件,音频,视频…
字节输入流:InputSream: 抽象类:FileInputStream
字节输出流:OutputStream:抽象类 FileOutputStream
复制方法:1.单个字节遍历(最低)
2.一次便利一个字节数组(2)
3.缓冲流:1.一次一个(3) 2.一个一个字节数组(效率最高)
字符输入流:Reader:FileReader
字符输出流:Writer:FileWriter
字节输入流:一次遍历一个字节
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
FileInputStream fI = new FileInputStream("first.txt");
//字节输入流
int by=0;
while((by=fI.read())!=-1){
System.out.print((char)by+" ");
}
fI.close();
}
}
一个中文转换成两个字节:默认GBK格式
Utf-8:一个字节转换成三个字符
public class StringDemo {
public static void main(String[] args) {
String s="卢本伟牛逼";
byte[]bys=s.getBytes();
System.out.println(Arrays.toString(bys));
}
}
字节输入流:一次遍历一个字节数组
public class FileInputStreamDemo2 {
public static void main(String[] args) throws IOException {
//创建文件字节输入流对象
FileInputStream fI = new FileInputStream("second.txt") ;
byte[]by=new byte[1024];
int len=0;
while((len=fI.read(by))!=-1){
System.out.println(new String(by,0,len));
}
}
}
//将third.txt复制到four.txt
数据源: third.txt读数据
目的地: four.txt写数据
public class FileInputDemo3 {
public static void main(String[] args) throws IOException {
// 文件字节输入流
FileInputStream fs=new FileInputStream("third.txt");
// 文件字节输出流
FileOutputStream fo=new FileOutputStream("four.txt");
int by=0;
while((by=fs.read())!=-1){
fo.write(by);
fo.flush();
}
fs.close();
fo.close();
}
}
一次遍历单个字节复制图片
public class FileInputStreamDemo5 {
public static void main(String[] args) throws IOException {
FileInputStream fl = new FileInputStream("3.png");
FileOutputStream fo = new FileOutputStream("4.png");
int len=0;
while((len=fl.read())!=-1){
fo.write(len);
}
fl.close();
fo.close();
}
}
一次遍历一个字节数组.
//复制图片
public class FileInputDemo4 {
public static void main(String[] args) throws IOException {
FileInputStream fs= new FileInputStream("e:\\2.png");
FileOutputStream so = new FileOutputStream("3.png");
//一次便利一个字节数组
int by=0;
byte[]b=new byte[1024];
while((by=fs.read())!=-1){
so.write(b, 0, by);
}
fs.close();
so.close();
}
}
字符缓冲流:一次遍历一个字节数组.输出结果
public class FileInputStreamDemo6 {
public static void main(String[] args) throws IOException {
//创建一个字节缓冲输入流对象
BufferedInputStream bs = new BufferedInputStream(new FileInputStream("four.txt"));
int len=0;
byte []b=new byte [1024];
while((len=bs.read(b))!=-1){
System.out.println(new String(b,0,len));
}
bs.close();
}
}
Flush()和close()方法区别
Flush():刷新缓冲区的流,强迫将缓冲字节都写到流中,刷新之后可以写数据的
Close():关闭流对象指向这个资源文件,一旦关闭,写数据进不去.
计算机如何识别中文的?
GBK模式:每个中文左边对象字节一定是负数
GBK格式:右边的字节可以中正数,可以是0,可以是负数
一个汉字转换两个字节
public class FileDemo {
public static void main(String[] args) {
String s = "英雄联盟" ;
byte[] bys = s.getBytes() ;System.out.println(bys);
System.out.println(Arrays.toString(bys));
}
}
//转换成字节:
[-45, -94, -48, -37, -63, -86, -61, -53]
字节流一次读取一次字节,出现中文乱码:
给字节进行强制类型转换,当中文时需要使用字符流.默认GBK格式
字符流必须指定编码格式
编码:就是将能看懂的字符串转换成看不懂的字节数组
public byte[] getBytes(Charset charset):将字符串转换成字节数组,指定编码格式(字符集)
解码:就是将看不懂的字节数组看懂的:将字节数组字符串
public class FileDemo2 {
public static void main(String[] args) throws IOException {
//编码
String s = "我是龙哥" ;
byte[] by = s.getBytes() ;
System.out.println(Arrays.toString(by));
//解码
String st=new String(by,"GBK");
System.out.println(st);
}
}
输出结果:
[-50, -46, -54, -57, -63, -6, -72, -25]
我是龙哥
字符输入流:字节输入流+编码格式
字符输出流:Writer:是一个抽象类
使用子类:OutputStreamWriter字符转换流:可以将字节输出流转换字符流
public OutputStreamWriter(OutputStream out,Charset cs) 后面的指定编码格式
publicStreamWriter(OutputStream out) //使用默认GBK编码
eg:
public class InputStreamDemo {
public static void main(String[] args) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(
"second.txt"),"GBK"); //原有的写
OutputStreamWriter writer2 = new OutputStreamWriter(new FileOutputStream(
"six.txt"));//默认GBK //新建一个
writer.write("10");
writer.close();
writer2.write("本宝宝");
writer2.close();
}
}
将一个项目下内容复制.
public class InputDemo {
public static void main(String[] args) throws IOException {
//数据源:a.txt---->Reader---->InputStreamReader(InputStream in):字符转换输入流
//目的地:b.txt---->Writer---->OutputStreamWriter(Outputstream out):字符转换输出流
InputStreamReader isr = new InputStreamReader(new FileInputStream(
"first.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
"b.txt"));
char[] chs = new char[1024] ;
int len = 0;
while((len=isr.read(chs))!=-1){
//写数据:
osw.write(chs, 0, len) ;
//刷新流
osw.flush() ;
}
isr.close() ;
osw.close() ;
}
}
使用字符转换流进行操作数据的时候:字节流+编码格式
新的:便捷类.
public class FileReaderDemo1 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("four.txt");
FileWriter fw = new FileWriter("nine.txt");
int len=0;
char []b=new char[1024];
while((len=fr.read(b))!=-1){
fw.write(b, 0, len);
fw.flush() ;
}
fw.close() ;
fr.close() ;
}
}
符输出流写数据的方法:
public void write(int c):写单个字符
public void write(char[] cbuf):写一个字符数组
public abstract void write(char[] cbuf,int off,int len):写入字符数组的一部分
public void write(String str):写字符串
public void write(String str, int off,int len):写字符串的一部分
字符缓冲流:特有功能
BufferWriter:public void newline():写入换行字符
BufferReader:public String readLine():一次读取一行
public class FileDemo {
public static void main(String[]args)throws IOException {
//读数据
//创建一个字符缓冲输入流对象 读数据
BufferedReader br = new BufferedReader(new FileReader("four.txt")) ;
String line = null ;
while((line=br.readLine())!=null){
System.out.println(line);
}
}
}
public class FileDemo3 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("ten.txt")) ;
//写数据
for(int x = 0 ; x <10 ; x ++){
bw.write("龙哥"+x+"厉害") ;
bw.newLine() ;
bw.flush() ;
}
bw.close();
}
}
没有 newline()前,写入换行符号(“\r\n”):
一次读取一个字节,一次读取一个字节数组方法差不多.
递归三个条件
1. 定义一个方法,
2. 必须要有规律
3. 要有出口条件
IO流
最新推荐文章于 2024-01-27 16:24:57 发布