import java.io.*; import java.util.*; import java.util.regex.*; class ParseEmail1 { public static void main(String[] args) throws IOException { Reader r = new BufferedReader(new FileReader("message1.txt")); Scanner message = new Scanner(r); String line; String headers=""; // read headers while ( message.hasNextLine() ) { line = message.nextLine(); // check for end of headers if ( line.length()==0 ) { break; } headers += line + "\n"; } // join lines that were split Pattern p = Pattern.compile("\n[ \t]+"); Matcher m = p.matcher(headers); System.out.print( m.replaceAll(" ") ); // now we have all the header in one big string. // read message body while ( message.hasNextLine() ) { line = message.nextLine(); // do something with each line... } } }