java.io.FileNotFoundException 在 Spring 引导应用程序中

java.io.FileNotFoundException in Spring Boot App

我有一个使用 spring 启动的小应用程序。在这个应用程序中,我读取了一个位于本地系统磁盘中的 conf 文件,这个应用程序 运行s。

BufferedReader confFile = new BufferedReader(new FileReader("C:\Users\userName\Desktop\Tes\conf.json"));

我在指定位置也有一个 conf.json 文件。但是当我 运行 我的 spring 启动应用程序时它说

java.io.FileNotFoundException: C:\Users\userName\Desktop\Tes\conf.json (The system cannot find the file specified)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileReader.<init>(Unknown Source)

请告诉我我缺少什么。

注意:当我 运行 这个应用程序从 eclipse 中消失时 运行s 没有问题。

经典 BufferedReader

用于从文件中读取内容的经典 BufferedReader。

E:\test\filename.txt

This is the content to write into file
This is the content to write into file

ReadFileExample1.java

package com.mkyong;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileExample1 {

    private static final String FILENAME = "E:\test\filename.txt";

    public static void main(String[] args) {

        BufferedReader br = null;
        FileReader fr = null;

        try {

            fr = new FileReader(FILENAME);
            br = new BufferedReader(fr);

            String sCurrentLine;

            br = new BufferedReader(new FileReader(FILENAME));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if (br != null)
                    br.close();

                if (fr != null)
                    fr.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }

    }

}

输出:

This is the content to write into file
This is the content to write into file

资源 Link: How to read file in Java – BufferedReader