Java obtém o conteúdo do arquivo no caminho de acordo com o caminho

Os dois métodos a seguir fornecem
uma primeira

Insira a descrição da imagem aqui

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

Insira a descrição da imagem aqui
A segunda maneira é relativamente simples

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

Insira a descrição da imagem aqui
Você também pode obter

Persistência ou não persistência nesta vida não é terrível.O que eu tenho medo é de trilhar o caminho da persistência sozinha! ! !

Acho que você gosta

Origin blog.csdn.net/taiguolaotu/article/details/112287186
Recomendado
Clasificación