?
Solved

Reading file in Java Doubt

Posted on 2011-09-27
15
Medium Priority
?
207 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:Gurvinder Pal Singh
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:Gurvinder Pal Singh
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Independent Software Vendors: 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

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 …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

718 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