usernameistaken
usernameistaken

Reputation: 13

Storing values in different variables by reading a file

i'm a beginner at java and still learning so please excuse my question if it sounds stupid.

i've been stuck on a straight forward problem i was given:

i'm supposed to read a text file and store the values of the text file in different variables. my text file looks like:

foo.txt

Directory_path=C:\University
school_name=SyracuseUni

i want to store the directory path and school_name in a new variable say

var_one = C:\University and var_two = SyracuseUni

I was able to split it but in a single string.

public static void main(String[] args) throws IOException {
        try {
            BufferedReader br = new BufferedReader(new FileReader("C:\\foo.txt"));
            String strLine = null;
            String var_one = null;
            String var_two = null;
            while ((strLine = br.readLine()) != null) {
                String[] parts = strLine.split("=");
                String parameter = parts[1];
                System.out.println(parameter);
            }
        }  
        catch (IOException e) {
            e.printStackTrace();
        }
    }

this gives me an output like this which isn't how i want it:

C:\University
SyracuseUni

i will appreciate if anyone can guide me towards the right approach. thanks all.

Upvotes: 1

Views: 59

Answers (2)

Schaka
Schaka

Reputation: 772

Something simple would be using Java Properties. You could also store values in a map. If you really insisted on filling two separate varibles, you could always count how many lines you've went across in your while loop and use switch/case to determine which variable to fill.

public static void main(String[] args) throws IOException {
        try {
            BufferedReader br = new BufferedReader(new FileReader("C:\\foo.txt"));
            String strLine = null;
            HashMap<String, String> map = new HashMap<String, String>();
            while ((strLine = br.readLine()) != null) {
                String[] parts = strLine.split("=");
                map.put(parts[0], parts[1]);
            }
            for (Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + " = " + value);
        }
        }  
        catch (IOException e) {
            e.printStackTrace();
        }
}

Upvotes: 0

Rozart
Rozart

Reputation: 1778

There is already a simple way to deal with such files using java.util.Properties class. This could be an overkill if you are simply trying to learn how to read a file.

public static void main(String[] args) {
    String myVar1 = null;
    String myVar2 = null;
    Properties prop = new Properties();
    InputStream input = null;
    try (FileInputStream input = new FileInputStream("pathToYourFile")) {
        prop.load(input);

        myVar1 = prop.getProperty("Directory_path");
        myVar2 = prop.getProperty("school_name");

    } catch (IOException ex) {
        //Handle exception
    } 
}

Upvotes: 1

Related Questions