Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Flexibility in input - read from file, web resource or keyboard

When you're writing a program, don't think that you're reading from a file - think that you're reading from a stream. That gives you the flexibiity to read from a file, or from a web resource, or from a device such as a keyoard.

A stream is an input flow of data that comes to you, record by record (typically line by line) and each time you call for a record / line to be read, the next one will come to you. And when you reach the end of your data, you'll get back some sort of end-of-file signal to tell you that you're done.

Towards the end of last week's (Java) course, I wrote an example (source [here]) which would read either from a file, or from a URL (web resource / page). The initial opening of the resource differs, but once I have my Input Stream Reader, or a subclass of it, I can then use common code to handle the buffering of the data and the passing back of it to my application line by line.

In order to make the example clear, I didn't do anything fancy on the resource name the user types in to see if it looks like a file name or URL - I have him specify. Here's my resource opening code:

  import java.io.*;
  import java.net.*;
  
  try {
    if (args[0].equals("w"))
    instream = new InputStreamReader((new URL(args[1])).openStream());
    if (args[0].equals("f"))
    instream = new FileReader(args[1]);
  } catch (Exception e) {
    System.err.println(e);
  }
  
  if (instream == null) {
    System.err.println("Usage: java Stations w|f url|filename");
    System.exit(1);
  }


Common code then sets this stream up to be buffered, so that I can read it line by line:

  BufferedReader source = new BufferedReader(instream);

And further common code allows me to loop through the resource while data is still available:

  String lyne;
  while ((lyne = source.readLine()) != null) {

(written 2015-02-08, updated 2015-02-12)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J808 - Java - Network Programming
  [1976] Where is this IP address, IPv4 and IPv6 - (2009-01-04)
  [2863] Writing a server in Java - (2010-07-09)

J713 - Java - More Input and Output
  [1239] End of File on a Java BufferedReader - (2007-06-22)
  [2420] Exceptions in Java - why and how - (2009-09-24)
  [4350] Keyboard reader for Java programming newcomers - (2014-12-12)
  [4414] Java - converting an integer to a fixed length string - (2015-02-04)


Back to
Java Inheritance example - group of classes - step by step
Previous and next
or
Horse's mouth home
Forward to
How healthy are the stars of stage and screen?
Some other Articles
Looking Forward - TransWilts Community Rail Partnership and TransWilts CIC
Campus - about the logos / way marks you have been choosing from
Objects - from physical to virtual or abstract - Java
How healthy are the stars of stage and screen?
Flexibility in input - read from file, web resource or keyboard
Java Inheritance example - group of classes - step by step
Melksham Community Campus - which of these potential signs do you like?
Crisp morning, fast run
Challenging the IT course business model
Buffet review - taking the price into account
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4420_Fle ... board.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb