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?

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Arc
  • 441
  • 1
  • 9
  • 26
  • Read the text file into a `Map`. Hopefully you're using a standard serialization, like JSON, so you don't have to roll your own parser. – Matt Ball Sep 11 '13 at 02: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 – Prasad Kharkar Sep 11 '13 at 02: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](http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html). Here's a [related question](http://stackoverflow.com/q/1318347/778118) and here's a [tutorial](http://www.mkyong.com/java/java-properties-file-examples/). – jahroy Sep 11 '13 at 02:39
  • java has serialization : store object to file and recreate same obj when you need , is that what you need . http://www.tutorialspoint.com/java/java_serialization.htm – Srinath Ganesh Sep 11 '13 at 03:25

1 Answers1

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());
    }
}
upog
  • 4,965
  • 8
  • 42
  • 81
  • 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. – lreeder Sep 11 '13 at 03:02
  • 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) – Arc Sep 11 '13 at 12:58