首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何通过FileOutputStream从java修改file.txt中的参数?

如何通过FileOutputStream从java修改file.txt中的参数?
EN

Stack Overflow用户
提问于 2019-06-11 05:23:56
回答 1查看 34关注 0票数 -1

问题是,我有一个file.txt,我将其扩展名改为file.properties,以便在应用程序中对其进行操作。

通过FileInputStream,我可以像这样咨询他们:

代码语言:javascript
复制
public static Properties config = new Properties();
public static InputStream configInput = null;
public static OutputStream configOutput = null;

try{
/*file.properties es el nombre del archivo ubicado en:
C:\Users\Gerencia\Documents\NetBeansProjects\LectorParametros
*/
        configInput = new FileInputStream("configuracion.properties");
        config.load(configInput);
        System.out.println(config.getProperty("usuario"));
        System.out.println(config.getProperty("clave"));
    } catch(Exception e){
        JOptionPane.showMessageDialog(null, "Error LEYENDO configuración\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }

我尝试过使用FileOutputStream:

代码语言:javascript
复制
try{
            configOutput = new FileOutputStream("configuracion.properties");
            config.setProperty("clave", "123456");
        } catch(Exception e){
            JOptionPane.showMessageDialog(null, "Error MODIFICANDO configuración\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

但它所做的是修改"configuracion.properties“的所有内容,我能做什么呢?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-11 05:41:59

你只是错过了store方法。下面是代码(剥离异常管理)。

代码语言:javascript
复制
configOutput = new FileOutputStream("configuracion.properties");
config.setProperty("clave", "123456");
config.store(configOutput, "Your comment here");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56533724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档