package com.gaotime.b;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;public class c {
public static void main(String[] args) {
Properties propertie;
FileInputStream inputFile;
propertie = new Properties();
try {
inputFile = (FileInputStream)Configuration.class.getResourceAsStream("/Test.properties");
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) {
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("装载文件--->失败!");
ex.printStackTrace();
} }}我在src目录下有个Test.properties文件
inputFile = (FileInputStream)Configuration.class.getResourceAsStream("/Test.properties");报错Exception in thread "main" java.lang.ClassCastException: java.io.BufferedInputStream cannot be cast to java.io.FileInputStream
为什么getResourceAsStream不能转成FileInputStream ?
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;public class c {
public static void main(String[] args) {
Properties propertie;
FileInputStream inputFile;
propertie = new Properties();
try {
inputFile = (FileInputStream)Configuration.class.getResourceAsStream("/Test.properties");
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) {
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("装载文件--->失败!");
ex.printStackTrace();
} }}我在src目录下有个Test.properties文件
inputFile = (FileInputStream)Configuration.class.getResourceAsStream("/Test.properties");报错Exception in thread "main" java.lang.ClassCastException: java.io.BufferedInputStream cannot be cast to java.io.FileInputStream
为什么getResourceAsStream不能转成FileInputStream ?
BufferedInputStream 继承FilterInputStream根本不是一个东西,如何转化
你这样写很好,谢谢了,但是就是碰到了这个错误,无法解决
getResourceAsStream返回的是一个InputStream,但是转化成FileInputStream的时候就是报错,想知道原因
if (!connected) {
try {
filename = file.toString();
isDirectory = file.isDirectory();
if (isDirectory) {
files = (List) Arrays.asList(file.list());
} else {
is = new BufferedInputStream(new FileInputStream(filename)); // Check if URL should be metered
boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET");
if (meteredInput) {
ProgressSource pi = new ProgressSource(url, "GET", (int) file.length());
is = new MeteredStream(is, pi, (int) file.length());
}
}
} catch (IOException e) {
throw e;
}
connected = true;
}
}
public synchronized InputStream getInputStream()
throws IOException { int iconHeight;
int iconWidth; connect(); if (is == null) {
if (isDirectory) {
FileNameMap map = java.net.URLConnection.getFileNameMap(); StringBuffer buf = new StringBuffer(); if (files == null) {
throw new FileNotFoundException(filename);
} Collections.sort(files, Collator.getInstance()); for (int i = 0 ; i < files.size() ; i++) {
String fileName = (String)files.get(i);
buf.append(fileName);
buf.append("\n");
}
// Put it into a (default) locale-specific byte-stream.
is = new ByteArrayInputStream(buf.toString().getBytes());
} else {
throw new FileNotFoundException(filename);
}
}
return is;
}分三种情况:
1. 如果resource是文件,那么返回的是一个BufferedInputStream(new FileInputStream(filename))
2. 如果resource是目录,那么返回的是一个列出目录下所有文件名的ByteArrayInputStream
3. 如果需要测量,那么返回的是一个MeteredStream三种中任一种都不是FileInputStream的实例,再看看InputStream/FileInputStream/FilterInputStream/BufferedInputStream的类层次,也就知道为啥不行了