1

I come from C so working with files in java is kind of tricky for me.

I want to have the following file:

a = 3 b = 5 c = 1 d = 10

I want to be able to read whenever I want the values for a,b,c,d and also to be able to change the values for some of them.

I have tried a few things but I am not even close. Can anyone help?

reto
  • 9,995
  • 5
  • 53
  • 52
spoke
  • 257
  • 1
  • 6
  • 19
  • 4
    Try to divide your problem, tackling it step by step. What is your current solution to read the file? The internet offers plenty of resources for this question. – reto Aug 11 '15 at 06:46
  • 1
    If you can choose the formatting of your file, I would recommend something with delimiters like `a = 3; b = 5; c = 1; d = 10;`, so it is easier to read the file. – Klaus Aug 11 '15 at 06:49
  • @KlausPrinoth I would go even one step further: why having more than one key value pair per line? Makes parsing much harder than it ought to be. – GhostCat Aug 11 '15 at 06:54
  • 2
    Side note: coming from C is not an excuse to come up with bad questions. This question is very unspecific; it almost reads like "please tell me everything I need and do the work for me". As reto said: focus on smaller parts; and resolve them step by step. And: solving a concrete problem is one way to learn a new language; but not always the best one. Whats wrong with reading some books, tutorials first; to get a glimpse "of the whole thing" - before diving into very specific and narrow coding activities? – GhostCat Aug 11 '15 at 06:56
  • I added an answer with an example for the Properties class. If the OP is able to change the style to line by line pairs, then this is the easiest solution i could come up with. – Abbel Aug 11 '15 at 06:59
  • @Abbel where did you add the example? – spoke Aug 11 '15 at 07:11
  • sorry for the late response [here](http://stackoverflow.com/a/31934977/4225763) as a link on the word Properties – Abbel Aug 11 '15 at 08:42

4 Answers4

1

If you are able to change the format for a line by line style for the values you could have a look at the Properties in Java. Here you can define Key value pairs, line by line with a separator between key and value that you can specify.

Aaron Esau
  • 1,083
  • 3
  • 15
  • 31
Abbel
  • 320
  • 1
  • 9
1

Sample code on load and Usage of Properties

import java.io.; import java.util.;

public class ReadLineExample {

public static void main(String args[]) throws FileNotFoundException {
    Properties ps =new Properties();
    // Create the file object
    File fileObj = new File("data.txt");
    try {

        FileInputStream fis = new FileInputStream(fileObj);
        ps.load(fis);
        System.out.println("Properties:"+ps);
        System.out.println("Get A:"+ps.getProperty("a"));

    } catch (Exception err) {

        err.printStackTrace();
    }
}

}

-- Format of data.txt

a = 3

b = 5

c = 1

d = 10

Output:

Properties:{b=5 , a=3 , d=10, c=1 } Get A:3

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

This is a very "NullPointer-ish" way to complete this task, but here's a sample

String[] list = fileContents.split("=");
String[] temp = list[1].split(" ");
a = temp[0]
temp = list[1].split(" ");
b = temp[0]
temp = list[2].split(" ");
c = temp[0]
temp = list[3].split(" ");
d = temp[0]

This way is not very flexible either. If I were you, I'd look at the Properties class. It makes this job way easier.

You could also write a function that parses the text and finds the values between the "=" sign and a " " (But you'd have to remove the " " between the "=" and the value...)

Aaron Esau
  • 1,083
  • 3
  • 15
  • 31
0

Since Properties is not recommended, I have bettered the solution with preferences.

package com.mypack.test;

import java.io.*;
import java.util.*;
import java.util.prefs.Preferences;

public class PreferencesExample {

    public static void main(String args[]) throws FileNotFoundException {
        Preferences ps = Preferences.userNodeForPackage(PreferencesExample.class);
        // Load  file object
        File fileObj = new File("data.xml");
        try {
            FileInputStream fis = new FileInputStream(fileObj);
            ps.importPreferences(fis);
            System.out.println("Prefereces:"+ps);
            System.out.println("Get property1:"+ps.getInt("property1",10));

        } catch (Exception err) {
            err.printStackTrace();
        }
    }
}

data.xml is in below format

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'>
<preferences EXTERNAL_XML_VERSION="1.0">
<root type="user">
<map />
<node name="com">
  <map />
  <node name="mypack">
    <map />
    <node name="test">
      <map>
        <entry key="property1" value="80" />
        <entry key="property2" value="Red" />
      </map>
    </node>
  </node>
</node>
</root>
</preferences>
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • Why isin't Properties recommended? I've used it and it works great – spoke Aug 13 '15 at 08:16
  • Properties is effectively deprecated. Visit this question: http://stackoverflow.com/questions/2358651/are-java-properties-effectively-deprecated – Ravindra babu Aug 13 '15 at 08:30