user1790945
user1790945

Reputation:

How to get the last two record from the .csv file using java?

I'm trying to get the data from .csv file (Only Last two records) from the file location D:\Teja\Docs\data.csv and the data format is

1st record

PRO;SEN_PRO;99;302;2;TOUR-78995;TOU-POIN-7895;2012-11-05 08:00:00+0500;1;2;42.0;51.966375;8.255308

2nd record

PRO;SEN_PRO;100;402;2;TOUR-78995;TOU-POIN-7895;2012-11-05 08:00:00+0500;1;2;12.4;51.966375;8.255308

And so on..

There are number of records I want only last two record from them.

Upvotes: 2

Views: 2044

Answers (1)

MaVRoSCy
MaVRoSCy

Reputation: 17839

maybe this can be of some help

public class ReadLastTwoLine{

 public static void main(String[] args) throws Exception {

  FileInputStream in = new FileInputStream("file.txt");
  BufferedReader br = new BufferedReader(new InputStreamReader(in));

  String strLastLine = "";
  String tmp;
  String strlastLineMinusOne="";;

  while ((tmp = br.readLine()) != null)
  {
     strlastLineMinusOne = strLastLine;
     strLastLine = tmp;
  }

  System.out.println("Last Line Minus one is : "+strlastLineMinusOne");
  System.out.println("Last Line is : "+strLastLine");

  in.close();
 }
}

Upvotes: 2

Related Questions