Javaは、パスに従ってパス内のファイルのコンテンツを取得します

次の2つの方法は
、最初の方法を提供します

ここに画像の説明を挿入します

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();
    }

ここに画像の説明を挿入します
2番目の方法は比較的簡単です

 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));
    }

ここに画像の説明を挿入します
あなたも得ることができます

この人生の永続性または非永続性はひどいものではありません。私が恐れているのは、永続性の道を一人で歩いていることです!

おすすめ

転載: blog.csdn.net/taiguolaotu/article/details/112287186