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 ?

解决方案 »

  1.   

     FileInputStream, FilterInputStream实现InputStream,
    BufferedInputStream 继承FilterInputStream根本不是一个东西,如何转化
      

  2.   

    propertie.load(Configuration.class.getResourceAsStream("/Test.properties"));这样有什么问题?非要转化?画蛇添足
      

  3.   

    但是getResourceAsStream返回的是一个InputStream,怎么就不能转化?
      

  4.   

    propertie.load(Configuration.class.getResourceAsStream("/Test.properties"));
    你这样写很好,谢谢了,但是就是碰到了这个错误,无法解决
    getResourceAsStream返回的是一个InputStream,但是转化成FileInputStream的时候就是报错,想知道原因
      

  5.   

    FileInputStream inputFile;改成InputStream就行,现在的也不要强制类型转换。Properties只要求InputStream
      

  6.   

    恩...还过一会就结贴吧,只能说javaAPI也有骗人的时候 #_#
      

  7.   

    看看jdk源码中关于文件的URLStreamHandler不就知道为啥不可以了public void connect() throws IOException {
            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的类层次,也就知道为啥不行了