Java—File Operation

File byte stream, can read character files and byte files

  • FileInputStream: input stream
  • FileOutputStream: output stream

Realize file copy function:

package com.cao.demo.lesson00;

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

public class Test {
    
    
    public static void main(String[] args) {
    
    
    	// 源文件路径
        String src="C:\\iweb\\01.jpg";
        // 目标文件路径
        String dst="C:\\iweb\\02.jpg";
        // 读取文件
        FileInputStream in=null;
        // 写入文件
        FileOutputStream out=null;

        try{
    
    
        	// 创建流对象
            in=new FileInputStream(src);
            out=new FileOutputStream(dst);
			// 读取文件
            // 1. 创建一个缓冲区
            byte[] buffer=new byte[1024];
            // 2. 定义一个变量接收每次读取的字节数
            int len=0;
			// 3. 开始读取文件
            while((len=in.read(buffer))!=-1){
    
    
            	// 4. 写文件
                out.write(buffer,0,len);
                // 5.从内存刷新到磁盘
                out.flush();
            }
        }catch(FileNotFoundException fnfe){
    
    
            fnfe.printStackTrace();
        }catch(IOException ie){
    
    
            ie.printStackTrace();
        }
        finally{
    
    
            try {
    
    
            	// 关闭流: 流在使用的过程中一定要关闭
                if(in!=null) in.close();
                if(out!=null) out.close();
            }catch(IOException ie){
    
    
                ie.printStackTrace();
            }
        }
    }
}

File character stream : character stream, can not read and write byte files

  • FilerReader: input
  • FilerWriter: output

Realize text copy function:

package com.cao.demo.lesson00;

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

public class Test5 {
    
    
    public static void main(String[] args) {
    
    
        String src = "C:\\iweb\\test\\01.txt";
        String dst = "C:\\iweb\\test\\02.txt";

        FileReader reader = null;
        FileWriter writer = null;
        try {
    
    
            reader = new FileReader(src);
            writer = new FileWriter(dst);

            char[] buffer=new char[5];
            int len=0;

            while((len=reader.read(buffer))!=-1){
    
    
                writer.write(buffer,0,len);
                writer.flush();
            }
        }catch (Exception e){
    
    
            e.printStackTrace();
        }finally {
    
    
            if(reader!=null){
    
    
                try {
    
    
                    reader.close();
                }catch (IOException ioe){
    
    
                    ioe.printStackTrace();
                }
            }
            if(writer!=null){
    
    
                try{
    
    
                    writer.close();
                }catch (IOException ioe){
    
    
                    ioe.printStackTrace();
                }
            }
        }
    }
}

If you break the point here,
Insert picture description here
you can see that five characters are added to the target file every time you execute it.


File category : File system (FS) system category

  • The basic operations of creating, modifying, and deleting file paths ( it has nothing to do with the file content, and the file content is operated by IO stream)
package com.cao.demo.lesson00;

import java.io.File;

public class Test1 {
    
    
    public static void main(String[] args) {
    
    
        String path = "C:\\iweb\\test";
        // 根据文件的路径创建对象
        File file = new File(path);
        // 根据文件的路径创建对象
        System.out.println(file.isDirectory());
        System.out.println(file.isFile());
        // 判断文件路径是否有效
        System.out.println(file.exists());
        // 查看指定文件路径下的所有文件
        String[] filenames = file.list();// 返回的是文件名称
        for (int i = 0; i < filenames.length; i++) {
    
    
            System.out.println(filenames[i]);
        }

        File[] files=file.listFiles();// 返回的是文件对象数组
        for (int i = 0; i < files.length; i++) {
    
    
            File f=files[i];
            System.out.println(f.getName()+'\t'+f.isDirectory()+'\t'+f.length());
        }
    }

Insert picture description here

true
false
true
01.jpg
01.txt
02.jpg
02.txt
testIn
01.jpg	false	13547
01.txt	false	50
02.jpg	false	13547
02.txt	false	50
testIn	true	0

File creation :

public class Test6 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        String path="C:\\iweb\\test\\testIn\\txtTest";
        File file=new File(path);
        file.mkdirs();
        path="C:\\iweb\\test\\testIn\\txtTest1.txt";
        file=new File(path);
        file.createNewFile();
    }
}

Insert picture description here
File deletion

public class Test6 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        String path="C:\\iweb\\test\\testIn\\txtTest";
        File file=new File(path);
        file.delete();
    }
}

File rename:

public class Test6 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        String path="C:\\iweb\\test\\testIn\\txtTest.txt";
        File file=new File(path);
        file.renameTo(new File("C:\\iweb\\test\\testIn\\txtTest66.txt"));
    }
}

File creation, deletion, renaming and other methods, if successful, it returns true, otherwise it returns false

public class Test6 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        String path="C:\\iweb\\test\\testIn\\txtTest66.txt";
        File file=new File(path);
        boolean b=file.renameTo(new File("C:\\iweb\\test\\testIn\\txtTest22.txt"));
        System.out.println(b);
    }
}

Successful execution, return true

true
public class Test6 {
    
    
    public static void main(String[] args) throws Exception{
    
    
        String path="C:\\iweb\\test\\testIn\\txtTest.txt";
        File file=new File(path);
        boolean b=file.renameTo(new File("C:\\iweb\\test\\testIn\\txtTest22.txt"));
        System.out.println(b);
    }
}

The file could not be found and the execution was unsuccessful. Return false

false

Character buffer stream : processing stream (feature: the parameter of the constructor must be a stream type)

  • BufferedReader input stream
  • BufferedWriter output stream
public class Test7 {
    
    
    public static void main(String[] args) {
    
    
        String src="C:\\iweb\\test\\01.txt";
        String dst="C:\\iweb\\test\\88.txt";

        BufferedReader bufferedReader=null;
        FileReader fileReader=null;
        BufferedWriter bufferedWriter=null;
        FileWriter fileWriter=null;
        try {
    
    
            // 业务过程
            // 读取的是流中的数据
            fileReader = new FileReader(src);
            bufferedReader = new BufferedReader(fileReader);
            fileWriter = new FileWriter(dst);
            bufferedWriter = new BufferedWriter(fileWriter);
            // 字符缓冲流 提供了一个默认的缓冲大小  以行为结束点每次读取一行
            String line=null;
            while((line=bufferedReader.readLine())!=null){
    
    
                //打印每次缓存的一行
                System.out.println(line);
                bufferedWriter.write(line);
                //换行
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }
        }catch (IOException ioe){
    
    
            ioe.printStackTrace();
        }finally {
    
    
            try{
    
    
                if(fileReader!=null){
    
    
                    fileReader.close();
                }
                if(fileWriter!=null){
    
    
                    fileWriter.close();
                }
                if(bufferedReader!=null){
    
    
                    bufferedReader.close();
                }
                if(bufferedWriter!=null){
    
    
                    bufferedWriter.close();
                }
            }catch (IOException ioe){
    
    
                ioe.printStackTrace();
            }
        }
    }
}

Print by line:

hello word!
hello word!
hello word!
hello word!
122	
569

Guess you like

Origin blog.csdn.net/qq_44371305/article/details/113558683