Java自学-IO流(2)

Java自学-IO流(2)

1、递归求阶乘

Java自学-IO流(2)

Java自学-IO流(2)

package lesson02;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/23 20:05
 * FileName: RecursionDemo01
 * Description:
 */
public class RecursionDemo01 {
    public static void main(String[] args) {
        int result = jc(5);
        System.out.println("5的阶乘为:"+result);
    }

    public static int jc(int n){
        if(n==1){
            return 1;
        }else{
            return n*jc(n-1);
        }
    }
}

Java自学-IO流(2)

Java自学-IO流(2)

2、遍历目录

Java自学-IO流(2)

package lesson02;

import java.io.File;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/23 20:12
 * FileName: RecursionDemo02
 * Description:
 */
public class RecursionDemo02 {
    public static void main(String[] args) {
        File srcFile=new File("D:\\Java");
        getAllFilePath(srcFile);
    }

    public static void getAllFilePath(File srcFile){
        File[] fileArray = srcFile.listFiles();
        if(fileArray !=null){
            for(File file:fileArray){
                if(file.isDirectory()){
                    getAllFilePath(file);
                }else{
                    System.out.println(file.getAbsolutePath());
                }
            }
        }
    }
}

3、IO流概述和分类

Java自学-IO流(2)

Java自学-IO流(2)

Java自学-IO流(2)

4、字节流写数据

Java自学-IO流(2)

package lesson01;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/23 20:28
 * FileName: FileOutputStreamDemo01
 * Description:
 */
public class FileOutputStreamDemo01 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("fos.txt");

        /*
        做了三件事情:
        A:调用系统功能创建了文件
        B:创建了字节输出流对象
        c:让字节输出流对象指向创建好的文件
         */
        fos.write(97);
//        fos.write(57);
//        fos.write(55);
        fos.close();
        
    }
}

5、字节流写输入的三种方式

Java自学-IO流(2)

IDEA格式化快捷键:Ctrl+Alt+l

package lesson01;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/23 20:39
 * FileName: FileOutputStreamDemo02
 * Description:
 */
public class FileOutputStreamDemo02 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("fos.txt");

//        File file = new File("fos.txt");
//        FileOutputStream fos1 = new FileOutputStream(file);
//        FileOutputStream fos2 = new FileOutputStream(new File("fos.txt"));

//        fos.write(97);
//        fos.write(98);
//        fos.write(99);
//        fos.write(100);
//        fos.write(101);

//        byte[] bys={97,98,99,100,101};
        byte[] bys = "abcde".getBytes();
//        fos.write(bys);

//        fos.write(bys,0,bys.length);
        fos.write(bys,1,3);

        fos.close();
    }
}

6、字节流写数据的两个小问题

Java自学-IO流(2)

package lesson01;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/23 20:52
 * FileName: FileOutputStreamDemo03
 * Description:
 */

/*
window : \r\n
linux: \n
mac:\r
 */
public class FileOutputStreamDemo03 {
    public static void main(String[] args) throws IOException {
//        FileOutputStream fos = new FileOutputStream("fos.txt");
        FileOutputStream fos = new FileOutputStream("fos.txt",true);

        for (int i = 0; i < 10; i++) {
            fos.write("hello".getBytes());
            fos.write("\r\n".getBytes());
        }

        fos.close();
    }
}

7、字节流写数据加异常处理

Java自学-IO流(2)

package lesson01;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/23 21:03
 * FileName: FileOutputStreamDemo04
 * Description:
 */
public class FileOutputStreamDemo04 {
    public static void main(String[] args) {
//        try {
//            FileOutputStream fos = new FileOutputStream("fos.txt");
//            fos.write("hello".getBytes());
//            fos.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("fos.txt");
            fos.write("hello".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

8、字节流读数据(一次读一个字节数据)

Java自学-IO流(2)

package lesson02;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/23 21:13
 * FileName: FileInputStreamDemo01
 * Description:
 */
public class FileInputStreamDemo01 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("fos.txt");

        //第一次读取数据。
//        int by = fis.read();
//        System.out.println(by);
//        System.out.println((char)by);
//
//        //第二次读取数据。
//        by = fis.read();
//        System.out.println(by);
//        System.out.println((char)by);

//        int by=fis.read();
//        while(by!=-1){
//            System.out.print((char)by);
//            by=fis.read();
//        }

        //优化
        int by;
        while((by=fis.read())!=-1){
            System.out.print((char)by);
        }

        fis.close();
    }
}

9、字节流复制文本文件

Java自学-IO流(2)

Java自学-IO流(2)

package lesson02;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/23 21:33
 * FileName: CopyTxtDemo
 * Description:
 */
public class CopyTxtDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("D:\\Java\\test.txt");

        FileOutputStream fos = new FileOutputStream("test.txt");

        int by;
        while((by=fis.read())!=-1){
            fos.write(by);
        }

        fis.close();
        fos.close();
    }
}

10、字节流读数据(一次读一个字节数组数据)

Java自学-IO流(2)

package lesson02;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/23 21:38
 * FileName: FileInputStreamDemo02
 * Description:
 */
public class FileInputStreamDemo02 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("fos.txt");

//        byte[] bys=new byte[5];
//        int len=fis.read(bys);
//        System.out.println(len);
////        System.out.println(new String(bys));
//        System.out.println(new String(bys,0,len));
//
////        \r\nlov
//        len=fis.read(bys);
//        System.out.println(len);
////        System.out.println(new String(bys));
//        System.out.println(new String(bys,0,len));
//
////        e\nlov
//        len=fis.read(bys);
//        System.out.println(len);
////        System.out.println(new String(bys));
//        System.out.println(new String(bys,0,len));

        byte[] bys=new byte[1024];//1024及其整数倍。
        int len;
        while((len=fis.read(bys))!=-1){
            System.out.print(new String(bys,0,len));
        }
        fis.close();
    }
}

Java自学-IO流(2)

上一篇:android 系统启动


下一篇:Python-内置函数_@propery、@classmethod、@staticmethod详解