1

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.

1
  • you could check the value of parts[0] and based on this value you could do var_xxx = parts[1]. Commented Jan 14, 2016 at 11:23

2 Answers 2

1

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
    } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the answer. however, i can't use properties class.
0

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();
        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.