Results 1 to 4 of 4
  1. #1
    karrydhawan is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default reading particular line from textfile file

    Java 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
    Attached Files Attached Files
    Last edited by Norm; 01-04-2012 at 02:04 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: reading particular line from textfile file

    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?

  3. #3
    karrydhawan is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: reading particular line from textfile file

    yea it is the position of line

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default 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.

Similar Threads

  1. Reading Next Line of File
    By smitsky in forum New To Java
    Replies: 5
    Last Post: 12-04-2011, 05:35 PM
  2. Java- Writing a file and reading a file line by line
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 07-20-2011, 07:56 AM
  3. Reading a specific line from a file
    By efebatistaarda in forum New To Java
    Replies: 6
    Last Post: 02-22-2011, 02:57 PM
  4. Reading part of a line in a text file
    By aaronfsimons in forum New To Java
    Replies: 3
    Last Post: 05-04-2009, 07:57 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •