Spring Files.readString()不适用于将Maven设置为Java 11的情况 [英] Spring Files.readString() does not work with Maven set to Java 11

查看:78
本文介绍了Spring Files.readString()不适用于将Maven设置为Java 11的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编写一个简单的应用程序,该应用程序读取文件并打印出文件内容.我读到Java 11具有Java.io.file库,因此我使用了它.我在本地创建了一个.txt文件.

Trying to write a simple application that reads a file and prints out the file's content. I read that Java 11 has Java.io.file library so I used it. I created a .txt file locally.

我的主要班级是这样的:

My main class looks like this:

package com.iz.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import javax.swing.text.AbstractDocument.Content;

@SpringBootApplication
public class BackendApplication {

    public static void main(String[] args) {
        SpringApplication.run(BackendApplication.class, args);        
    
        Path filePath = Paths.get("/Users/iz/Proj/backend/src/testFile.txt");

        try
        {
            String content = Files.readString(filePath);

            System.out.println(content);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
} 

运行时

mvn clean install

编译并生成一个jar文件时,出现编译错误:

to compile and build a jar file, I get a compilation error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project backend: Compilation failure
[ERROR] /home/proj/backend/src/main/java/com/iz/backend/BackendApplication.java:[26,34] error: cannot find symbol
[ERROR] 
[ERROR] -> [Help 1]

推荐答案

对我有用,并带有以下pom:

Works for me, with the following pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.1</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>11</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

这篇关于Spring Files.readString()不适用于将Maven设置为Java 11的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆