1 Attachment(s)
reading particular line from textfile file
Code:
package com.lnt.karan;
import java.io.*;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class Copyclass {
String newfile = "newfile.txt";
FileReader inputstream = null;
public static void main(String[] args) {
Copyclass copy = new Copyclass();
copy.copyclass();
}
public void copyclass()
{
File newfile1 = new File(newfile);
try
{
newfile1.createNewFile();
FileInputStream fstream = new FileInputStream("empDetails.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
strLine=br.readLine();
/* HashMap hm = new HashMap();
Set set = hm.entrySet();
*/
while ((strLine = br.readLine()) != null) {
int c = 0;
c= c+1;
/* hm.put(strLine, c);*/
System.out.println (strLine);
}
/* BufferedReader reader = new BufferedReader(FileReader("empDetails.txt"));
char c ;
c = inputstream.read();
*/
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
its copying and printing text in console from the text file but don't know how to copy particular lines from the file and print them. I have attached the text file above .pls help
regards
karry
Re: reading particular line from textfile file
Quote:
but don't know how to copy particular lines from the file
Which lines are the "particular lines"? How can the program determine which lines to print?
Is it the position of the line (say the third line)
or does the line contain a String that you can detect by using some of the String class's methods?
Re: reading particular line from textfile file
yea it is the position of line
Re: reading particular line from textfile file
To get to the nth line, read n-1 lines and ignore them. Then next line you read will be the one you want.