Solved

Reading file in Java Doubt

Posted on 2011-09-27
15
199 Views
Last Modified: 2012-05-12
Hi Experts...
I have a file with the beginning 8 lines with some header information and the real information is present from the 8th line onwards.
How do i read the content starting from the 8th line onwards.
All the data is of string type and all data is enclosed under " " in every column and with , as the column separator.
I want to read the eighth column.
How do i do this.
0
Comment
Question by:gaugeta
  • 7
  • 6
  • 2
15 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36708570
You can read the file line by line and start processing info only after the 8th line, by using some counter
http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36708587
something like
import java.io.*;
class FileRead 
{
 public static void main(String args[])
  {
  try{
  FileInputStream fstream = new FileInputStream("textfile.txt");
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  int counter = 0;
  while ((strLine = br.readLine()) != null)   
  {
    if (counter == 8 )
    {
            System.out.println (strLine);
    }
    else
    {
     counter++
    }
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36708599
This should print your 8th column starting with 8th line:

import java.io.*;
class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
int count = 0;
  while ((strLine = br.readLine()) != null)   {
if(count < 8) continue;
StringTokenizer t = new StringTokenizer(strLine,",");
for(int j=0;j<7;j++)t.nextToken();
String s = t.nextToken();
System.out.println(s);


  // Print the content on the console
  
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Open in new window

0
 

Author Comment

by:gaugeta
ID: 36708601
@gurvinder372:Thanks for the reply.
How do read only the 8th column and whose data is enclosed under " " and the cloumns are separated by commas.
Please help...
0
 

Author Comment

by:gaugeta
ID: 36708606
@for_yan:Thanks for the reply.
I will try this and get back if i face any issues.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36708607
import java.io.*;
class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
int count = 0;
  while ((strLine = br.readLine()) != null)   {
if(count < 8) continue;
StringTokenizer t = new StringTokenizer(strLine,",");
for(int j=0;j<7;j++)t.nextToken();
String s = t.nextToken();
System.out.println(s);
count++;

  // Print the content on the console
  
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36708612
I forgot to incement the lines in the fuirst code - so the secnd one should work
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:gaugeta
ID: 36708615
@for_yan:The data in all column is enclosed in double quotes how to remove them while reading.
Please help...
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36708622
sorry again made a mistke, this is better

import java.io.*;
class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
int count = 0;
  while ((strLine = br.readLine()) != null)   {
if(count < 8){count++; continue;}
StringTokenizer t = new StringTokenizer(strLine,",");
for(int j=0;j<7;j++)t.nextToken();
String s = t.nextToken();
System.out.println(s);


  // Print the content on the console
  
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36708625
I think this should remove it.
The only problem may be if theera are commas inside the columns

import java.io.*;
class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
int count = 0;
  while ((strLine = br.readLine()) != null)   {
if(count < 8){count++; continue;}
StringTokenizer t = new StringTokenizer(strLine,",\"");
for(int j=0;j<7;j++)t.nextToken();
String s = t.nextToken();
System.out.println(s);


  // Print the content on the console
  
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Open in new window

0
 

Author Comment

by:gaugeta
ID: 36708646
@for_yan:Yes there are commas inside columns.
May be thats the reason for having double quotes to indicate that whatever the data inside the double quotes should be taken as data even if they contain commas.
Now how do i extract data.
Please help...
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36708742
This should do it with commas inside the quoted strings

import java.io.*;
class FileRead 
{
 public static void main(String args[])
  {
  try{
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("textfile.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  //Read File Line By Line
int count = 0;
  while ((strLine = br.readLine()) != null)   {
if(count < 8){count++; continue;}

 String [] lines =strLine.split("(\",\")|(^\")|(\"$)");



System.out.println(lines[7]);


  // Print the content on the console
  
  }
  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Open in new window

0
 

Author Comment

by:gaugeta
ID: 36708778
@for_yan:It works perfectly.
can you explain how does String [] lines =strLine.split("(\",\")|(^\")|(\"$)"); achieve this.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36708819
It splits the line by finding either "," sequence or one quote at the beginning of line or one quote at the end; the spoliting symbols are removed and the values between them populate array lines
0
 

Author Comment

by:gaugeta
ID: 36708848
@for_yan:Thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now