2

我想在当前类的同一个包中创建一个新文件。

FileOutputStream fileOut = new FileOutputStream(YYY);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);

为此目的,正确的 YYY 是什么?

我试过这个:

FileOutputStream fileOut = new FileOutputStream("myObject");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);

但“myObject”文件创建于:c:\program files\eclipse

请帮忙!谢谢

编辑:也许值得一提的是这是一个 SVN 项目。也许与此有关

4

2 回答 2

0

测试简单案例:

URL resource = this.getClass().getClassLoader().getResource(this.getClass().getName().concat(".class"));

这应该为您提供 .class 文件。请记住 .java 文件甚至可能不在系统上。

于 2012-11-10T13:25:13.750 回答
0

What you want is not possible because there might not be a .java file around (only a .class file), the .class file might reside inside a (nested) .jar file and the JVM does not know which classpath entry was used to locate the .class file.

That said, I have tried to solve the same problem for a project of mine. The code goes like this:

    private static List<Class> getClassesForPackage(String packagename)
            throws ClassNotFoundException
    {
        // This will hold a list of directories matching the pckgname.
        // There may be more than one if a package is split over multiple
        // jars/paths
        List<Class> classes = new ArrayList<Class>();
        List<File> directories = new ArrayList<File>();
        try {
            ClassLoader classLoader =
                Thread.currentThread().getContextClassLoader();
            if (classLoader == null) {
                throw new ClassNotFoundException("Can't get class loader.");
            }

            // Ask for all resources for the path
            Enumeration<URL> resources =
                classLoader.getResources(packagename.replace('.', '/'));
            while (resources.hasMoreElements()) {

                URL res = resources.nextElement();
                if (res.getProtocol().equalsIgnoreCase("jar")) {

                    JarURLConnection conn =
                        (JarURLConnection) res.openConnection();
                    JarFile jar = conn.getJarFile();
                    for (JarEntry e : Collections.list(jar.entries())) {

                        if (e.getName().startsWith(
                                packagename.replace('.', '/'))
                            && e.getName().endsWith(".class")
                            && !e.getName().contains("$"))
                        {
                            String className =
                                e.getName().replace("/", ".").substring(
                                        0,
                                        e.getName().length() - 6);
                            classes.add(Class.forName(className));
                        }
                    }
                }
                else
                    directories.add(new File(URLDecoder.decode(
                            res.getPath(),
                            "UTF-8")));
            }
        }
        catch (NullPointerException x) {
            throw new ClassNotFoundException(packagename
                + " does not appear to be "
                + "a valid package (Null pointer exception)");
        }
        catch (UnsupportedEncodingException encex) {
            throw new ClassNotFoundException(packagename
                + " does not appear to be "
                + "a valid package (Unsupported encoding)");
        }
        catch (IOException ioex) {
            throw new ClassNotFoundException(
                "IOException was thrown when trying "
                    + "to get all resources for " + packagename);
        }

        List<String> subPackages = new ArrayList<String>();
        // For every directory identified capture all the .class files
        for (File directory : directories) {

            if (directory.exists()) {

                // Get the list of the files contained in the package
                File[] files = directory.listFiles();
                for (File file : files) {

                    // add .class files to results
                    String fileName = file.getName();
                    if (file.isFile() && fileName.endsWith(".class")) {
                        // removes the .class extension
                        classes.add(Class.forName(packagename + '.'
                            + fileName.substring(0, fileName.length() - 6)));
                    }
                    // add directories to subpackages
                    if (file.isDirectory()) {
                        subPackages.add(packagename + "." + fileName);
                    }
                }
            }
            else {
                throw new ClassNotFoundException(packagename + " ("
                    + directory.getPath()
                    + ") does not appear to be a valid package");
            }
        }
        // check all potential subpackages
        for (String subPackage : subPackages) {
            classes.addAll(getClassesForPackage(subPackage));
        }
        return classes;
    }

To find the directory of the corresponding .java file you need to configure the source directory you are using.

于 2012-11-10T13:48:32.383 回答