如何从路径中读取java中的文件我的路径如下:“path”:“C:\\ somefolder \\ otherfoldet \\ abc.war \\ acm.jar \\ a.properties”

时间:2018-06-05 11:32:23

标签: java json

如何从java

中的指定路径读取文件

代码

public void readPropertyFile(String path)throws IOException {

File fp = new File(path);
        FileReader  reader = new FileReader(fp);//        java.io.FileNotFoundException: C:\json\trm\trm.jar\quartz.properties (The system cannot find the path specified)

        Properties properties = new Properties();
        properties.load(reader)
}

Exption:
java.io.FileNotFoundException: C:\json\trm\trm.jar\quartz.properties (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)....

1 个答案:

答案 0 :(得分:0)

由于您正在从WAR(acm.war)或JAR(acm.jar)内部阅读,因此需要ZipInputStreamJarInputStream

String entryName = "a.properties";
Path path = Paths.get("C:", "somefolder", "abc.war");

try (ZipInputStream zipIn = new ZipInputStream(Files.newInputStream(path))) {
  ZipEntry entry;
  while ((entry = zipIn.getNextEntry()) != null) {
    if (entry.getName().equals(entryName) {
      // read zipIn content here to get the entry
    }
  }
}