我有一个文件存储在C:/file.txt中。属性文件location.properties仅包含路径,即C:/file.txt。我想读取属性文件,获取位置,读取文件并显示所有内容。
但是我收到fileNotFound异常。有谁能够帮助我?这是我的代码:

package com.tcs.fileRead;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class ReadFile {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Properties prop = new Properties();

        try {

            prop.load(new FileInputStream("location.properties"));
            //prop.load(fileIn);
            String loc = prop.getProperty("fileLoc");
            System.out.println(loc);

            BufferedReader buffer;
            buffer = new BufferedReader(new FileReader(loc));
            String line;
            while((line =buffer.readLine())!= null)
            {
                System.out.println(line);
            }



        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}


这是输出:

"C:\file.txt"
java.io.FileNotFoundException: "C:\file.txt" (The filename, directory name, or volume label syntax is incorrect.)
    at java.io.FileInputStream.<init>(FileInputStream.java:156)
    at java.io.FileInputStream.<init>(FileInputStream.java:111)
    at java.io.FileReader.<init>(FileReader.java:69)
    at com.tcs.fileRead.ReadFile.main(ReadFile.java:29)

最佳答案

您的属性文件中的路径用引号引起来,因此您尝试打开"C:\file.txt"(这不是有效路径)而不是C:\file.txt

关于java - 使用属性文件中的位置读取文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28020924/

10-11 22:54
查看更多