Accessing a file using Properties

Asked

Viewed 591 times

0

I am working with a method that takes as argument an object from the Properties class:

public static void main(String[] args) {
        //Properties props = new Properties();
        Properties props = new Properties();
        props.setProperty("TREINAMENTO.txt", "E:\\USER\\Documents\\Interface");

    }

In the setup method, I have:

public void setUp(Properties props) {


        props.getProperty("TREINAMENTO.txt", "E:\\USER\\Documents\\Interface");

        }

I need to access the TREINAMENTO.txt file, which has the format of a double matrix, and put it into a training variable[][]. How can I do this? I don’t quite understand how the Properties class works.

1 answer

0

Properties, are configuration files that work with pairs of chave and valor - these keys and values are always Strings.

You use the chave to recover the valor that you save in this configuration file.

Here’s an example of how to read your file (put it in your project folder):

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class LerPropriedades {
    public static void main(String[] args) throws IOException {

        FileInputStream in = new FileInputStream("TREINAMENTO.txt");
        Properties propriedades = new Properties();
        propriedades.load(in);
        in.close();

        for(String chave : propriedades.stringPropertyNames()) {
              String valor = propriedades.getProperty(chave);
              System.out.println(chave + ": " + valor);
            }
    }
}

You can also try creating a property file to see how it should be structured in this way:

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class SalvarProperties {
    public static void main(String[] args) throws IOException {
        Properties propriedades = new Properties();

        propriedades.setProperty("0", "1.14");
        propriedades.setProperty("1", "132.495");

        FileOutputStream out = new FileOutputStream("TREINAMENTO.txt");
        propriedades.store(out, "---comentario---");
        out.close();
    }
}

If you want to put the read information on array, you can do it this way (sorry, I don’t know how to do it better):

Double meuArray[][] = new Double[10][2];
int counter = 0;

for(String chave : propriedades.stringPropertyNames()) {
    String valor = propriedades.getProperty(chave);
    meuArray[counter][0] = Double.parseDouble(chave);
    meuArray[counter][1] = Double.parseDouble(valor);
    counter++;
}

Just replace the loop for of LerPropriedades by that code. I used the following file to test:

#TREINAMENTO
#25/05/2017
0=2.43
1=1.2343
2=15.32
3=80.55
4=532.0
5=943.1
6=9.0
7=3.00038
8=65.12
9=200.01

See here more information

  • But one thing I didn’t quite understand, Daniel: in my case, the matrix has 1000 rows and 20 columns, would I need to start a file for each column? Because, as I understand it, meuArray needs the column of values and the column of keys...

  • @Do you mean something like a key for 20 values? You can use some separator in the values, for example comma, and save them like this: 0=1.32,2.54,17.12,80.1 - then at the time of reading you separate them again - and so you keep a single file.

  • @But I had understood that you already had a file to read - what is the structure of it? To read it as Properties it kind of necessarily has to be structured as keys and values... :

  • It has values separated by space, as for example 0.28 1.36 4.24 8.66 1.41 1.71 6.99 12.5 In that case, should I insert a separator, such as a semicolon, into the file itself?? In the example you gave, the key 0 is associated with line 1.32,2.54,17.12,80.1??

  • @donut If you you already have a file with 1000 lines, maybe you can’t open it the way I indicated - pq. the program expects the correct structure (did you even try?) - and I think it makes no sense for you to change your text file to be "compatible" with Properties - makes more sense to read the file and figure out a way to turn it into properties within the program. It may be interesting you edit your question, and include more information - including snippets from your archive, if possible.

  • @donut Yes, in case the key 0 would be associated with the whole line - but in your example, I didn’t understand where the key is - it’s just run numbers separated by space?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.