Skip to content

Commit 6817646

Browse files
committed
提高资源加载的健壮性
1 parent c096bc5 commit 6817646

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/main/java/com/hankcs/hanlp/corpus/io/IOUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,4 +662,21 @@ public static void writeCustomNature(DataOutputStream out, LinkedHashSet<Nature>
662662
TextUtility.writeString(nature.toString(), out);
663663
}
664664
}
665+
666+
/**
667+
* class.getResourceAsStream的wrapper,在资源不存在的情况下抛出IOException,
668+
* @param path
669+
* @return
670+
* @throws FileNotFoundException
671+
*/
672+
public static InputStream getResourceAsStream(String path) throws FileNotFoundException
673+
{
674+
InputStream is = IOUtil.class.getResourceAsStream(path);
675+
if (is == null)
676+
{
677+
throw new FileNotFoundException("资源文件" + path + "不存在于jar中");
678+
}
679+
680+
return is;
681+
}
665682
}

src/main/java/com/hankcs/hanlp/corpus/io/ResourceIOAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ResourceIOAdapter implements IIOAdapter
2121
@Override
2222
public InputStream open(String path) throws IOException
2323
{
24-
return IOUtil.isResource(path) ? IOUtil.class.getResourceAsStream("/" + path) : new FileInputStream(path);
24+
return IOUtil.isResource(path) ? IOUtil.getResourceAsStream("/" + path) : new FileInputStream(path);
2525
}
2626

2727
@Override

0 commit comments

Comments
 (0)