Results 1 to 4 of 4
  1. #1
    j0rdan is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default skip first line when reading a csv

    practically i have an e-mail client which reads e-mail address, then subject, then the message from a csv.

    the problem is i want the first line in the csv to be like a heading.

    soo far this is the code i have.

    i tried setting line at one but with no luck it just read the second line first then read the first line last.

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    
    public class EmailCSV {
    	public static void main(String[] args) {
    		// Strings for the file the email address from and the host
    		String fileName="C:\\file\\file.csv";
                    String from = "myemail@email.com";
                    String host = "my host";
    
                    
          Properties properties = System.getProperties();
    
          
          properties.setProperty("mail.smtp.host", host);
    
          Session session = Session.getDefaultInstance(properties);
    		try {
    	
                        BufferedReader br = new BufferedReader( new FileReader(fileName));
    			//String strLine = null;
    			StringTokenizer st = null;
    			int lineNumber = 0, tokenNumber = 0;
     
    			while( (fileName = br.readLine()) != null)
    			{
    				lineNumber++;
     
    				//break comma separated line using ","
    				st = new StringTokenizer(fileName, ",");
                            
    				
                                    while(st.hasMoreTokens())
    				{
    					//display csv values
    					tokenNumber++;
                                            // strings to read first line
                                            String to = st.nextToken();  
                                            
              MimeMessage message = new MimeMessage(session);
                                                  
             //sets message from
             message.setFrom(new InternetAddress(from));
             //sets message to
             message.addRecipient(Message.RecipientType.TO,
                                      new InternetAddress(to));
             //sets subjest
             message.setSubject(st.nextToken());
             // sets the content of the email
             message.setContent(st.nextToken(),
                                "text/html" );
    
             // Send message successfull
             Transport.send(message);
             System.out.println("Sent message successfully....");
                                    }
    				//reset token number
    				tokenNumber = 0;
    			}
    		} 		
    		catch (FileNotFoundException e) {
    			//Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			//Auto-generated catch block
    			e.printStackTrace();
    		}
                    catch (MessagingException mex) {
             mex.printStackTrace();
          }
                    
    }
    }
    Last edited by j0rdan; 08-28-2012 at 05:00 PM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default Re: skip first line when reading a csv

    Just don't do anything with the first line then.
    Java Code:
    br.readLine();
    while ((file = br.readLine()) != null) {
    // etc etc
    }
    ALso, just a note, but StringTokeniser is deprecated. You should be using the split() method on String instead.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: skip first line when reading a csv

    A side issue - what happens if you want to place a comma inside one of the fields in your CSV file?

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default Re: skip first line when reading a csv

    I was wondering that, especially since one of the fields seems to be a slab of html.

    Still, I'm sure they know what they're doing...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 08-19-2011, 07:29 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. skip a line between 2 system.out.println messages
    By aconti in forum New To Java
    Replies: 5
    Last Post: 05-30-2011, 11:27 AM
  4. Replies: 9
    Last Post: 04-07-2009, 03:51 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
  •