View Javadoc
1   package ca.uhn.hl7v2.examples;
2   
3   import java.io.BufferedInputStream;
4   import java.io.File;
5   import java.io.FileInputStream;
6   import java.io.FileNotFoundException;
7   import java.io.InputStream;
8   
9   import ca.uhn.hl7v2.model.Message;
10  import ca.uhn.hl7v2.util.Hl7InputStreamMessageIterator;
11  import ca.uhn.hl7v2.util.Hl7InputStreamMessageStringIterator;
12  
13  public class ReadMessagesFromFile {
14  
15  	@SuppressWarnings("unused")
16  	public static void main(String[] args) throws FileNotFoundException {
17  
18  		/*
19  		 * This example shows how to read a series of messages from a file
20  		 * containing a number of HL7 messages. Assume you have a text file
21  		 * containing a number of messages.
22  		 */
23  		
24  		// Open an InputStream to read from the file
25  		File file = new File("hl7_messages.txt");
26  		InputStream is = new FileInputStream(file);
27  		
28  		// It's generally a good idea to buffer file IO
29  		is = new BufferedInputStream(is);
30  		
31  		// The following class is a HAPI utility that will iterate over
32  		// the messages which appear over an InputStream
33  		Hl7InputStreamMessageIterator iter = new Hl7InputStreamMessageIterator(is);
34  		
35  		while (iter.hasNext()) {
36  			
37  			Message next = iter.next();
38  			
39  			// Do something with the message
40  			
41  		}
42  		
43  		/*
44  		 * If you don't want the message parsed, you can also just
45  		 * read them in as strings.
46  		 */
47  		
48  		file = new File("hl7_messages.txt");
49  		is = new FileInputStream(file);
50  		is = new BufferedInputStream(is);
51  		Hl7InputStreamMessageStringIterator iter2 = new Hl7InputStreamMessageStringIterator(is); 
52  		
53  		while (iter2.hasNext()) {
54  			
55  			String next = iter2.next();
56  			
57  			// Do something with the message
58  			
59  		}
60  		
61  	}
62  
63  }