| |||
Directory listing, file i/o and formatting demo
this example from a Well House Consultants training course
Source code: Yvonne.java Module: J050
import java.io.*;
import java.text.*; /* */ public class Yvonne { public static void main (String [] args) throws IOException { // Without parameters, do a directory listing if (args.length == 0) { File current = new File("."); String [] things = current.list(); for (int i=0; i<things.length; i++) { System.out.println(things[i]); } System.exit(0); } // If not a listing Check that we have exactly two parameter if (args.length != 2) { System.err.println("Usage line ..."); System.exit(2); } // Check that the parameter IS the name of a file that exists File Source = new File(args[0]); if (! Source.exists()) { System.err.println("Input file not available"); System.exit(1); } // Looks good, so open it for read BufferedReader DataInput = new BufferedReader(new FileReader(Source)); int matched = 0, all = 0; // Open an output file too FileWriter SafetyNet = new FileWriter("latest.tmp"); // Go through the file, line by line while (true) { String stuff = DataInput.readLine(); if (stuff == null) break; all++; if (stuff.startsWith(args[1])) { System.out.println(stuff); SafetyNet.write(stuff + "\n"); matched ++; } } SafetyNet.close(); float pcmatched = matched * 100.0F / all; System.out.println("Matched "+matched+" of "+all); System.out.println("Matched to "+pcmatched+"%"); // Oops - that's over-accurate so let's format it! // May as well do it the same way we would format money! DecimalFormat money = new DecimalFormat("###0.00"); String pc2dp = money.format(pcmatched); System.out.println("Matched to "+pc2dp+"% of records"); } } /* ---------- Sample Output A REGULAR CALL Dorothy-2:java grahamellis$ java Yvonne access_log.xyz catfish catfish - - [28/Aug/1998:09:47:39 -0400] "GET /index.html HTTP/1.0" 200 1476 catfish - - [28/Aug/1998:09:58:34 -0400] "GET /index.html HTTP/1.0" 200 1476 catfish - - [28/Aug/1998:09:59:41 -0400] "#GET /perl/index.html HTTP/1.0" 501 - catfish - - [11/Dec/1998:09:25:44 -0500] "GET index.html HTIP/1.0" 400 - catfish - - [11/Dec/1998:09:27:25 -0500] "GET /index.html HTTP/1.0" 200 1476 Matched 5 of 6912 Matched to 0.07233796% Matched to 0.07% of records Dorothy-2:java grahamellis$ A CALL THAT IS WRONG Dorothy-2:java grahamellis$ java Yvonne access_log.xyz Usage line ... Dorothy-2:java grahamellis$ THE SPECIAL CALL THAT LISTS THE DIRECTORY Dorothy-2:java grahamellis$ java Yvonne access_log.xyz Adam.class Adam.java Bernie.class Bernie.java Chloe.class Chloe.java Daniel.class Daniel.java disney Edward.class Edward.java Fred.class Fred.java Glenda.class Glenda.java Hilary.class Hilary.java Irene.class Irene.java Julie.class Julie.java Keith.class Keith.java latest.tmp Lenny.class Lenny.java Margaret.class Margaret.java Niamh.class Niamh.java Orlando.java Peter.class Peter.java Quentin.class Quentin.java Rolf.class Rolf.java Steve.class Steve.java Time.class Time.java TimePlus.class TimePlus.java Trevor.class Trevor.java Ulrika.class Ulrika.java Viv.class Viv.java WellHouseInput.class WellHouseInput.java Wendy.class Wendy.java Xena.class Xena.java Yvonne.class Yvonne.java Zoe.class Zoe.java Dorothy-2:java grahamellis$ */ Learn about this subject
This module and example are covered as required on private courses.
Should you wish to cover this example and associated subjects, and you're attending a public course
to cover other topics with us, please see our extra topic program.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering Java and associated technologies are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "this" training module. You'll find a description of the topic and some
other closely related examples on the "this" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. | |||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.info/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |