Results 1 to 4 of 4
- 08-28-2012, 04:58 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 13
- Rep Power
- 0
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.
- 08-28-2012, 05:07 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: skip first line when reading a csv
Just don't do anything with the first line then.
ALso, just a note, but StringTokeniser is deprecated. You should be using the split() method on String instead.Java Code:br.readLine(); while ((file = br.readLine()) != null) { // etc etc }Please do not ask for code as refusal often offends.
- 08-28-2012, 11:36 PM #3
Member
- Join Date
- Jul 2012
- Location
- Earth
- Posts
- 75
- Rep Power
- 0
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?
- 08-29-2012, 09:46 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
SocketClient is reading line with an end of line byte in it?
By noahssite in forum NetworkingReplies: 1Last Post: 08-19-2011, 07:29 PM -
Java- Writing a file and reading a file line by line
By Nazneen Ali in forum New To JavaReplies: 7Last Post: 07-20-2011, 07:56 AM -
skip a line between 2 system.out.println messages
By aconti in forum New To JavaReplies: 5Last Post: 05-30-2011, 11:27 AM -
Reading in a line of data and splitting the line up into variables
By guru32 in forum New To JavaReplies: 9Last Post: 04-07-2009, 03:51 AM -
Reading in data from file line by line
By bluekswing in forum New To JavaReplies: 1Last Post: 10-02-2007, 12:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks