Solved

Reading file in Java Doubt

Posted on 2011-09-27
15
198 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 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

16 Experts available now in Live!

Get 1:1 Help Now