javaweb(实用) | 您所在的位置:网站首页 › java往文件里写数据怎么写 › javaweb(实用) |
首先idea下 真实路径与项目原本路径对比:
(文件操作之后idea内打开项目内的文件并没有任何变化,项目真实路径或者说编译后情况下的文件发生了变化) 项目中resources文件file.txt原本路径: D:\java\IDEAProjects\springDemo\firstWeb\resources\prop\file.txt(windows资源管理器的直接复制过来的目录) 项目中真实resources文件file.txt的路径:(因为配了输出的位置在classes文件夹下) 所以是:D:/java/IDEAProjects/springDemo/firstWeb/web/WEB-INF/classes/prop/file.txt 获得resources的文件路径: String path = this.getClass().getClassLoader().getResource("./prop/file.txt").getPath(); 输出path是:/D:/java/IDEAProjects/springDemo/firstWeb/web/WEB-INF/classes/prop/file.txt最前面会带一个“/” 左斜杠右斜杠问题请移步->https://blog.csdn.net/zlwzlwzlw/article/details/7768313/ 对resources文件夹下的文件可以直接这样读取 InputStream inputStream = WBTool.class.getClassLoader().getResourceAsStream("/coupon-export-template.xls");最好把文件放进resources进行读写,通过包的读取的文件会出现问题,也不好管理。(不管是网上加斜杠还是不加斜杠,文件流读取都是空指针,因为根本没有编译这些不是.java的文件,如果把它放进classes编译文件夹的话,就不是空指针了) String file = "/com/searlas/tool/coupon-export-template.xls"; InputStream is = FileTest.class.getResourceAsStream(file);所以读取配置文件放进resources文件夹比较好。 文件的操作经过尝试一下四种路径都可以实现写入,读取想必也可以 测试代码: @Test public void readTest(){ String path = this.getClass().getClassLoader().getResource("./prop/file.txt").getPath();//"./prop/file.txt"效果一样 System.out.println(path); System.out.println("---------------------------------------------------------------"); path = path.substring(1); System.out.println(path); System.out.println("--------------------------------------------------------------"); path = path.replaceAll("/", "\\\\"); System.out.println(path); System.out.println("--------------------------------------------------------------"); path = path.replaceAll("\\\\", "\\\\\\\\"); System.out.println(path); Date date = new Date(); String key = date.toString(); try { FileOutputStream fos = new FileOutputStream(new File(path)); fos.write(key.getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 小测试(写文件,也试试FileWriter)直接相对路径写入好像是会出现问题的 (下图注释的内容) // 测试写入目录 String root = people.getClass().getResource("/").getPath(); //File file = new File( "/key"); File file = new File( root + "key"); if (!file.exists()) { file.mkdir(); } //File peoFile = new File("/key" + "/people.ser"); File peoFile = new File(root + "key" + "/people.ser"); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } try { FileWriter fw = new FileWriter(peoFile); fw.write(1 + "\n1"); fw.close(); } catch (IOException e) { e.printStackTrace(); } 相对路径写文件会报权限问题permission denied应该是不能写入到编译的文件夹内,但是通过绝对路径访问项目内文件都是可以的。 如果是通过绝对路径访问编译文件夹下的文件会报错。找不到文件。C:\Users\searlas\IdeaProjects\newLearningDemo\java\target\classes\a.txt 对于文件写入,只要绝对路径就行了,一般对于文件存储也都是绝对路径文件夹内写入,相对路径写入classes文件夹应该是不怎么实用也会报错。 杂记附上项目内获取绝对路径的方法:(应该是他人总结的 以前笔记本里面发现的,侵删) 1可以在servlet的init方法里 String path = getServletContext().getRealPath("/"); 这将获取web项目的全路径 例如 :E:\eclipseM9\workspace\tree\ tree是我web项目的根目录 2.你也可以随时在任意的class里调用 this.getClass().getClassLoader().getResource("/").getPath(); 这将获取 到classes目录的全路径 例如 : E:\eclipseM9/workspace/tree/WEB-INF/classes/ 这个方法也可以不在web环境里确定路径,比较好用 3.request.getContextPath(); 是在开发Web项目时,经常用到的方法,是为了解决相对路径的问题,可返回站点的根路径。 获得web根的上下文环境 如 /tree tree是我的web项目的root context /*jsp 取得当前目录的路径 path=request.getRealPath(""); /*得到jbossWEB发布临时目录 warUrl=.../tmp/deploy/tmp14544test-exp.war/ path=C:\jboss-4.0.5.GA\server\default\tmp\deploy\tmp14544test-exp.war\ String path = (String)request.getContextPath();
|
CopyRight 2018-2019 实验室设备网 版权所有 |