Code Examples
A repository of 155 code examples for BeepBeep
io.LineReaderExample Class Reference

Read lines from a text file and convert them into numbers. More...

Static Public Member Functions

static void main (String[] args)
 

Detailed Description

Read lines from a text file and convert them into numbers.

This illustrates the use of the ReadLines processor.

The expected output of the program is:

3,Integer
1,Integer
4,Integer
1,Integer
5,Integer
9,Integer
2.2,Float
Exception in thread "main" java.util.NoSuchElementException
    at ca.uqac.lif.cep.UniformProcessor$UnaryPullable.pull(UniformProcessor.java:269)
    at io.LineReaderExample.main(LineReaderExample.java:43)
Author
Sylvain Hallé Easy

Definition at line 48 of file LineReaderExample.java.

Member Function Documentation

◆ main()

static void io.LineReaderExample.main ( String []  args)
static

We obtain an InputStream from a file. Since we refer to a file

Definition at line 50 of file LineReaderExample.java.

51  {
52  /// We obtain an InputStream from a file. Since we refer to a file
53  // that lies within the source code's folders, we can access it using
54  // the getResourceAsStream method. You could replace this with any
55  // other instruction that returns an InputStream.
56  InputStream is = LineReaderExample.class.getResourceAsStream("pi.txt");
57 
58  // We then instantiate a ReadLines processor, and instruct it to read
59  // from the input stream we created
60  ReadLines reader = new ReadLines(is);
61 
62  // The output of ReadLines are String events. We apply the function
63  // NumberCast on these strings to convert them into number objects.
64  ApplyFunction cast = new ApplyFunction(Numbers.numberCast);
65 
66  // The reader and cast processors are connected
67  Connector.connect(reader, cast);
68 
69  // We then pull repeatedly on cast. Note the use of hasNext: since we
70  // are reading from a file, eventually we reach the end of the file
71  // and no more events can be produced.
72  Pullable p = cast.getPullableOutput();
73  while (p.hasNext())
74  {
75  Number n = (Number) p.next();
76  System.out.println(n + "," + n.getClass().getSimpleName());
77  }
78  // Just for fun, let us try to pull one last time on p. This will
79  // throw an exception
80  p.pull();
81  ///
82  }

The documentation for this class was generated from the following file: