Solved

Reading file in Java Doubt

Posted on 2011-09-27
15
200 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

825 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