2

I was wondering how I would create a java program which reads from a file and assigns variables.

import java.io.*;

public class Reader {
    public static void main(String[] args) {
        String str;
        String time=null;
        try  {
            BufferedReader reader = new BufferedReader(new FileReader("pathfilehere"));
            while((a = reader.readLine()) != null) {
                reader.readLine();
            }
        } catch(FileNotFoundException e){System.out.println("Error: "+e.getMessage());
        } catch(IOException e){System.out.println("Error: "+e.getMessage());
        }
        System.out.println(time);
    }
}

If I have a txt file with a variable "str" and its value is "randomstring", my question is how can I tell my java program to assign this variable from the text file and store it into the java program to use later on?

4
  • Read the text file into a Map<String, String>. Hopefully you're using a standard serialization, like JSON, so you don't have to roll your own parser. Commented Sep 11, 2013 at 2:27
  • 2
    If you have key values pairs, then you can use the Properties class which can read the key value paired data in a file and interpret them as properties Commented Sep 11, 2013 at 2:28
  • +1 for "use the Properties class". Unless I'm reading the question wrong, that's exactly what you're looking for. Here's the JavaDoc for Properties. Here's a related question and here's a tutorial. Commented Sep 11, 2013 at 2:39
  • java has serialization : store object to file and recreate same obj when you need , is that what you need . tutorialspoint.com/java/java_serialization.htm Commented Sep 11, 2013 at 3:25

1 Answer 1

1
import java.io.*;

public class Reader {
    public static void main(String[] args) {

        BufferedReader reader = null;
        StringBuilder randomstring =  new StringBuilder();
        try  {
             reader = new BufferedReader(new FileReader("file.txt"));


            String line = reader.readLine();
            while(line !=  null) {
                randomstring.append(line + '\n');

                 line = reader.readLine();
            }
        } catch(FileNotFoundException e){System.out.println("Error: "+e.getMessage());
        } catch(IOException e){System.out.println("Error: "+e.getMessage());
        }
        finally{
            try{
                reader.close();
            }catch(Exception E){}
        }
        System.out.println(randomstring.toString());
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

How does this code sample answer the question? Looks like it's just printing out the lines in the file, not making them available for later use by the code.
It looks great, but lets just say for example, I'm making a bank account program. I need to be able to read the accounts information from the file and then use it later on in the program? How would I go upon doing that? (I quickly made the program used in the example just to show in a way what I wanted to do, although it does just print out the value in the txt file, I would like to know how I can use the value in the txt file later on)

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.