简体   繁体   English

Java如何读取和写入内部属性文件?

[英]Java How do I read and write an internal properties file?

I have a file I'm using to hold system information that my program needs on execution. 我有一个文件,我用来保存我的程序执行时需要的系统信息。 The program will read from it and write to it periodically. 程序将从中读取并定期写入。 How do I do this? 我该怎么做呢? Among other problems, I'm having trouble with paths 在其他问题中,我遇到路径问题

Example

在此输入图像描述

How do I read/write to this properites file if deploying application as runnable jar 如果将应用程序部署为runnable jar,我该如何读/写这个属性文件

Take a look at the http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html 看一下http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html

You can utilize this class to use your key=value pairs in the property/config file 您可以利用此类在property / config文件中使用key = value对

Second part of your question, how to build a runnable jar. 问题的第二部分,如何构建一个可运行的jar。 I'd do that with maven, take a look at this : 我和maven一起做,看看这个:

How can I create an executable JAR with dependencies using Maven? 如何使用Maven创建具有依赖关系的可执行JAR?

and this : 和这个 :

http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

I see you're not using maven to build your project altogether 我发现你并没有使用maven来完全构建你的项目

You can't write to a file that exists as part of a ZIP file... it does not exist as a file on the filesystem. 您不能写入作为ZIP文件的一部分存在的文件...它不作为文件系统上的文件存在。

Considered the Preferences API? 考虑了Preferences API?

To read from a file you can declare a file reader using a scanner as 要从文件中读取,您可以使用扫描仪声明文件读取器

Scanner diskReader = new Scanner(new File("myProp.properties"));

After then for example if you want to read a boolean value from the properties file use 之后例如,如果要从属性文件中读取布尔值,请使用

boolean Example = diskReader.nextBoolean();

If you wan't to write to a file it's a bit more complicated but this is how I do it: 如果你不想写一个文件,它会有点复杂,但这就是我这样做的方法:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

public class UpdateAFile {

    static Random random = new Random();
    static int numberValue = random.nextInt(100);

    public static void main(String[] args) {
        File file = new File("myFile.txt");
        BufferedWriter writer = null;
        Scanner diskScanner = null;

        try {
            writer = new BufferedWriter(new FileWriter(file, true));
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            diskScanner = new Scanner(file);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        appendTo(writer, Integer.valueOf(numberValue).toString());
        int otherValue = diskScanner.nextInt();
        appendTo(writer, Integer.valueOf(otherValue + 10).toString());
        int yetAnotherValue = diskScanner.nextInt();
        appendTo(writer, Integer.valueOf(yetAnotherValue * 10).toString());

        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void appendTo(BufferedWriter writer, String string) {
        try {
            writer.write(string);
            writer.newLine();
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

And then write to the file by: 然后通过以下方式写入文件:

diskWriter.write("BlahBlahBlah");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM