繁体   English   中英

使用属性文件中的位置读取文件

[英]Read a file using location from properties file

我有一个文件存储在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM