文件字节输入流:每次读取一个字节
package com.itheima.IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Test1 {
public static void main(String[] args) throws Exception {
//1、创建文件字节输入流管道,与原文件接通
//InputStream is = new FileInputStream(new File("bilibili\\src\\itheima01.txt"));
//简化写法
InputStream is = new FileInputStream("bilibili\\src\\itheima01.txt");
//2、开始读取文件的字节数据
//public int read():每次读取一个字节返回,如果没有数据了,返回-1.
int b1 = is.read();
System.out.println(b1);
System.out.println((char) b1);
int b2 = is.read();
System.out.println(b2);
System.out.println((char) b2);
int b3 = is.read();
System.out.println(b3);
System.out.println((char) b3);
int b4 = is.read();
System.out.println(b4);
}
}
package com.itheima.IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Test1 {
public static void main(String[] args) throws Exception {
//1、创建文件字节输入流管道,与原文件接通
//InputStream is = new FileInputStream(new File("bilibili\\src\\itheima01.txt"));
//简化写法
InputStream is = new FileInputStream("bilibili\\src\\itheima01.txt");
//2、开始读取文件的字节数据
//public int read():每次读取一个字节返回,如果没有数据了,返回-1.
// int b1 = is.read();
// System.out.println(b1);
// System.out.println((char) b1);
//
// int b2 = is.read();
// System.out.println(b2);
// System.out.println((char) b2);
//
// int b3 = is.read();
// System.out.println(b3);
// System.out.println((char) b3);
//
// int b4 = is.read();
// System.out.println(b4);
//使用循环读取
int b;//用于记住读取的字节
while ((b = is.read() )!= -1){
System.out.print((char) b);
}
//读取数据的性能差
//读取汉字输出会乱码,且无法避免
//流使用完毕之后,必须关闭,释放系统资源
is.close();
}
}
字节输入流:每次读取多个字节
package com.itheima.IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Test2 {
public static void main(String[] args) throws Exception {
//创建一个字节输入流对象代表字节输入流管道与源文件接通
InputStream is = new FileInputStream("bilibili\\src\\itheima02.txt");
//开始读取文件中的字节数据,每次读取多个字节。
//public int read(byte b[]) throws IOException
//每次读取多个字节到数组中去,返回读取的字节数量,读取完毕会返回-1
byte[] buffer = new byte[3];
int len = is.read(buffer);
String rs = new String(buffer);//解码
System.out.println(rs);
System.out.println("当次读取的字节数量" + len);
//注意:读取多少,到处多少
int len2 = is.read(buffer);
String rs2 = new String(buffer, 0, len2);
System.out.println(rs2);
System.out.println("当次读取的字节数量" + len2);
int len3 = is.read(buffer);
System.out.println(len3);
}
}
package com.itheima.IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Test2 {
public static void main(String[] args) throws Exception {
//创建一个字节输入流对象代表字节输入流管道与源文件接通
InputStream is = new FileInputStream("bilibili\\src\\itheima02.txt");
//开始读取文件中的字节数据,每次读取多个字节。
//public int read(byte b[]) throws IOException
//每次读取多个字节到数组中去,返回读取的字节数量,读取完毕会返回-1
// byte[] buffer = new byte[3];
// int len = is.read(buffer);
// String rs = new String(buffer);//解码
// System.out.println(rs);
// System.out.println("当次读取的字节数量" + len);
//
// //注意:读取多少,到处多少
// int len2 = is.read(buffer);
// String rs2 = new String(buffer, 0, len2);
// System.out.println(rs2);
// System.out.println("当次读取的字节数量" + len2);
//
// int len3 = is.read(buffer);
// System.out.println(len3);
byte[] buffer = new byte[3];
int len;
while ((len = is.read(buffer)) != -1){
String rs = new String(buffer, 0, len);
System.out.print(rs);
}
//读取汉字输出乱码
is.close();
}
}
字节输入流:一次读取完全部字节
package com.itheima.IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Test3 {
public static void main(String[] args) throws Exception {
//一次性读取完文件的全部字节到一个字节数组中去
InputStream is = new FileInputStream("bilibili\\src\\itheima03.txt");
//2、准备一个字节数组,大小与文件的大小正好一样大
File f = new File("bilibili\\src\\itheima03.txt");
long size = f.length();
byte[] buffer = new byte[(int) size];
int len = is.read(buffer);
System.out.println(new String(buffer));
System.out.println(size);
System.out.println(len);
}
}
注意:此方法JDK8没法用
byte[] buffer = is.readAllBytes();
System.out.println(new String(buffer));
文件字节输出流:写字节出去
package com.itheima.IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public class Test4 {
public static void main(String[] args) throws Exception {
//创建一个字节输出流管道与目标文件接通
//覆盖管道:覆盖之前的数据
// OutputStream os = new FileOutputStream("bilibili/src/itheima04out.txt");
//追加数据的管道
OutputStream os = new FileOutputStream("bilibili/src/itheima04out.txt", true);
//2、开始写字节数据出去了
os.write(97);//97戴白哦一个字节a
os.write('b');
byte[] bytes = "sdvbdsbvdsvhsdk".getBytes(StandardCharsets.UTF_8);
os.write(bytes);
os.write(bytes,0, 4);
os.write("\r\n".getBytes(StandardCharsets.UTF_8));
os.close();
}
}
案例:文件复制
package com.itheima.IO;
import java.io.*;
public class Test5 {
public static void main(String[] args) throws Exception {
//1、创建一个字节输入管道与源文件接通
InputStream is = new FileInputStream("C:\\Users\\RCH\\Pictures\\Screenshots");
//2、创建一个字节输出流管道与目标文件接通
OutputStream os = new FileOutputStream("D:\\z\\Screenshots");
//3、创建一个字节数组,负责转移字节数据
byte[] buffer = new byte[1024];
//4、从字节输入流中读取字节数据,写出去到字节输出流中,读多少写出去多少
int len;//记住每次读取了多少个字节
while ((len = is.read(buffer)) != -1){
os.write(buffer,0,len);
}
os.close();
is.close();
System.out.println("复制完成");
}
}