-1

I have two file .First the property file and second the content file

first file

properties

{

"rowcount" : "3"

"delim" : "|"

}

second file

a|b|c

d|e|f

g|h|i

i want to read both the file and check whether the second file follows the rule .(the structure of the first file will always be similar )

Jahid
  • 21,542
  • 10
  • 90
  • 108
Avinash Nishanth S
  • 514
  • 1
  • 5
  • 15

2 Answers2

1

For the first part, simply read in the rowcount number from the first file and compare to the line count from the second file.

For the second part, read in delim and then use a Scanner or similar to read the second file using delim as your delimiter. If you only want a single character in between each delimiter, then test for this as you read the file, and throw an exception if you see more than a single character being read in.

From the example link:

import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class MainClass {
  public static void main(String args[]) throws IOException {

    char[] chars = new char[100]; //If you know how many you want to read
    //(if not, use an ArrayList or similar)

    FileReader fin = new FileReader("Test.txt");
    Scanner src = new Scanner(fin);

    // Set delimiters to newline and pipe ("|")
    //Use newline character OR (bitwise OR is "|") pipe "|" character
    //since pipe is also OR (thus a meta character), you must escape it (double backslash)
    src.useDelimiter(src.useDelimiter(System.getProperty("line.separator")+"|\\|");
    // Read chars
    for(int i = 0; src.hasNext(); i++) {
        String temp = src.next();
        if(temp.length != 1)
            System.out.println("Error, non char"); //Deal with as you see fit
        chars[i] = temp.charAt(0); //Get first (and only) character of temp
    }
    fin.close();
    //At this point, chars should hold all your data
  }
}
Community
  • 1
  • 1
River
  • 8,585
  • 14
  • 54
  • 67
1

Well, first of all i think the first file should be a properties.json with this syntax:

{
    properties: {
        rowcount: "3",
        delim: "|" 
    }
}

With a JSONParser you can read the JSON file and map it in a Object.

Then with a FileReader you can deal with the second file. For the validation part i think that a regular expression and a rowcount from the FileReader can easily solve the problem.

Community
  • 1
  • 1
IlGala
  • 3,331
  • 4
  • 35
  • 49