Code Examples
A repository of 155 code examples for BeepBeep
FileReader.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2016 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package basic;
19 
20 import java.io.InputStream;
21 
22 import ca.uqac.lif.cep.Pullable;
23 import ca.uqac.lif.cep.UtilityMethods;
24 import ca.uqac.lif.cep.io.ReadLines;
25 
26 /**
27  * Read an input stream from a text file line by line and show the output.
28  *
29  * @author Sylvain HallĂ©
30  * @difficulty Easy
31  */
32 public class FileReader
33 {
34  public static void main(String[] args)
35  {
36  /* Get an input stream on some resource. Here we read a file
37  * that resides with the source code by using the getResourceAsStream()
38  * method. (This is standard Java and not specific to BeepBeep.) */
39  InputStream stream = FileReader.class.getResourceAsStream("numbers.txt");
40 
41  /* Give this stream to a LineReader processor. The LineReader
42  * reads the lines of a file one by one, and outputs each line
43  * as an event (a String object). */
44  ReadLines reader = new ReadLines(stream);
45 
46  /* The addCrlf() method tells the line reader *not* to append a
47  * carriage return at the end of each event. There exist situations where
48  * this might be useful, but not here. */
49  reader.addCrlf(false);
50 
51  /* Get a reference to the output pullable of the LineReader. */
52  Pullable p = reader.getPullableOutput();
53 
54  /* We exploit the fact that p can be used like an iterator to
55  * write the loop as follows: */
56  for (Object o : p)
57  {
58  /* Note that the events produced by the LineReader are *strings*;
59  * here, they just happen to be strings that contain numbers. */
60  System.out.printf("The event is %s\n", o);
61  // Sleep a little so you have time to read!
62  UtilityMethods.pause(1000);
63  }
64  }
65 }
Read an input stream from a text file line by line and show the output.
Definition: FileReader.java:32