Java obtains the content of the file in the path according to the path

The following two methods provide
a first

Insert picture description here

public static void main(String[] args) throws IOException {

        String fullFilePath = "C:\\Users\\yangquan\\Desktop\\book\\1.html";
        String txt = readFileByPath(fullFilePath);
        System.out.println(txt);

    }

    public static String readFileByPath(String fullFilePath) throws IOException {
        StringBuilder result = new StringBuilder();
        try {
            File file = new File(fullFilePath);
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                result.append(line);
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

Insert picture description here
The second way is relatively simple

 public static void main(String[] args) throws IOException {

        String fullFilePath = "C:\\Users\\yangquan\\Desktop\\book\\1.html";
        String txt = readFileByPath(fullFilePath);
        System.out.println(txt);

    }

    public static String readFileByPath(String fullFilePath) throws IOException {
        byte[] content = Files.readAllBytes(Paths.get(fullFilePath));
        return (new String(content));
    }

Insert picture description here
You can also get

Persistence or non-persistence in this life is not terrible. What I am afraid of is walking on the road of persistence alone! ! !

Guess you like

Origin blog.csdn.net/taiguolaotu/article/details/112287186