您现在的位置是:群英 > 开发技术 > 编程语言
java中使用相对路径读取文件的方法是什么?
Admin发表于 2022-05-31 17:39:482000 次浏览
今天就跟大家聊聊有关“java中使用相对路径读取文件的方法是什么?”的内容,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

java中使用相对路径读取文件的方法:1、使用文件【File file = new File(“src/test.txt”)】方法;2、使用类的相对路径;3、使用当前线程的类加载器;4、读取web工程下的文件。

【相关学习推荐:java基础教程】

java中使用相对路径读取文件的方法:

一、简单粗暴的 File file = new File(“src/test.txt”);

  @Test
    /**
     * 这种方法 “” 空代表的是 这个Java项目 TestSomeTechnology  由于实际项目在打包后没有src目录 所以这种方法不常用
     */
    public  void  testMethod1() throws IOException{
        File file = new File("src/test.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String len = null;
        while ((len=br.readLine())!=null){
            System.out.println(len);
        }
    }

二、使用类的相对路径

TestRelativePath.class.getResource(“/test.txt”).getFile()

 @Test
    /**
     * 使用类的相对路径
     * 这种方法 “/” 代表的是bin。  src文件夹和resources 文件夹下的的东西都会被加载到bin下面 因为这两个文件被配置为了source
     */
    public  void  testMethod2() throws IOException{
        File file = new File(TestRelativePath.class.getResource("/test.txt").getFile());
        BufferedReader br = new BufferedReader(new FileReader(file));
        String len = null;
        while ((len=br.readLine())!=null){
            System.out.println(len);
        }
    }

三、使用当前线程的类加载器

Thread.currentThread().getContextClassLoader().getResource(“test.txt”).getFile()

@Test
    /**
     * 这种是通过当前线程的类加载器
     * 这种方法 “ ” 空代表的是bin 。 于是就直接填写test,文件夹下的的东西都会被加载到bin下面,因为这两个文件被配置为了source
     */
    public  void  testMethod3() throws IOException{
        File file = new File(Thread.currentThread().getContextClassLoader().getResource("test.txt").getFile());
        BufferedReader br = new BufferedReader(new FileReader(file));
        String len = null;
        if ((len=br.readLine())!=null){
            System.out.println(len);
        }
    }

四、读取web工程下的文件 使用getRealPath()读取

目录如下 :

读取 index.jsp

@WebServlet(name = "TestServlet",urlPatterns = "/TestServlet")
public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
    }
    /**
     *  web工程的根目录是 webRoot, 使用 “/” 代表webroot webroot下面有index.jsp文件
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        File file = new File(getServletContext().getRealPath("/index.jsp"));
        BufferedReader br = new BufferedReader(new FileReader(file));
        String len = null;
        while ((len=br.readLine())!=null){
            System.out.println(len);
        }
    }
}

读取 test.txt文件

不过如果想读取test.txt 的话 我们可用用上面的方式

 File file = new File(Thread.currentThread().getContextClassLoader().getResource("test.txt").getFile());

也可以使用 getRealPath()

不过由于是以 webroot为根目录 我们需要从classes里面读:idea的classes等价于esclipse的classpath 。classes说明:

代码:

File file = new File(getServletContext().getRealPath("/WEB-INF/classes/test.txt"));

getResourceAsStream()方法详解

getResourceAsStream()用法与getResouce()方法一样的,用getResource()取得File文件后,再new FileInputStream(file) 与 getResourceAsStream() 的效果一样。。

两个代码效果一样

InputStream inputStream1 = new FileInputStream(new File(Thread.currentThread().getContextClassLoader().getResource("test.txt").getFile()));
InputStream inputStream2 = Thread.currentThread().getContextClassLoader().getResourceAsStream("test.txt");

关于“java中使用相对路径读取文件的方法是什么?”的内容就介绍到这,感谢各位的阅读,相信大家对java中使用相对路径读取文件的方法是什么?已经有了进一步的了解。大家如果还想学习更多知识,欢迎关注群英网络,小编将为大家输出更多高质量的实用文章!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: java相对路径
相关信息推荐
2022-09-06 17:50:15 
摘要:什么是this?下面本篇文章给大家介绍一下JavaScript中的this,并聊聊this在函数不同调用方式下的区别,希望对大家有所帮助!
2022-02-07 18:19:10 
摘要:这篇文章我们来了解JSTL如何遍历集合,这里我们使用到c:forEach标签可以实现循环遍历,下文有详细的介绍,有需要的朋友可以参考,接下来就跟随小编来一起学习一下吧!
2021-12-14 17:59:19 
摘要:篇文章我们来了解PHP垃圾回收机制的相关内容,本文通过PHP垃圾回收机制的相关示例给大家介绍,文中的解释很清楚,对大家学习PHP垃圾回收机制有一定的帮助,有需要的朋友可以参考,接下来就跟随小编来一起学习一下吧!
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部