0

For the framework our team is building, we are required to make a text file that we will edit before we run tests. That text file has two lines - the URL of our web application, and the location of an excel file that contains test cases.

Right now, for reading the file, I've been using Scanner.

 private static void readFile(String fileName) {
   try {
     File file = new File(fileName);
     Scanner scanner = new Scanner(file);
     while (scanner.hasNextLine()) {
       System.out.println(scanner.nextLine());
     }
     scanner.close();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   }
 }

My code lingo isn't the best so try to get what I'm asking:

Could someone point me in the right direction of extracting these two lines (the URL and the Excel path) from the text file, assigning them to two different variables/objects/functions/whatever you really want to call them, and than passing them into the main test script, so the test script knows what it wants to do.

I drew a picture in paint. Took 100 hours.

100 hours in paint

jagdpanzer
  • 693
  • 2
  • 10
  • 35
  • What's the format of the file? As is, your code should be able to read them just fine as long as each path is on its own line. – npinti Oct 14 '15 at 14:19
  • 1
    how about the Properties object ? https://docs.oracle.com/javase/tutorial/essential/environment/properties.html – svarog Oct 14 '15 at 14:21
  • 1
    The format is a .txt. It's a little notepad. It reads it fine, I'm just wondering how to get it to read it than pass it into my script. @svarog Haven't ever used Properties. I'll read that right now and let you know. – jagdpanzer Oct 14 '15 at 14:24
  • Try using Reader Objects https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html and assign by String str1 = reader.readLine() – Einstein Oct 14 '15 at 14:26
  • Declare an int variable and just +1 for each line. Then you can do an 'if int == 0 (url = scanner.nextLine()). However, if you do this, it won't allow you to add lines to that text file. You'll have to explicitly declare what each line of that txt file is. – Ryan C Oct 14 '15 at 14:27
  • @RyanCarlisle But if my .txt file will always only have two lines, that shouldn't be a problem right? I could assign line 0 to a URL variable and line 1 to the Excel path variable and than pass them? – jagdpanzer Oct 14 '15 at 14:32
  • 1
    Correct, if you don't change that text file layout, you will be fine. See below answer. – Ryan C Oct 14 '15 at 14:36

2 Answers2

1
private String urlText;
private String excelLocation;

private static void readFile(String fileName) {
   try {
     int lineNumber = 0;
     File file = new File(fileName);
     Scanner scanner = new Scanner(file);
     while (scanner.hasNextLine()) {
       lineNumber++;
       if (lineNumber == 1){
       urlText = scanner.nextLine();
       }
       else{
       excelLocation = scanner.nextLine();
     }
     scanner.close();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   }
 }

If you wanted to be more verbose, you can also use a LineNumberReader. See here: How to get line number using scanner

Community
  • 1
  • 1
Ryan C
  • 572
  • 5
  • 18
1

You can use the Properties Object

to write to a file:

Properties prop = new Properties();
OutputStream output = null;

try {

    output = new FileOutputStream("yourfile.txt");

    // set the properties value
    prop.setProperty("database", "localhost");
    prop.setProperty("dbuser", "john");
    prop.setProperty("dbpassword", "password");

    // save properties to project root folder
    prop.store(output, null);

} catch (IOException io) {
    io.printStackTrace();
} finally {
    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

to read:

Properties prop = new Properties();
InputStream input = null;

try {

    input = new FileInputStream("yourfile.txt");

    // load a properties file
    prop.load(input);

    // get the property value and print it out
    System.out.println(prop.getProperty("database"));
    System.out.println(prop.getProperty("dbuser"));
    System.out.println(prop.getProperty("dbpassword"));

} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

and you will get a "yourfile.txt" file with these lines:

dbpassword=password
database=localhost
dbuser=john

I've taken the code from: http://www.mkyong.com/java/java-properties-file-examples/

svarog
  • 9,477
  • 4
  • 61
  • 77