javaIO


File

public class FileDemo {
    public static void main(String[] args) {
        File file = new File("D:\\note\\test.txt");
        System.out.println(file);

        File file1 = new File("D:\\note", "test.txt");
        System.out.println(file1);
        File file2 = new File("D:\\note");

        File file3 = new File(file2, "test.txt");
        System.out.println(file3);

    }
}

public class FileDemo2 {
    public static void main(String[] args) throws IOException {
        //1.  在D盘  note目录下 创建test.txt
        File file = new File("D:\\note\\test.txt");
        //若文件已存在 返回false
        System.out.println(file.createNewFile());
        System.out.println("//////////////////////////////");


        //2.  在D盘  note目录下 创建test文件夹
        File file2 = new File("D:\\note\\test");
        //若文件夹已存在 返回false
        System.out.println(file2.mkdir());  //不能创建多级目录
        //3.  在D盘  note目录下 创建多级文件夹
        File file1 = new File("D:\\note\\test\\a\\b");
        System.out.println(file1.mkdirs());//能创建多级目录



        File file3 = new File("D:\\note\\test.txt");
        //若文件已存在 返回false
        System.out.println(file.mkdir()); //成功 只是创建了个test.txt的文件夹
    }
}

public class FileDemo3 {
    public static void main(String[] args) {
        File file = new File("java.txt");
        System.out.println(file.isDirectory());//是否为目录
        System.out.println(file.isFile());//是否为文件
        System.out.println(file.exists());//是否存在

        System.out.println(file.getAbsolutePath());
        System.out.println(file.getPath());
        System.out.println(file.getName());


        File file1 = new File("D:\\note");
        String[] list = file1.list();
        for (String s : list) {
            System.out.println(s);
        }

        System.out.println("---------------");
        File[] files = file1.listFiles();
        for (File file2 : files) {
            if(file2.isDirectory()){
                System.out.println(file2.getName());
            }
        }

    }
}


public class FileDemo4 {
    public static void main(String[] args) throws IOException {
        File file = new File("java.txt");
        System.out.println(file.createNewFile());

        //删除java.txt文件
        boolean delete = file.delete();
        System.out.println(delete);
    }
}

IO流


public class FileDemo5 {
    public static void main(String[] args) throws IOException {
        //
        FileOutputStream fileOutputStream = new FileOutputStream("test.txt");
        /**
         * 做了三件事
         *创建文件
         * 创建输出流对象
         * 将字节输出流指向创建好的对象
         */
        fileOutputStream.write(98);
        //关闭流
        fileOutputStream.close();
    }
}

写数据的三种方式

/*
其实下面这三句代码都是等价的
*/

FileOutputStream fileOutputStream = new FileOutputStream("test.txt");
//实际上底层做了
FileOutputStream fileOutputStream1 = new FileOutputStream(new File("test.txt"));


File file = new File("test.txt");
FileOutputStream fileOutputStream2 = new FileOutputStream(file);
package IO2;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileDemo6 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("test.txt");
        //实际上底层做了
        //FileOutputStream fileOutputStream1 = new FileOutputStream(new File("test.txt"));
/*
        File file = new File("test.txt");
        FileOutputStream fileOutputStream2 = new FileOutputStream(file);*/
        // 1.
        // fileOutputStream.write(11);

        byte[] a={97,98,99,100};
       /* fileOutputStream.write(a);

        byte[] b="eeeee".getBytes();
        fileOutputStream.write(b);*/
        fileOutputStream.write(a,1,2);
    }
}
public class FileDemo7 {
    public static void main(String[] args) throws IOException {
         /*
        追加写入问题   append指定为true
         */
        FileOutputStream fileOutputStream = new FileOutputStream("test.txt",true);
        for (int i = 0; i <10 ; i++) {
            fileOutputStream.write("hello".getBytes());
            fileOutputStream.write("\n".getBytes());
        }
    }
}

public class FileDemo8 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream=new FileInputStream("test.txt");
        //读一个字节
       /* int read = fileInputStream.read();
        System.out.println(read);
        System.out.println((char)read);*/

        int read1 = fileInputStream.read();
        while (read1!=-1){
            System.out.println((char)read1);
            read1=fileInputStream.read();
        }
    }
}

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

public class FileDemo9 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("test.txt");
        FileOutputStream out = new FileOutputStream("test2.txt");

        int re=in.read();
        while (re!=-1){
            out.write(re);
            re=in.read();
        }
        in.close();
        out.close();
    }
}
package IO2;

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

public class FileDemo10 {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("test3.txt");
        FileInputStream fileInputStream = new FileInputStream("test.txt");
        byte[] bytes = new byte[1024];
        int len;
        while ((len=fileInputStream.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
    }
}


package IO2;

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

/**
 * @Date: 2022/4/26 15:59
 *
 * IO流
 */
public class FileDemo11 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("test.txt");
        byte[] bytes = new byte[5];

        int read = fileInputStream.read(bytes);
        System.out.println(read);
        System.out.println(new String(bytes));

        int read1 = fileInputStream.read(bytes);//返回的是世纪读取的长度
        System.out.println(read1);
        System.out.println(new String(bytes,0,read1));
    }
}
package IO2;

import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @Date: 2022/4/26 16:04
 *复制图片
 */
public class FileDemo12 {
    public static void main(String[] args) throws Exception {
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\PC\\Pictures\\壁纸\\2.png");

        FileOutputStream fileInputStream1 = new FileOutputStream("C:\\Users\\PC\\Pictures\\壁纸\\3.png");

        byte[] bytes = new byte[1024];

        int len=0;
        while ((len=fileInputStream.read(bytes))!=-1){
            fileInputStream1.write(bytes,0,len);
        }

        fileInputStream.close();
        fileInputStream1.close();

    }
}

缓冲流


复制音频

package IO2;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @Date: 2022/4/26 16:29
 *复制视频
 *
 * 需求;
 *
 * 思路:
 * 1:根据数据源创建字节输入流对象2:根据目的地创建字节输出流对象
 * 3:读写数据,复制图片(一次读取一个字节数组,一次写入一个字节数组)4:释放资源
 * 四种方式实现复制视频,并记录每种方式复制视频的时间
 * 1 :基本字节流一次读写一个字节
 * 2:基本字节流一次读写一个字节数组
 * 3:字节缓冲流一次读写一个字节
 * 4 :字节缓冲流一次读写一个字节数组
 * I
 */
public class FileDemo14 {
    public static void main(String[] args) throws Exception {
        long l = System.currentTimeMillis();
       // method1();   //太慢
       // method2();   //1716
      //  method3();    //132
       // method4();    //24
        test();
        long l1 = System.currentTimeMillis();
        System.out.println(l1-l);

    }

    public static void method1() throws Exception{

        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\PC\\Music\\美人鱼-林俊杰.mp3");
        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\PC\\Music\\美人鱼-林俊杰2.mp3");
        byte[] bytes = new byte[100];
       int len;

       while ((len=fileInputStream.read(bytes))!=-1){
           fileOutputStream.write(len);
       }
       fileInputStream.close();
        fileOutputStream.close();
    }


    public static void method2() throws Exception{
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\PC\\Music\\美人鱼-林俊杰.mp3");
        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\PC\\Music\\美人鱼-林俊杰2.mp3");
        byte[] bytes = new byte[1024];
        int len;

        while ((len=fileInputStream.read(bytes))!=-1){
            fileOutputStream.write(len);
        }
       fileInputStream.close();
        fileOutputStream.close();
    }

    public static void method3() throws Exception{
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\PC\\Music\\美人鱼-林俊杰.mp3"));
        BufferedOutputStream fileOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\Users\\PC\\Music\\美人鱼-林俊杰2.mp3"));
        int len;
        while ((len=bufferedInputStream.read())!=-1){
            fileOutputStream.write(len);
        }
        bufferedInputStream.close();
        fileOutputStream.close();
    }

    public static void method4() throws Exception{
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\PC\\Music\\美人鱼-林俊杰.mp3"));
        BufferedOutputStream fileOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\Users\\PC\\Music\\美人鱼-林俊杰2.mp3"));
        int len;
        byte[] bytes = new byte[1024];
        while ((len=bufferedInputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,len);
        }
        bufferedInputStream.close();
        fileOutputStream.close();
    }

    public static void test()throws  Exception{
        FileInputStream fileInputStream = new FileInputStream("test2.txt");
        int read = fileInputStream.read();
        System.out.println(read);

        byte[] bytes = new byte[20];
        byte[] bytes1 = new byte[30];
        int read1 = fileInputStream.read(bytes);
        System.out.println(read1);
        int read2 = fileInputStream.read(bytes1);
        System.out.println(read2);

    }

}

输入流的read 不传参 返回值是读到的数据 传递bytes 返回值 是读取到的长度

字符流

字节流复制文件为什么没有问题呢?

因为字节流在读取到汉字是 得到他的getBytes[] 汉字开头是 -20多 如果识别出来则认为是 汉字 再看编码格式进行拼接

package IO3;

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

/**
 * @Date: 2022/4/26 17:08
 *字符流
 */
public class Demo1 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("test.txt"));
        outputStreamWriter.write("97");
        outputStreamWriter.flush();  //将缓冲流的数据  刷新到文件里面

        char[] chars={'a','b','c'};
        outputStreamWriter.write(chars,0,chars.length);
        outputStreamWriter.flush();
       // outputStreamWriter.close();//先flush刷新后  关闭流
    }
}

package IO3;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Demo2 {
    public static void main(String[] args) throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("test.txt"));
     /*      int ch;
           while ((ch=inputStreamReader.read())!=-1){
               System.out.println((char) ch);
           }*/

           char []chs=new char[1024];
           int len;
           while((len=inputStreamReader.read(chs))!=-1){
            System.out.println(new String(chs,0,len));
        }

           inputStreamReader.close();

    }
}

复制java文件

package IO3;

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

public class Demo3 {
    public static void main(String[] args) throws Exception {
        FileReader fileReader = new FileReader("test2.txt");
        FileWriter fileWriter = new FileWriter("copy.txt");
        char[] chars = new char[1024];
        int len;
        while ((len=fileReader.read(chars))!=-1){
            fileWriter.write(chars,0,len);
        }
        fileWriter.close();

    }
}

package IO3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * @Date: 2022/4/26 17:47
 *字符缓冲流
 */
public class Demo4 {
    public static void main(String[] args) throws Exception{
        FileWriter fileWriter = new FileWriter("test4.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write("hello");
        bufferedWriter.close();

        BufferedReader bufferedReader = new BufferedReader(new FileReader("test.txt"));
        int read = bufferedReader.read();
        System.out.println(read);
    }
}
package IO3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
/**
字符缓冲流特有功能
*/
public class Demo5 {
    public static void main(String[] args) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("test2.txt"));

        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("test5.txt"));

        String line;
        while ((line=bufferedReader.readLine())!=null){
            bufferedWriter.write(line);
            //换行
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }

        bufferedReader.close();
        bufferedWriter.close();
    }
}

小结:



字节打印流


文章作者: 蛰伏
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 蛰伏 !
  目录