从包访问文件 [英] Accessing file from package

查看:52
本文介绍了从包访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我将文件a.txt放入com.xyz包中,然后尝试按以下方式访问它.能行吗?

suppose I put a file a.txt in package com.xyz and the try access it like following. Will it work?

大家好,

          import com.xyz.*;
          public class Hello
          { 
            File f = new File("a.txt");
             ...
          }

它对我不起作用.有什么解决方法吗?

It is not working for me. Is there any workaround?

推荐答案

基于您对以上评论的回答.如果您正在寻找解决方法,只需在文件系统上指定.txt文件的路径即可.将其放在包装中无济于事.

Based on your responses to the comments above. If you are looking for a work around, just specify the path to the .txt file on the file system. Putting it in a package does not help.

新文件("a.txt")

在文件系统上而不是在包中查找文件.

looks for a file on the the file system and not within a package.

也请阅读File上的javadocs: http://download.oracle.com/javase/6/docs/api/java/io/File.html

Please also read the javadocs on File: http://download.oracle.com/javase/6/docs/api/java/io/File.html

但是,除非您想将其用作资源,否则我看不出将文件放入包中的基本原理.在这种情况下,@ jcomeau_ictx有正确的解决方案

However I do not see the rationale in putting the file inside a package unless you would want to use it as a resource. In which case @jcomeau_ictx has the right solution

这篇关于从包访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆