1.字节流读数据(一次读一个字节数组数据)
public class App
{
public static void main( String[] args ) throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\Faker\\Desktop\\study\\test.txt");
/*
//从该输入流读取第一个字节数据
int read = fis.read();
System.out.println(read);
System.out.println((char)read);
//从该输入流读取第二个字节数据,到达文件末尾后返回-1
read=fis.read();
System.out.println(read);
System.out.println((char)read);
*/
/*
//读取所有
int read = fis.read();
while (read!=-1){
System.out.print((char) read);
read=fis.read();
}
*/
//优化上面的程序
int by;
while ((by=fis.read())!=-1){
System.out.print((char)by);
}
//释放资源
fis.close();
}
}
案例:字节流复制文本文件
public class App
{
public static void main( String[] args ) throws IOException {
//创建字节输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\Faker\\Desktop\\study\\test.txt");
//创建字节输出流对象
FileOutputStream fos = new FileOutputStream("C:\\Users\\Faker\\Desktop\\study\\out.txt");
int by;
while ((by=fis.read())!=-1){ //循环读,末尾返回-1
fos.write(by); //写
}
fis.close();
fos.close();
}
}
2.字节流读数据(一次读一个字节数组数据)
int read(byte[] 5):从该输入流读取最多b.length个字节的数据到一个字节数组
public class App
{
public static void main( String[] args ) throws IOException {
//创建字节输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\Faker\\Desktop\\study\\test.txt");
/*
//int read(byte[] 5):从该输入流读取最多b.length个字节的数据到一个字节数组
byte[] bys = new byte[5];
//第一次读取
int len = fis.read(bys);
System.out.println(len);//实际读取的数据个数 5
System.out.println(new String(bys,0,len));
//第二次读取
len = fis.read(bys);
System.out.println(len);//实际读取的数据个数 5
System.out.println(new String(bys,0,len));
//第二次读取
len = fis.read(bys);
System.out.println(len);//实际读取的数据个数 4
//String(byte[] bytes, int offset , int length) :从第0个位置开始读,读取len个字节
System.out.println(new String(bys,0,len));
*/
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes))!=-1){ //到达末尾返回长度len为-1
System.out.println(new String(bytes,0,len));
}
/*
hello\r\n
world\r\n
第一次:hello
第二次:\r\nwor
第三次:ld\r\n
*/
fis.close();
}
}
案例:字节流复制图片
public class App
{
public static void main( String[] args ) throws IOException {
//创建字节输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\Faker\\Desktop\\study\\mn.png");
FileOutputStream fos = new FileOutputStream("C:\\Users\\Faker\\Desktop\\study\\mq.png");
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes))!=-1){ //到达末尾返回长度len为-1
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
}
3.字节缓冲流
public class App
{
public static void main( String[] args ) throws IOException {
//字节缓冲输出流:BufferedOutputStream(OutputStream out)
// FileOutputStream fos = new FileOutputStream("C:\\Users\\Faker\\Desktop\\study\\test.txt");
// BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("").getCanonicalPath()+"\\dddd.txt"));
//写数据
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
//释放资源
bos.close();
//字节缓冲输入流:BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("").getCanonicalPath()+"\\dddd.txt"));
//读数据
/*
//一次字节
int by;
while ((by=bis.read())!=-1){
System.out.print((char)by);
}
*/
//一次一数组
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
System.out.print(new String(bys,0,len));
}
//释放资源
bis.close();
}
}
案例:字节流复制视频
public class App
{
public static void main( String[] args ) throws IOException {
//记录开始时间
long starttime = System.currentTimeMillis();
//复制视频
method1(); //基本字节流一次读一个字节,共耗时:72496毫秒
method2();//基本字节流一次读一个字节数组,共耗时:132毫秒
method3();//字节缓冲流一次读一个字节,共耗时:525毫秒
method4();//字节缓冲流一次读一个字节数组,共耗时:54毫秒
//记录结束时间
long endtime = System.currentTimeMillis();
System.out.println("共耗时:"+(endtime-starttime)+"毫秒");
}
//基本字节流一次读一个字节
public static void method1() throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\Faker\\Desktop\\study\\hali.mp4");
FileOutputStream fos = new FileOutputStream("C:\\Users\\Faker\\Desktop\\study\\halibote.mp4");
int by;
while ((by=fis.read())!=-1){
fos.write(by);
}
fos.close();
fis.close();
}
//基本字节流一次读一个字节数组
public static void method2() throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\Faker\\Desktop\\study\\hali.mp4");
FileOutputStream fos = new FileOutputStream("C:\\Users\\Faker\\Desktop\\study\\halibote.mp4");
byte[] bys = new byte[1024];
int len;
while ((len=fis.read(bys))!=-1){
fos.write(bys);
}
fos.close();
fis.close();
}
//字节缓冲流一次读一个字节
public static void method3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Faker\\Desktop\\study\\hali.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Faker\\Desktop\\study\\halibote3.mp4"));
int by;
while ((by=bis.read())!=-1){
bos.write(by);
}
bis.close();
bos.close();
}
//字节缓冲流一次读一个字节数组
public static void method4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Faker\\Desktop\\study\\hali.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Faker\\Desktop\\study\\halibote4.mp4"));
int len;
byte[] bys = new byte[1024];
while ((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bis.close();
bos.close();
}
}
4.字符流
为什么会出现字符流
public class App
{
public static void main( String[] args ) throws IOException {
String s1 = "abc";
String s2 = "中国";
byte[] bys1 = s1.getBytes();
byte[] bys2 = s2.getBytes();
byte[] bys3 = s2.getBytes("UTF-8"); //一个汉字占两个字节
byte[] bys4 = s2.getBytes("GBK"); //一个汉字占三个字节
System.out.println(Arrays.toString(bys1));
System.out.println(Arrays.toString(bys2));
System.out.println(Arrays.toString(bys3));
System.out.println(Arrays.toString(bys4));
/*
输出结果
[97, 98, 99]
[-28, -72, -83, -27, -101, -67]
[-28, -72, -83, -27, -101, -67]
[-42, -48, -71, -6]
*/
}
}
编码表
字符串中的编码解码问题
字符流中的编码解码问题
public class App
{
/*
OutputStreamWriter(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
OutputStreamWriter(OutputStream out, Charset cs) 创建一个使用给定字符集的OutputStreamWriter。
*/
public static void main( String[] args ) throws IOException {
String s2 = "中国";
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Faker\\Desktop\\study\\test.txt"),"GBK");
osw.write(s2);
osw.close();
InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\Faker\\Desktop\\study\\test.txt"),"GBK");
//一次读一个字符
int ch;
while ((ch=isr.read())!=-1){
System.out.print((char)ch);
}
isr.close();
}
}
字符流写数据五种方式
字符流读数据两种方式
字符流复制java文件(改进)
5.字符缓冲流
字符缓冲流复制java文件(改进)
public class App
{
public static void main( String[] args ) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Faker\\Desktop\\study\\test.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Faker\\Desktop\\study\\testc.java"));
char[] chs = new char[1024];
int len;
while ((len=br.read(chs))!=-1) {
bw.write(chs,0,len);
}
bw.close();
br.close();
}
}
字符缓冲流特有功能
复制java文件(字符缓冲流特有功能)
public class App
{
public static void main( String[] args ) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Faker\\Desktop\\study\\test.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Faker\\Desktop\\study\\testc.java"));
String line;
while ((line=br.readLine())!=null){
bw.write(line);//写数据
bw.newLine();//换行
bw.flush();//刷新
}
bw.close();
br.close();
}
}
6. IO流小结
字节流
字符流
案例:集合到文件
public class App
{
public static void main( String[] args ) throws IOException {
//创建ArrayList集合
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("hello");
arrayList.add("world");
arrayList.add("java");
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Faker\\Desktop\\study\\testc.txt"));
for (String s: arrayList){
bw.write(s);
bw.newLine();
bw.flush();
}
bw.close();
}
}
案例:文件到集合
public class App
{
public static void main( String[] args ) throws IOException {
//创建ArrayList集合
ArrayList<String> arrayList = new ArrayList<String>();
BufferedReader bw = new BufferedReader(new FileReader("C:\\Users\\Faker\\Desktop\\study\\testc.txt"));
String line;
while ((line=bw.readLine())!=null){
arrayList.add(line);
}
bw.close();
for (String s : arrayList){
System.out.println(s);
}
}
}
案例:文件到集合(排序改进版)
public class App
{
public static void main( String[] args ) throws IOException {
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getAge()-o1.getAge();
}
});
//录入
for (int i=0;i<5;i++){
Scanner sc = new Scanner(System.in);
System.out.println("请输入第"+(i+1)+"个学生信息");
System.out.println("姓名:");
String name = sc.nextLine();
System.out.println("年龄:");
int age = sc.nextInt();
Student student = new Student(name,age);
ts.add(student);
}
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Faker\\Desktop\\study\\student.txt"));
for (Student s:ts){
StringBuilder sb = new StringBuilder();
sb.append(s.getUsername()).append(",").append(s.getAge());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
}
案例:复制单级文件夹
public class App
{
public static void main( String[] args ) throws IOException {
//原目录
File src = new File("C:\\Users\\Faker\\Desktop\\study\\itcast");
String srcFolderName = src.getName();
//目标目录
File dest = new File("C:\\Users\\Faker\\Desktop",srcFolderName);
if (!dest.exists()){
dest.mkdir();
}
//获取数据源目录下所有文件的File数组
File[] listFiles = src.listFiles();
for (File f:listFiles){
String srcfilename = f.getName();
File destfile = new File(dest,srcfilename);
//复制
copyFile(f,destfile);
}
}
private static void copyFile(File f, File destfile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destfile));
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
}
案例:复制多级文件夹
public class App
{
public static void main( String[] args ) throws IOException {
//原目录
File srcFile = new File("E\\itcast");
//目的地
File destFile = new File("F\\");
//写方法实现文件夹的复制
copyFolder(srcFile,destFile);
}
//复制文件夹
private static void copyFolder(File srcFile, File destFile) throws IOException {
//判断原文件是否为目录
if (srcFile.isDirectory()){
String srcFileName = srcFile.getName();
File newFolder = new File(destFile,srcFileName);//F:\\itcast
if(!newFolder.exists()){
newFolder.mkdir();
}
//复制文件数据
File[] fileArray = srcFile.listFiles();
for (File file:fileArray){
copyFolder(file,newFolder);
}
}else { //是文件
File newFile = new File(destFile,srcFile.getName());
copyFile(srcFile,newFile);
}
}
private static void copyFile(File f, File destfile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destfile));
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
}
复制文件异常处理
JDK7:
JDK9: