簡體   English   中英

Spring Boot App中的java.io.FileNotFoundException

[英]java.io.FileNotFoundException in Spring Boot App

我有一個使用彈簧靴的小型應用程序。 在此應用程序中,我讀取了一個位於該應用程序運行所在的本地系統磁盤中的conf文件。

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

我在指定位置也有一個conf.json文件。 但是當我運行Spring Boot應用時,它說

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運行此應用程序時,它運行沒有問題。

經典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

資源鏈接: 如何在Java中讀取文件– BufferedReader

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM