IO流总结
字节流
写数据
字节流创建输出对象
做了三件事情
A.调用系统功能创建了文件
B.创建字节输出流对象
C.让字节输出流对象指向创建好的文件
最后一定要释放资源
void.close();
public static void main(String[] args) throws IOException {
//创建字节流输出对象
//FileOutputStream(String name):创建文件输出流以指定的名称写入文件
FileOutputStream fos=new FileOutputStream("fos.txt");
/*
做了三件事情
A:调用系统功能创建了文件
B:创建了字节输出流对象
C:让字节输出流对象指向创建好的文件
*/
//void write(int b):将指定的字节写入此文件输出流
fos.write(97);
//最后一定要释放资源
//void close:关闭此文件输出流并释放与此流相关的任何系统资源
fos.close();
}
两个问题
1.字节流写数据如何实现换行?
windows:\r\n
linux:\n
mac:\r
2.字节流写数据如何实现追加写入?
public FileOutputStream(String name,boolean append)
创建文件输出流以指定的名称写入文件
如果第二个参数为true,则字节将写入文件的末尾而不是开头
public static void main(String[] args) throws IOException {
//创建字节流输出对象
FileOutputStream fos=new FileOutputStream("fos2.txt",true);
//写数据
for (int i=0;i<10;i++){
fos.write("hello".getBytes());
fos.write("\r\n".getBytes());
}
//释放资源
fos.close();
}
异常处理
一般采用的是throws IOException来解决问题
public static void main(String[] args) {
//用try catch以及finally来实现资源释放
FileOutputStream fos=null;
try{
fos=new FileOutputStream("fos.txt3");
fos.write("hello".getBytes());
}catch (IOException e){
e.printStackTrace();
}finally {
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}}}}
写输出的三种方式
将指定的字节写入此文件输出流
public static void main(String[] args) throws IOException {
//FileOutputStream(String name):创建文件输出流以指定的名称写入文件
FileOutputStream fos=new FileOutputStream("fos1.txt");
//void write(int b):将指定的字节写入此文件输出流
fos.write(97);
fos.write(98);
fos.write(99);
fos.write(100);
fos.write(101);
//释放资源
fos.close();
}
将b.length字节从指定的字节数据写入此文件输出流
public static void main(String[] args) throws IOException {
//FileOutputStream(String name):创建文件输出流以指定的名称写入文件
FileOutputStream fos=new FileOutputStream("fos1.txt");
//void write(byte[] b):将b.length字节从指定的字节数据写入此文件输出流
//byte[] getBytes():返回字符串对应的字节数组
byte[] bys="abcde".getBytes(StandardCharsets.UTF_8);
fos.write(bys);
//释放资源
fos.close();
}
将len字节从指定的字节数组开始,从偏移量off开始写入此文件的输出流
public static void main(String[] args) throws IOException {
//FileOutputStream(String name):创建文件输出流以指定的名称写入文件
FileOutputStream fos=new FileOutputStream("fos1.txt");
//void write(byte[] b,int off,int len):将len字节从指定的字节数组开始,从偏移量off开始写入此文件的输出流
fos.write(bys,1,3);
//释放资源
fos.close();
}
读数据
一次输入一个字节数据
public static void main(String[] args) throws IOException {
//创建字节输入流对象
FileInputStream fis=new FileInputStream("fos2.txt");
int by;
/*
1.fis.read():读数据
2.by=fis.read():把读取道德数据赋值给by
3.by!=-1:判断读取到的数据是否为-1
*/
while ((by= fis.read())!=-1){
/*
1.不用println,因为读取可以读取到换行/r/n字符
2.char强转,输出字符,而不是底层的数字表
*/
System.out.print((char) by);
}}
一次输入一个字节数组数据
public static void main(String[] args) throws IOException {
//创建
FileInputStream fis=new FileInputStream("fos2.txt");
byte[] bys=new byte[1024];//1024及其整数倍
int len;
while((len= fis.read(bys))!=-1){
//new String(bys,offset:0,len)指的是读取实际数据
System.out.print(new String(bys,0,len));
}
//释放资源
fis.close();
}
复制文本文件
public static void main(String[] args) throws IOException {
//根据数据源创建字节输入流对象
FileInputStream fis=new FileInputStream("fos2.txt");
//根据目的地创建字节输出流对象
FileOutputStream fos=new FileOutputStream("java.txt");
//读写数据,复制文本文件(一次读取一个字节,一次写入一个字节)
int by;
while((by= fis.read())!=-1){
fos.write(by);
}
//释放资源
fos.close();
fis.close();
}
复制图片
public static void main(String[] args) throws IOException {
//根据数据源创建字节输入流对象
FileInputStream fis=new FileInputStream("D:\\Java\\test\\111.jpg");
//根据目的地创建字节输出流对象
FileOutputStream fos=new FileOutputStream("D:\\Java\\笔记\\javaTest\\111.jpg");
//读写数据,复制图片(一次读取一个字节数组,一次写入一个字节数组)
byte[] bys=new byte[1024];
int len;
while (((len=fis.read())!=-1)){
fos.write(bys);
}
//释放资源
fis.close();
fos.close();
}
字节缓冲流
基本方法
public static void main(String[] args) throws IOException {
//字节缓冲输出流:BufferedOutputStream(OutputStream out)
// BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\Java\\笔记\\javaTest\\bos.txt"));
// //写数据
// bos.write("hello\r\n".getBytes());
// bos.write("world\r\n".getBytes());
//
// //释放资源
// bos.close();
//字节缓存输入流:BufferedInputStream(InputStream in)
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\Java\\笔记\\javaTest\\bos.txt"));
//一次读取一个数据
int by;
while((by=bis.read())!=-1){
System.out.print((char) by);
}
//释放资源
bis.close();
}
复制视频
1.基本字节流一次读写一个字节 共耗时:70557毫秒
2.基本字节流一次读写一个字节数组 共耗时:102毫秒
3.字节缓冲流一次读写一个字节 共耗时:330毫秒
4.字节缓冲流一次读写一个字节数组 共耗时:33毫秒
public static void main(String[] args) throws IOException {
//记录开始时间
long startTime=System.currentTimeMillis();
//复制视频
//method1();
//method2();
//method3();
method4();
//记录结束时间
long endTime=System.currentTimeMillis();
System.out.println("共耗时:"+(endTime-startTime)+"毫秒");
}
//基本字节流一次读写一个字节 共耗时:70557毫秒
public static void method1()throws IOException{
FileInputStream fis=new FileInputStream("D:\\Java\\test\\222.mp4");
FileOutputStream fos=new FileOutputStream("D:\\Java\\笔记\\javaTest\\222.mp4");
int by;
while ((by=fis.read())!=-1){
fos.write(by);
}
fos.close();
fis.close();
}
//基本字节流一次读写一个字节数组 共耗时:102毫秒
public static void method2()throws IOException{
FileInputStream fis=new FileInputStream("D:\\Java\\test\\222.mp4");
FileOutputStream fos=new FileOutputStream("D:\\Java\\笔记\\javaTest\\222.mp4");
byte[] bys=new byte[1024];
int len;
while((len=fis.read(bys))!=-1){
fos.write(bys,0,len);
}
fos.close();
fis.close();
}
//字节缓冲流一次读写一个字节 共耗时:330毫秒
public static void method3()throws IOException{
// FileInputStream fis=new FileInputStream("D:\\Java\\test\\222.mp4");
// FileOutputStream fos=new FileOutputStream("D:\\Java\\笔记\\javaTest\\222.mp4");
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\Java\\test\\222.mp4"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\Java\\笔记\\javaTest\\222.mp4"));
int by;
while ((by=bis.read())!=-1){
bos.write(by);
}
bos.close();
bis.close();
}
//字节缓冲流一次读写一个字节数组 共耗时:33毫秒
public static void method4()throws IOException{
// FileInputStream fis=new FileInputStream("D:\\Java\\test\\222.mp4");
// FileOutputStream fos=new FileOutputStream("D:\\Java\\笔记\\javaTest\\222.mp4");
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\Java\\test\\222.mp4"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\Java\\笔记\\javaTest\\222.mp4"));
byte[] bys=new byte[1024];
int len;
while ((len= bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
编码表
字符串中的编码解码
public static void main(String[] args) throws IOException {
//定义一个字符串
String s="中国";
//byte[] getBytes():使用平台默认字符集将该String 编码为一系列字节,将结果存储到新的字节数组中
//byte[] bys=s.getBytes();[-28, -72, -83, -27, -101, -67]
//byte[] getBytes(String charsetName):使用指定的字符集将该String编码为一系列字节,将结果储存到新的字节数组中
//byte[] bys=s.getBytes("UTF-8");[-28, -72, -83, -27, -101, -67]
byte[] bys=s.getBytes("GBK");//[-42, -48, -71, -6]
System.out.println(Arrays.toString(bys));
//String(byte[] bytes):通过使用平台的默认字符集解码指定的字节数组来构造新的String
//String ss=new String(bys);
//String(byte[] bytes, String charsetName):通过指定的字符集解码指定的字节数组来构造新的String
String ss=new String(bys,"GBK");
System.out.println(ss);
}
字符流中的编码解码
public static void main(String[] args) throws IOException {
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("osw.txt"),"GBK");
osw.write("中国");
osw.close();
InputStreamReader isr=new InputStreamReader(new FileInputStream("osw.txt"),"GBK");
//一次读取一个字符数据
int ch;
while((ch=isr.read())!=-1){
System.out.print((char) ch);
}
}
字符流
为什么会出现字符流:
中文或者其他语言编码解码时候,计算机需要自动匹配字节来输出相对应的语言文字
写数据的五种方式
public static void main(String[] args) throws IOException {
//OutputStreamWriter(OutputStream out):创建一个使用默认字符编码的OutputStreamWriter
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("osw.txt"));
// //void write (int c):写一个字符
// osw.write(97);
// //void flush刷新流,用于让数据输出
// osw.flush();
//void write (char[] cbuf):写一个字符数组
//字符用''单引号
char[] chs={'a','b','c','d'};
osw.write(chs);
//void write(char[] cbuf,int off, int len):写字符数组的一部分
osw.write(chs,1,3);
//void write(String str):写一个字符串
osw.write("abcde");
//void write(String str,int off,int len):写一个字符串的一部分
osw.write("abcde",1,3);
//释放资源
//释放资源自带刷新流,且close之后不可以再书写数据
osw.close();
}
读数据的两种方式
public static void main(String[] args) throws IOException {
//InputStreamReader(InputStream in):创建一个使用默认字符集的InputStreamReader
InputStreamReader isr=new InputStreamReader(new FileInputStream("fos2.txt"));
//int read():一次读一个字符数据
int ch;
while((ch=isr.read())!=-1){
System.out.print((char)ch);
}
//int read(char[] cbuf):一次读一个字符数组数据
char[] chs=new char[1024];
int len;
while ((len=isr.read(chs))!=-1){
System.out.print(new String(chs,0,len));
}
//释放资源
isr.close();
}
字符流复制java文件
普通方法
public static void main(String[] args) throws IOException {
InputStreamReader isr=new InputStreamReader(new FileInputStream("D:\\Java\\笔记\\javaTest\\src\\字符流\\字符流写数据的五种方式.java"));
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("D:\\Java\\笔记\\javaTest\\osw.txt"));
//一次读一个字符数据
// int ch;
// while ((ch= isr.read())!=-1){
// osw.write(ch);
// }
// 一次读一个字符数组数据
char[] chr=new char[1024];
int len;
while ((len= isr.read(chr))!=-1){
osw.write(chr,0,len);
}
osw.close();
isr.close();
}
改进方法
FileReader fr=new FileReader(地址)
FileWriter fw=new FileWriter(地址)
public static void main(String[] args) throws IOException {
//根据数据源创建字符输入流对象
FileReader fr=new FileReader("D:\\Java\\笔记\\javaTest\\src\\字符流\\字符流写数据的五种方式.java");
//根据目的地创建字符输出流对象
FileWriter fw=new FileWriter("D:\\Java\\笔记\\javaTest\\osw.txt");
//一个字符
// int ch;
// while((ch=fr.read())!=-1){
// fw.write(ch);
// }
//一个字符数组
//字符char,字节byte
char[] chr=new char[1024];
int len;
while ((len=fr.read(chr))!=-1){
fw.write(chr,0,len);
}
fr.close();
fw.close();
}
字符缓冲流复制java文件
复制java文件普通方法
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("D:\\Java\\笔记\\javaTest\\src\\字符流\\字符流写数据的五种方式.java"));
BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\Java\\笔记\\javaTest\\osw.txt"));
//1
// int ch;
// while((ch=br.read())!=-1){
// bw.write(ch);
// }
//2
char[] chr=new char[1024];
int len;
while ((len=br.read(chr))!=-1){
bw.write(chr,0,len);
}
br.close();
bw.close();
}
复制java文件的特殊方法
1.readline(),读取每一行数据,最后一行值为null
2.newline(),换行
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("D:\\Java\\笔记\\javaTest\\src\\字符流\\字符流写数据的五种方式.java"));
BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\Java\\笔记\\javaTest\\osw.txt"));
String line;
//特有方法readline(), 读取每一行数据,最后一行值为null
while ((line=br.readLine())!=null){
bw.write(line);
//特有方法,newline()换行
bw.newLine();
//建议写一个flush
bw.flush();
}
bw.close();
br.close();
}