Spring Boot App中的java.io.FileNotFoundException

时间:2017-05-04 09:15:51

标签: java spring spring-boot filenotfoundexception

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

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

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

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运行这个应用程序时,它运行没有问题。

1 个答案:

答案 0 :(得分:0)

Classic BufferedReader

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

E:\测试\ 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

资源链接: How to read file in Java – BufferedReader