Solved

Reading file in Java Doubt

Posted on 2011-09-27
15
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

734 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