JAVA读写文件

  • 写文件
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class fileTest {
    
    public static void main(String[] args) {
        
        File file = new File("F:\\test.txt");
        if (!file.exists()) {
            try {
                file.createNewFile(); //如果文件不存在则创建文件
            } catch (IOException e) {
                e.printStackTrace();
            }  
        }
        writeInFile(file, "test");   //写入文件
    }
    
    private static void writeInFile(File file, String content) {
        Writer writer = null;
        StringBuilder outputString = new StringBuilder();
        try {
            outputString.append(content + "\r\n");
            writer = new FileWriter(file, true); // true表示追加
            writer.write(outputString.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }
}


  • 读文件
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;

public class readFile {
    public static void main(String[] args) {

        File file = new File("F:\\test.txt");
        if (!file.exists()) {
            System.out.println("文件不存在!");
            return ;
        }
        try {
            readtFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /** 读取数据,存入集合中 */
    public static void readtFile(File file) throws IOException, ParseException {
        InputStreamReader read = null;// 考虑到编码格式
        try {
            read = new InputStreamReader(new FileInputStream(file), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader bufferedReader = new BufferedReader(read);
        String lineTxt = null;
        while ((lineTxt = bufferedReader.readLine()) != null) {
            System.out.println(lineTxt);
        }
        read.close();
    }
}

  • 读取文件夹
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;

public class readDir {
    public static void main(String[] args) {
        String dir = "F:\\zhijiao\\小学";
        File[] files = new File(dir).listFiles();
        for (File file : files) {
            if (file.isFile() && file.exists()) { // 判断文件是否存在
                try {
                    readtFile(file);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ParseException e) {
                    e.printStackTrace();
                } // 将文件中的数据读取出来,并存放进集合中
            } else {
                System.out.println("文件不存在,请检查文件位置!");
            }
        }
    }
    
    /** 读取数据,存入集合中 */
    public static void readtFile(File file) throws IOException, ParseException {
        InputStreamReader read = null;// 考虑到编码格式
        try {
            read = new InputStreamReader(new FileInputStream(file), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader bufferedReader = new BufferedReader(read);
        String lineTxt = null;
        while ((lineTxt = bufferedReader.readLine()) != null) {
            System.out.println(lineTxt);
        }
        read.close();
    }
}

你可能感兴趣的:(JAVA读写文件)