IO

2022/8/6 23:23:27

本文主要是介绍IO,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

File类

package IO;

import java.io.File;

/**
 *
 **/
public class Test01 {
    public static void main(String[] args) {
        /**
         * java.io包
         * io对磁盘进行操作 文件操作(增删改查)
         */
        File file = new File("E:/mayikt/mayikt.txt");
        System.out.println(file);//E:\mayikt\mayikt.txt
        //"E:/mayikt/mayikt.txt"
        File file1=new File("E:/mayikt/","mayikt.txt");
        System.out.println(file1);//E:\mayikt\mayikt.txt
        File file2=new File("E:/mayikt/");
        File file3=new File(file2,"mayikt.txt");
        System.out.println(file3);//E:\mayikt\mayikt.txt
    }
}

File类创建功能

package IO;

import java.io.File;
import java.io.IOException;

/**
 *
 **/
public class Test02 {
    public static void main(String[] args) throws IOException {
        /**
         * 在E:\mayikt创建mayikt.txt
         */
        File file = new File("E:/mayikt.txt");
        boolean newFile = file.createNewFile();//不能越级创建,如果E:/mayikt/mayikt.txt,没有mayikt这个文件夹,就会失败
        System.out.println(newFile);
        /**
         * mkdir
         */
        File file2 = new File("E:/mayikt");
        boolean mkdir = file2.mkdir();//不能跨级,创建文件夹,不能重复创建
        System.out.println(mkdir);
        File file3 = new File("E:/mayikt/mayikt2");//可以越级创建
        System.out.println(file3.mkdirs());

    }
}

File类判断方法

package IO;

import java.io.File;

/**
 *
 **/
public class Test03 {
    public static void main(String[] args) {
        File file = new File("E:/mayikt.txt");
        if(file.isFile()){
            System.out.println("是文件");
        }else{
            System.out.println("不是文件");
        }
        File file2 = new File("E:/mayikt");
        if(file.isDirectory()){
            System.out.println("是文件夹");
        }else{
            System.out.println("不是文件夹");
        }
        File file3 = new File("E:/mayikt");
        if(file.exists()){
            System.out.println("已经创建");
        }else{
            System.out.println("没有创建");
        }
    }
}

File类常见获取方法

package IO;

import java.io.File;

/**
 *
 **/
public class Test04 {
    public static void main(String[] args) {
        File file = new File("E:/mayikt/");
        System.out.println(file.getAbsolutePath());//E:\mayikt 绝对路径
        System.out.println(file.getPath());//E:\mayikt 抽象路径-->相对路径
        System.out.println(file.getName());//mayikt 文件夹名
        String[] list = file.list();//返回目录下所有文件和文件夹,String类型
        for (String s :
                list) {
            System.out.println(s);
        }
        File[] files = file.listFiles();
        for (File f :
                files) {
            System.out.println(f);//E:\mayikt\mayikt2 返回目录下所有文件和文件夹,以File类型
        }
    }
}

File删除

package IO;

import java.io.File;

/**
 *
 **/
public class Test05 {
    public static void main(String[] args) {
        File file = new File("E:/mayikt/");
        boolean delete = file.delete();
        System.out.println(delete);//false,因为E:/mayikt/下还有文件,删除目录的化需要删除里面的文件或者目录
        File file2 = new File("E:/mayikt.txt");
        System.out.println(file2.delete());//true   删除文件,直接删除
    }
}

循环删除

递归求阶乘(注意栈溢出)

方法定义中调用方法本身

package IO;

/**
 *
 **/
public class Test06 {
    public static void main(String[] args) {
        jc(5);
    }
    public static int jc(int n){
        if(n==1){
            return 1;
        }else{
            return n*jc(n-1);
        }
    }
}

I/O流

input输入:硬盘读取到内存

output输出:写到硬盘

流的分类

按流向:输入流和输出流

按类型:字节流,字符型

推荐使用:

纯文本用字符

图片音频:字节流

字节输出流

写数据到磁盘

package IO;

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

/**
 *初次使用
 **/
public class Test07 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("E:/mayikt.txt");
        fileOutputStream.write(97);
        fileOutputStream.write(98);
        fileOutputStream.write(99);
        fileOutputStream.write(100);
        fileOutputStream.close();
           /**
         * write(byte[])
         */
        byte[] a={97,98,99,100};
        fileOutputStream.write(a);
        fileOutputStream.close();
    }
}

字节流换行追加写

package IO;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 *
 **/
public class Test08 {
    public static void main(String[] args) throws IOException {
        File file = new File("E:/mayikt.txt");
        //true为追加
        FileOutputStream fileOutputStream = new FileOutputStream(file,true);
        String str="我是蔡23123徐坤";
        for (int i = 0; i < 10; i++) {
            fileOutputStream.write(str.getBytes());
            //换行
            fileOutputStream.write("\n".getBytes());
        }


    }
}

字节流读取

package IO;

import java.io.*;

/**
 *
 **/
public class Test09 {
    public static void main(String[] args) throws IOException {
        File file = new File("E:/mayikt.txt");
        //true为追加
        FileInputStream fileInputStream = new FileInputStream(file);

        InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream,"utf-8");
        try{
            //read()一个一个读取
            int read = inputStreamReader.read();
            while(read!=-1){
                System.out.print((char)read);
                read=inputStreamReader.read();
            }
            System.out.println((char)read);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fileInputStream!=null){
                try{
                    fileInputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }

    }
}

优化

package IO;

import java.io.*;

/**
 *
 **/
public class Test09 {
    public static void main(String[] args) throws IOException {
        File file = new File("E:/mayikt.txt");
        //true为追加
        FileInputStream fileInputStream = new FileInputStream(file);

        InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream,"utf-8");
        try{
            //read()一个一个读取
            int read ;
            while((read=inputStreamReader.read())!=-1){
                System.out.print((char)read);
//                read=inputStreamReader.read();
            }
            System.out.println((char)read);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fileInputStream!=null){
                try{
                    fileInputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }

    }
}

字节流拷贝

package IO;

import java.io.*;

/**
 * 字节流拷贝
 **/
public class Test10 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream=new FileInputStream(new File("E:/mayikt.txt"));
        FileOutputStream fileOutputStream = new FileOutputStream(new File("E:/mayikt2.txt"));
        int read;
        while((read=fileInputStream.read())!=-1){
            fileOutputStream.write(read);
        }
        fileOutputStream.close();
        fileInputStream.close();
    }
}

1024数组读取

package IO;

import java.io.*;

/**
 * 字节流拷贝
 **/
public class Test10 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream=new FileInputStream(new File("E:/mayikt.txt"));
        FileOutputStream fileOutputStream = new FileOutputStream(new File("E:/mayikt2.txt"));
        byte[] bytes = new byte[1024];
        int read;
        while((read=fileInputStream.read(bytes))!=-1){
            System.out.println(read);
            System.out.println(new String(bytes,0,read));
        }
        fileOutputStream.close();
        fileInputStream.close();
    }
}

字节缓冲流

减少系统调用(用户态到内核调用,效率很低)

字节缓冲流默认大小8k

package IO;

import java.io.*;

/**
 * 利用缓冲流写入数据
 **/
public class Test11 {
    public static void main(String[] args) throws IOException {
            FileOutputStream fileOutputStream=new FileOutputStream(new File("E:/mayikt.txt"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        bufferedOutputStream.write("jdjadkhadiu".getBytes());
        bufferedOutputStream.close();
        fileOutputStream.close();
    }
}

输出缓冲一样

复制时,都是用缓冲流进行操作

汉字占用字节

汉字GBK占两个字节

汉字UTF-8占用3个字节

编码乱码

package IO;

import java.io.*;

/**
 * 利用缓冲流写入数据
 **/
public class Test11 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream=new FileInputStream(new File("E:/mayikt.txt"));
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

        byte[] bytes = new byte[1024];
        int read;
        while((read=bufferedInputStream.read(bytes))!=-1){
            System.out.println(read);
            System.out.println(new String(bytes,0,read,"GBK"));//鏄紑鍙戝紑鍙戜簡瀹㈡湇缁忕悊j //文件内容为utf-8
        }
        bufferedInputStream.close();
        fileInputStream.close();
    }
}

存是编码,读是解码

字符流

package IO;

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

/**
 *字符流写
 **/
public class Test12 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("E:/mykt.txt");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"Utf-8");
        outputStreamWriter.write("中国万岁da");
        outputStreamWriter.close();
        fileOutputStream.close();
    }
}
package IO;

import java.io.*;
import java.util.Arrays;

/**
 * 字符流读取
 **/
public class Test13 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("E:/mykt.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8");
        char[] chars=new char[1024];
        int len;
        while ((len=inputStreamReader.read(chars))!=-1){
            System.out.println(new String(chars,0,len));
        }

    }
}

转换流便捷形式

FileWrite和FileOut

package IO;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 *字符转换流
 **/
public class Test14 {
    public static void main(String[] args) throws IOException {
        FileWriter fileWriter = new FileWriter("E:/mykt.txt", true);
        fileWriter.write("ufhiahfaikfajfaf");
        fileWriter.close();
        FileReader fileReader = new FileReader("E:/mykt.txt");
        char[] chars=new char[1024];
        int len;
        while((len=fileReader.read(chars))!=-1){
            System.out.println(new String(chars,0,len));
        }
    }
}
无法指定编码,需要指定编码使用字符流,字符流需要追加文件里买的字节流加上true

字符缓冲流

package IO;

import java.io.*;

/**
 *字符转换流+缓冲流
 **/
public class Test14 {
    public static void main(String[] args) throws IOException {
        FileWriter fileWriter = new FileWriter("E:/mykt.txt", true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write("ufhiahfaikfajfaf");
        bufferedWriter.newLine();//换行
        bufferedWriter.close();
        fileWriter.close();
        FileReader fileReader = new FileReader("E:/mykt.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        
        String str;
        while((str=bufferedReader.readLine())!=null){
            //System.out.println(bufferedReader.readLine());//不能这样写,因为每执行一次其实就是往下走了,我们需要一个变量来接收
            System.out.println(str);
        }

    }
}


这篇关于IO的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程