小樊 2011-10-11 14:04
浏览 451
已采纳

java类如何生成具有格式的.cfg文件

例如:#config.cfg
define server{
userName xiaoming
hostName window
alias XP
hostIP 192.168.1.1
}
define client{
userName changchun
hostName Linux
alias ubuntu
hostIP 192.168.1.2
}
define server{
userName xiaoming1
hostName window
alias XP1
hostIP 192.168.1.3
}
define client{
userName changchun1
hostName Linux
alias ubuntu1
hostIP 192.168.1.4
}
...
...

  • 写回答

3条回答 默认 最新

  • _1_1_7_ 2011-10-12 16:21
    关注

    其实很简单的
    [code="java"]

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;

    public class CfgWriter {
    private PrintWriter out;
    private static final String DEFINE = "define";

    public CfgWriter(File file) throws FileNotFoundException {
        super();
        this.out = new PrintWriter(file);
    }
    
    public void writeComment(String comment) {
        out.print('#');
        out.println(comment);
    }
    
    public void startDefine(String name) {
        out.print(DEFINE);
        out.print(' ');
        out.print(name);
        out.println('{');
    }
    
    public void writeProperty(String key, String value) {
        out.print(' ');
        out.print(key);
        out.print('\t');
        out.println(value);
    }
    
    public void endDefine() {
        out.println('}');
    }
    
    public void close() {
        out.close();
    }
    
    public static void main(String[] args) throws Exception {
        CfgWriter w = new CfgWriter(new File("d:/config.cfg"));
        w.writeComment("config.cfg");
        w.startDefine("Server");
        w.writeProperty("Id", "0001");
        w.writeProperty("Name", "xxxx");
        w.writeProperty("Amount", "100");
        w.endDefine();
        w.close();
    }
    

    }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 Error in check.length("fill") : 'gpar'成分'fill'的长度不能为零
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误