• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2268
  • Last Modified:

Generic File Reader using Java that can read any comma or space or tab position in a file

PLEASE URGENTLY HELP.
What I mean by a generic file reader is that when the java code is reading the data it might start of identifying that the first comma is at position 6 (meaning that the data is 5 characters in length before the comma) in the first line. Whereas another file will have the first comma on the first line positioned at position 9 (meaning that the data is 8 characters in length before the comma). Can the java code handle these issues? Also can the java code handle commas inside the data field as well.

Also the Java code needs to skip reading the first line in the file which is only the header information.
The file reader in java must be able to read a file regardless of the different positions of where the comma or space delimiter is placed within the file.
Look at the data file in this question.

The Java code must also be bale to read the windows directory and grab the file in question that is going to be read.

The file that is attached is a comma delimited file. However, the code should be able to work no matter what the delimiter is.

Can you please help ? I really appreciate this.

GABinFixaq.txt
0
darrylf290567
Asked:
darrylf290567
  • 15
  • 14
1 Solution
 
darrylf290567Author Commented:
Hi All,

I forgot to mention that I am also using the Eclipse IDE for Java.

kindest regards
darrylf290567
0
 
cmalakarCommented:
Read line by line in the file..

and use String.split("") function to split the fields..

Refer

http://www.experts-exchange.com/Programming/Languages/Java/Q_22930007.html
0
 
darrylf290567Author Commented:
Hi cmalakar,

Could you provide me with your code please based on the text file that was in this question?

kindest regards
darylf290567
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
cmalakarCommented:
Sorry I cannot write the code..
0
 
darrylf290567Author Commented:
Hi cmalakar,

No that is not what I was looking for. I was hoping that you could have written the code considering that you are an expert in Java. I went onto the PLSQL section and there was a absolute brilliant PLSQL expert there who had helped me without question. I was hoping that you could have helped me. Your expert comment therefore has not helped me.
That is why i supplied you with the data file.

kind regards
darrylf290567
0
 
cmalakarCommented:
>> Sorry I cannot write the code..

It doesn't mean I am not able to write the code... :)

You please try yourself implementing the problem, with the help of first comment i have given..

If you are facing any kind of problem in implementing that.. we are here to help you..
0
 
darrylf290567Author Commented:
I have tried and it does not work. The only thing that I have been able to do so far is this:

import java.io.BufferedReader;
import java.io.FileReader;

public class ODSSFileReader
{
      public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new FileReader("C:/H1070927.REP"));
 
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
    }
}

That is why I need your expertise. I really appreciate it if you can help. I know also that you could write the code and I ma sorry if you had misinterpreted what I had meant. But I just need the help, that is why I asked the question in the first place.

Kind Regards
darrylf290567
0
 
cmalakarCommented:
What does.. this is printing ...

System.out.println(line);
0
 
cmalakarCommented:
inside while loop, you have to write...

String list[] = line.split(",");

now the list array will contain all the fields in the record...
0
 
darrylf290567Author Commented:
Hi cmalakar,

How can I then assign form the array into local variables the values and then I want to print out the values from the local variables or from the array?

Can you please help me with this?

kindest regards
darrylf290567
0
 
darrylf290567Author Commented:
Hi cmalakar,

How can i make this a generic reader where I can pass in the delimiter? The delimiter could be a space a ~ sign or  a tab? Can you please help me with the java code to do this?
kindest regards
darrylf290567
0
 
cmalakarCommented:

while ((line = in.readLine()) != null)
 {
        String list[] = line.split(",");
        for(int i = 0: i < list.length; i++)
        {
          //prints all the fields in a record in single line.. with space as delimiter..
          System.out.print(list[i] + " "); 
        }
        System.out.println(""); //to go to next line...
 }

Open in new window

0
 
cmalakarCommented:
You can use the program arguments for this..

like..

if(args.length > 0 )
{
   //put while loop here...

}

Change the line..  

String list[] = line.split(","); to  String list[] = line.split(args[0]);


While you run the program you should pass the delimter as the argument..

0
 
darrylf290567Author Commented:
Hi cmalakar,
the line below does not compile:

for(int i = 0: i < list.length; i++)
 something is wrong with the character after 0 ":" the colon has an error?

kind regards
darrylf290567
0
 
cmalakarCommented:
sorry.. it should be ";" (semi colon)
0
 
darrylf290567Author Commented:
Hi cmalakar,

Thanks. What I then need to do with the values is to place each value into a variable so I can insert the data into an oracle table

Can I print out each of the list array values as a separate line by line print so I can see the values more clearly?
Here is my code and the data printout to the screen.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.System;

public class ODSSFileReader
{
      public static void main(String[] args) throws Exception {
            String patternStr = " ";
            File curDir = new File("C:/odss_data/inc");
        BufferedReader in = new BufferedReader(new FileReader(curDir + "/" + "H1070927.REP"));
 
        String line;
        while ((line = in.readLine()) != null)
        {
              String[] ODSSFileList = line.split(patternStr);
              for(int i=0; i < ODSSFileList.length; i++)
            {
              //prints all the fields in a record in single line.. with space as delimiter..
              System.out.print(ODSSFileList[i] + " ");
            }
              //System.out.println(""); //to go to next line...
        }
        in.close();
    }
}

print:
HISTORY REPORT OF OUTPUT & CONSUMPTION    27-09-07 08:49:12 (TO 08:00)   21-09-07 22-09-07 23-09-07 24-09-07 25-09-07 26-09-07 27-09-07 HILLS OUTPUT STIRLING   :    58.65    58.76    61.74    63.22    63.65    83.49   100.85 SAMSON WTP +    69.80    59.63    63.25    63.93    72.82    54.04    43.58 S. DANDALUP+     0.00     0.00     0.00     0.00     0.00     0.00     0.00 CONJURUNUP +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 N. DANDALUP+     0.00     0.00     0.00     0.00     0.00     0.00     0.00 SERPENTINE +    26.80*   27.40*   40.00*   67.00*   54.70*   32.90*   25.90* WUNGONG    +    18.05    19.00    18.05    20.90    19.00    18.05    17.10 CANNING    +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 CHURCHMANS +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 VIC GRAVITY+     0.00*    0.00     0.00     0.00*    0.00     0.00*    0.00 VIC PUMPED +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 MUNDARING  +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 MUND PUMPED+    58.78    61.50    98.91    86.05    52.89    53.30    48.14  TOTAL HILLS=   232.09*  226.29*  281.95*  301.10*  263.07*  241.77*  235.58*  INDEPENDENT ARTESIAN OUTPUT LEED 5     :     0.00     0.00     0.00     0.00     0.00     0.00     0.00 LEED 6     +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 MOUNTS BAY +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 BALCATTA 1 +     2.44     2.44     2.43     2.44     2.44     2.42     2.45 BALCATTA 2 +     2.44     2.44     2.43     2.43     2.41     2.42     2.43 YOKINE 1   +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 YOKINE 2   +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 BOLD PK 1  +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 BOLD PK 2  +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 MELVILLE   +     4.67     0.00     4.21     3.63     3.91     0.75     5.37 MIRR M2A   +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 WANN W7    +    16.60    16.59    16.59    16.59    16.59    16.58    16.53 YANCHEP    +     1.04     1.62     1.72     1.11     1.54     0.94     0.94 TWO ROCKS  +     2.63     0.77     1.48     1.32     0.87     1.29     0.86 WHITFORD 97+     0.00*    0.00*    0.00*    0.00*    0.00*    0.00*    0.00* GWELUP 7   +    18.80*   18.80*   18.80*   18.80*   18.80*   18.82    18.90 GWELUP 17  +     0.00     0.00     0.00     0.00     0.00     0.00     0.00 GWELUP 27  +    14.58    14.27    14.27    14.29    14.34    14.39    14.66  TOTAL ARTES=    63.20*   56.92*   61.93*   60.60*   60.89*   57.62*   62.12*  TREATED OUTPUT MIRRABOOKA :     0.00     0.00     0.00     0.00     0.00     0.00     0.00 GWELUP     +    31.15*   47.50*   49.80*   49.53*   40.69    50.66    36.57 WANNEROO   +    84.98    91.00    91.72    93.03    90.53    92.89   111.50 JANDAKOT   +    39.45*   40.16*   39.10*   39.47*   38.75*   39.40*   22.15* NEERABUP   +    98.00*   99.00*   98.00*   98.00*   97.00*   98.00*   97.00* LEXIA      +     0.00     0.00     0.00     0.00     0.00     0.00     0.00  TOTAL TREAT=   253.58*  277.66*  278.62*  280.03*  266.97*  280.95*  267.22*  TOTAL GROUNDWATER:   316.79*  334.58*  340.55*  340.63*  327.86*  338.57*  329.34*  DESAL OUTPUT PERTH SWDP +   144.27*  153.84   160.69   136.05*  144.40*  155.83*  118.06*  TOTAL DESAL=   144.27*  153.84   160.69   136.05*  144.40*  155.83*  118.06*  TOT OUTPUT :   693.14*  714.72*  783.19*  777.78*  735.33*  736.18*  682.98*  INTNL TRNSF:   176.00   186.00   186.00   175.00   176.00   158.00   113.00 RIPARIAN   :     0.00     0.00     0.00     0.00     0.00     0.00     0.00  OUTPUT CSD :   517.14*  528.72*  597.19*  602.78*  559.33*  578.18*  569.98*  SUPPLIED TO GAWS       :    55.96*   58.53*   95.76*   82.69*   50.04*   50.31*   45.47* SW REGION  :    26.90*   29.71*   29.15*   26.30*   23.54*   25.92*   27.19* PERTH REG. :   434.28*  440.48*  472.28*  493.79*  485.75*  501.95*  497.32*  METROPOLITAN OPS LOSSES :    49.00*    0.00     0.00     0.00     0.00     0.00     0.00 SERVICE RESERVOIR STORAGE    :  1259.00* 1214.00* 1184.00* 1175.66* 1217.00* 1245.00* 1272.00* CHANGE     :  -101.00*  -45.00*  -30.00*   -8.34*   41.34*   28.00*   27.00*  METROPOLITAN CONSUMPTION:   486.28*  485.48*  502.28*  502.13*  444.41*  473.95*  470.32*  MAX TEMP   :    21.00    21.60    19.10    19.70    18.20    17.60    20.10 RAINFALL   :     0.00     3.20     0.00     7.80     8.40     1.20     2.80
I want to be able to place each of these values into variables and then prinit it out vertically, eg:
18.20
17.60
20.10
RAINFALL
:
0.00


kind regards
darrylf290567
0
 
darrylf290567Author Commented:
Hi cmalakar,

Can a .bat file call this program and pass in the filename, delimiter. How can I do this within the .bat file and how would I change the above java program to handle the 2 arguments being passed in by the .bat file?

This is very important. Can you please help me , cmalakar?

kindest regards
darrylf290567
0
 
cmalakarCommented:
System.out.print() function prints the value and does not moves the cursor to the next line..

To move the cursor to next line.. you have to use.. System.out.println()

ie., change

System.out.print(ODSSFileList[i] + " ");

line to

System.out.println(ODSSFileList[i]);
0
 
cmalakarCommented:
I already told in my above comment (ID: 20765529), that you can pass the argument to the program

And finally you simply put the "java" command into some batch file..
0
 
darrylf290567Author Commented:
Hi cmalakar,

But what is the syntax to do this within the .bat file as well as the java code below:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.lang.System;

public class ODSSFileReader
{
      public static void main(String[] args) throws Exception {
            String patternStr = " ";
            File curDir = new File("C:/odss_data/inc");
        BufferedReader in = new BufferedReader(new FileReader(curDir + "/" + "H1070927.REP"));
 
        String line;
        while ((line = in.readLine()) != null)
        {
              String[] ODSSFileList = line.split(patternStr);
              for(int i=0; i < ODSSFileList.length; i++)
            {
              //prints all the fields in a record in single line.. with space as delimiter..
              System.out.print(ODSSFileList[i] + " ");
            }
              //System.out.println(""); //to go to next line...
        }
        in.close();
    }
}

would it be args(0) for the filename
args(1) for the delimiter?

kind regards
darrylf290567

0
 
cmalakarCommented:
yes, you can do that...

>> new FileReader(curDir + "/" + "H1070927.REP") will become..

new FileReader(curDir + "/" + args[0])

>> String[] ODSSFileList = line.split(patternStr); will become..

String[] ODSSFileList = line.split(args[1]);

But in the main(), we should have the following check at the top..

if (args.length < 2) //Because you need two arguments...
{
   System.out.println("Invalid arguments");
   System.exit(1);
}
0
 
cmalakarCommented:
How are you running the file currently..?

I mean from command console or eclipse IDE.. or some other IDE..
0
 
darrylf290567Author Commented:
hi cmalaka,

I am only running it from the Eclipse IDE. However, in the real world I will be running it from .bat file that is why I need to know what call syntax I need to make from the .bat file with the 2 parameters?

kind regards
darrylf290567
0
 
cmalakarCommented:
Compile the class file in command prompt like..

<AbsolutePathToJdkInstallation>\bin\javac ODSSFileReader.java

Then put the following line in bat file and run it..

<AbsolutePathToJdkInstallation>\bin\java -cp <AbsoluteDirPathOf ODSSFileReader.classFileExists> ODSSFileReader <filename> <delimter>

for ex.. if ODSSFileReader.class exits in C:\ then

<AbsolutePathToJdkInstallation>\bin\java -cp c:\   ODSSFileReader H1070927.REP ~

0
 
darrylf290567Author Commented:
Hi cmalaka,

Sorry what I mean is that once i have built the executable for the java program how would i call this executable within the .bat file and passing in the parameters. I am not talking about the compilation of the program but when it actually has been built and is running.

kind regards
darryl290567
0
 
cmalakarCommented:
>> once i have built the executable for the java program

You mean to say, you have class file..

Then you have to do the following..

put the following line in bat file and run it..

<AbsolutePathToJdkInstallation>\bin\java -cp <AbsoluteDirPathOf ODSSFileReader.classFileExists> ODSSFileReader <filename> <delimter>

for ex.. if ODSSFileReader.class exits in C:\ then

<AbsolutePathToJdkInstallation>\bin\java -cp c:\   ODSSFileReader H1070927.REP ~
0
 
darrylf290567Author Commented:
Excellent solution and very helpful to cmalakar.

kind regards
darrylf290567
0
 
darrylf290567Author Commented:
Hi cmalakar,

The following syntax I placed in the .bat file to run the java class:

C:\Program Files\Java\jre1.5.0_10\bin\java -cp C:\Documents and Settings\francid0\workspace\ODSSFileReader\bin\ ODSSFileReader C:\odss_data\inc GABinFixaq.txt ,

Did not work

Can you please help?

kind regards
darrylf290567
0
 
cmalakarCommented:
What is the error..?

Instead of .bat file.. run in the command prompt.. and post the error..
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 15
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now