文件管理 · 2022年7月31日

页面如何修改properties文件|如何修改properties数据配置文件

1. 如何打开和修改后缀名为properties的文件呢

properties文件包含数据库的一般配置你看他是用什么数据库软件做的,就用相应的软件打开。

2. java修改properties文件 丢失内容

我注意到你写的这句FileOutputStream fos=new FileOutputStream(resourceFile);运行时会马上覆盖掉原有的内容,因此你这句话应该移到将键值对载入完成后。

修改如下,测试通过。

/***新增或修改资源文件的内容**@paramresourceFile*资源文件(绝对路径+文件名,不需要.properties后缀)*@paramkey键*@paramvalue值*/publicstaticvoidsetString(StringresourceFile,Stringkey,Stringvalue){Propertiesprop=newProperties();try{if(resourceFile.indexOf(".properties")==-1){resourceFile+=".properties";}FileInputStreamfis=newFileInputStream(resourceFile);try{prop.load(fis);fis.close();prop.setProperty(key,value);FileOutputStreamfos=newFileOutputStream(resourceFile);prop.store(fos,"CopyrightThcic");fos.close();}catch(IOExceptione){e.printStackTrace();System.out.println("修改资源文件:"+resourceFile+"异常!msg:"+e.getMessage());}}catch(FileNotFoundExceptione){e.printStackTrace();System.out.println("无法获得资源文件:"+resourceFile);}}

3. 修改properties文件key的value

你说的很对:需要FileInputStreamfs=newFileInputStream(路径)Propertiesp=newProperties();p.load(fs);这只是把磁盘中的文件数据库加载到内存中让后设置值p.setProperty(key,value);修改后数据依然还在内存。要想保存到文件必须把它从内存中传到磁盘文件此时FileOutputStreamfos=newFileOutputStream(路径)p.store(fos);之后才把修改的文件内容保存到磁盘。

4. 如何修改properties文件

你说的很对:需要FileInputStream fs=new FileInputStream(路径) Properties p=new Properties(); p.load(fs); 这只是把磁盘中的文件数据库加载到内存中 让后设置值 p.setProperty(key,value); 修改后数据依然还在内存。要想保存到文件必须…

5. javaweb 修改properties文件中的属性值

String realPath1 = getServletContext().getRealPath("/");//这样你就得到了 你的CLASS目录 然后再拼装即可 试用于非WAR包 String realPath2 = getServletContext().getResource("/").getPath();//试用于所有上面的都是获取到了CLASS目录 也就是你工程编译后的目录 然后再根据目录层级拼装即可注意 web项目部署到服务器后 用你之前的方式是拿不到文件的 因为部署到服务器上面后的目录都变了 而且文件都放入CLASS目录下面了 所以你那错的

6. javaweb项目发布到tomcat后如何修改属性文件(.properties)

String realPath1 = getServletContext().getRealPath("/");//这样你就得到了 你的CLASS目录 然后再拼装即可 试用于非WAR包 String realPath2 = getServletContext().getResource("/").getPath();//试用于所有 上面的都是获取到了CLASS目录

7. Java修改、删除properties文件内容

java 修改properties文件,参考如下:public static void main(String[] args)throws Exception {Properties prop = new Properties();// 属性集合对象FileInputStream fis = new FileInputStream("src/test.properties");// 属性文件输入流prop.load(fis);// 将属性文件流装载到Properties对象中fis.close();// 关闭流// 获取属性值,sitename已在文件中定义System.out.println("获取属性值:password=" + prop.getProperty("password"));// 获取属性值,country未在文件中定义,将在此程序中返回一个默认值,但并不修改属性文件// System.out.println("获取属性值:country=" + prop.getProperty("country", "中国"));

8. 如何使用xconfmanager修改properties文件

因为在后台有对这个文件进行加载和解析。把conf.properties这个文件里面的数据全部解析到一个对象里。 然后在其他地方就可以通过这个对象来获取配置属性。 等程序写好后,以后要修改数据,直接在配置文件里修改,程序就不用修改,也不用重新编译…

9. 如何修改properties数据配置文件

public void writeVaule(){Properties pro=new Properties();InputStream in=pro.getClass().getResourceAsStream("/dbConfig/dbsqlsever.properties");try {pro.load(in);pro.setProperty("driver", "mysql");pro.setProperty("url", "jdbce");pro.setProperty(username, "lijia");pro.setProperty("password", "125487");OutputStream os = null;os = new FileOutputStream(new File(pro.getClass().getResourceAsStream("/dbConfig/dbsqlsever.properties").toURI()));pro.store(os, null);os.flush();os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}