To still receiving newsletters from us please subscribe to our Newsletters:
http://tech.groups.yahoo.com/group/developers-Heaven/
/**
* Reports the number of seconds the computer spent
* creating a file of 3,000,000 random integers,
* 10 integers per line
* with a single space in between each integer on each line.
* (Note! There is NO space output following the 10th integer on each line.)
*
* @version 2017-12-06
* @author dwz
*/
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Random;
public class TimedWriteLine {
static final String FNAME = "MyRandomInts.txt";
static final int NUM_NUMBERS = 3_000_000;
public static void main( String[] args ) {
testItOut();
}
static void testItOut() {
System.out.println( "Getting/filing " + NUM_NUMBERS + " random integers." );
long startTime = System.currentTimeMillis();
Random randGen = new Random();
try( BufferedWriter fout = Files.newBufferedWriter( Paths.get( FNAME ),
Charset.defaultCharset() ) ) {
for( int i = 0; i < NUM_NUMBERS; ) {
String valStr = Integer.toString( randGen.nextInt(NUM_NUMBERS) );
fout.write( valStr );
++ i;
if( i % 10 == 0 ) fout.newLine();
else fout.write( " ", 0, 1 );
}
} catch( IOException ex ) {
System.out.println( ex );
}
long endTime = System.currentTimeMillis();
System.out.println();
System.out.println( "Run time in seconds was: " + (endTime - startTime) / 1000.0 );
}
}
/**
* @version 2017-11-27
* @author dwz
* An example of a Java 8 way a student might code to read
* a text file line into a Stream ... displaying each line as read.
*/
// structure of below used data file: Persons.txt //
/*
Matt 1 19820611
Bonnie 2 19830202
Theo 3 20140712
Isla 4 20171119
*/
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.IOException;
import java.util.stream.Stream;
class StreamFileTest {
// Place this file in the same folder
// as the .class file(s) produced here. //
static String FNAME = "Persons.txt";
public static void main( String[] args ) {
// code for try with resources that are auto-closeable //
try( Stream< String > myLines = Files.lines( Paths.get( FNAME ) ) ) {
myLines.forEach( System.out::println );
} catch( IOException ex ) {
System.out.println( ex );
}
}
}
/**
* @version 2017-12-05
* @author dwz
* An example of a Java 8 way a student might code to read
* text file lines into a Stream < String > ...
* parsing each line (in the stream) into a Person object ...
* and adding that new Person object to a growing List of type Person,
* then show that list sorted by names.
*/
// structure of below used data file: Persons.txt //
/*
Matt 1 19820611
Bonnie 2 19830202
Theo 3 20140712
Isla 4 20171119
*/
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.IOException;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
class StreamFileTest2 {
// Place this file in the same folder
// as the .class file(s) produced here. //
static String FNAME = "Persons.txt";
public static void main( String[] args ) {
List < Person > mattsFamily = new ArrayList <> ();
// code for try with resources that are auto-closeable //
try( Stream< String > myLines = Files.lines( Paths.get( FNAME ) ) ) {
// note use below of lambda function: line -> mattsFamily.add( new Person(line) //
myLines.forEach( line -> mattsFamily.add( new Person(line) ) );
} catch( IOException ex ) {
System.out.println( ex );
}
Collections.sort( mattsFamily, (a, b) -> a.name.compareTo(b.name) );
// Now show each Person in the sorted List < Person> object: mattsFamily ...
for( Person per : mattsFamily ) {
System.out.println( per ); // calls the 'toString' Person method //
}
}
}
// an example of a simple class Person //
class Person {
String name, id, dob;
// constructor from (here a file) line passed in //
public Person( String line ) {
String[] ary = line.split(" ");
name = ary[0];
id = ary[1];
dob = ary[2];
}
@Override
public String toString() {
return name + ", id: " + id + ", dob: " + dob;
}
}
/**
* @version 2017-11-26
* @author dwz
* An example of a simple way a beginning student might code
* to read a text file line by line ... displaying each line as read.
*/
import java.util.Scanner;
import java.nio.file.Paths;
import java.io.IOException;
class ScanFileTest {
// Place this file in the same folder
// as the .class file(s) produced here. //
static String FNAME = "Persons.txt";
public static void main( String[] args ) {
// code for try with resources that are auto-closeable //
try( Scanner scan = new Scanner( Paths.get( FNAME ) ) ) {
String line = null;
while( scan.hasNext() ) {
line = scan.nextLine();
System.out.println( line );
}
} catch( IOException ex ) {
System.out.println( ex );
}
}
}
/**
* @version 2017-12-05
* @author dwz
* An example of a simple way a beginning student might code
* to read a (small) text file into an ArrayList < Person > ...
* then to display that List < Person > in some sorted order
* specified by using a lambda function to control the sort.
*/
// structure of below used data file: Persons.txt //
/*
Matt 1 19820611
Bonnie 2 19830202
Theo 3 20140712
Isla 4 20171119
*/
import java.util.Scanner;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
class ScanFileTest2 {
// Place this file in the same folder
// as the .class file(s) produced here. //
static String FNAME = "Persons.txt";
public static void main( String[] args ) {
List < Person > mattsFamily = new ArrayList <> (); // get an empty ArrayList //
// code for try with resources that are auto-closeable //
try( Scanner scan = new Scanner( Paths.get( FNAME ) ) ) {
String line = null;
while( scan.hasNext() ) {
line = scan.nextLine();
mattsFamily.add( new Person( line ) );
}
} catch( IOException ex ) {
System.out.println( ex );
}
// show all family ... but firstly sort list by birthdays, youngest first //
Collections.sort( mattsFamily, (a, b) -> b.dob.compareTo(a.dob) );
for( Person per : mattsFamily ) {
System.out.println( per ); // calls toString method here //
}
}
}
// an example of a simple class Person //
class Person {
protected String name, id, dob;
// constructor from (here a file) line passed in //
public Person( String line ) {
String[] ary = line.split(" ");
name = ary[0];
id = ary[1];
dob = ary[2];
}
@Override
public String toString() {
return name + ", id: " + id + ", dob: " + dob;
}
}
/**
* @version: 2017-12-05
* @author: dwz
* This demo uses the latest Java 8 Stream with map, filter and forEach methods
* to read a .txt data file of names and blood pressures
* and then to print a possible diagnosis to console and file
* for each (NOT Normal) file record as read and processed.
* (i.e. NO storage container is used here.)
*/
// structure of data file: BP.txt //
/*
Smith Jane 133/76
Jones Rob 98/70
Costello Judy-Ann 144/90
Frank-Anderson Bibby-Bonnie 190/30
Sue Peggy 10/5
James-Thomas Andrew 190/111
*/
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.io.IOException;
import java.io.BufferedWriter;
import java.util.stream.Stream;
public class BloodPressure3 {
final static String FNAME = "BP.txt";
final static String RESULTS_FNAME = "Results" + FNAME;
public static void main( String[] args ) {
try( Stream< String > lines = Files.lines( Paths.get( FNAME ) );
BufferedWriter bw = Files.newBufferedWriter( Paths.get( RESULTS_FNAME ),
Charset.defaultCharset() ) ) {
lines.map( line -> (new BP(line)).toString() )
.filter( diaText -> ! diaText.substring( diaText.length() - 6 ).equals( "Normal" ) )
.forEach( diaText -> output( diaText, bw ) );
} catch( IOException ex ) {
System.out.println( ex );
}
}
static void output( String diaText, BufferedWriter bw ) {
System.out.println( diaText ); // print result to Console //
try {
bw.write( diaText ); // bw.write( diaText, 0, diaText.length() );
bw.newLine();
} catch( IOException ex ) {
System.out.println( ex );
}
}
}
/**
* @version: 2017-12-05
* @author: dwz
* This demos using the latest Java 8 Stream, with map and ForEach methods,
* to read a .txt data file of names and blood pressures
* and then to print a possible diagnosis to console and file.
* (Note that each result (String) gets stored in a List <String> container here.)
*/
// structure of data file: BP.txt //
/*
Smith Jane 133/76
Jones Rob 98/70
Costello Judy-Ann 144/90
Frank-Anderson Bibby-Bonnie 190/30
Sue Peggy 10/5
James-Thomas Andrew 190/111
*/
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.io.IOException;
import java.util.stream.Stream;
import java.util.List;
import java.util.ArrayList;
public class BloodPressure2 {
final static String FNAME = "BP.txt";
final static String RESULTS_FNAME = "Results" + FNAME;
// get (a ref to) an empty List to be filled up below //
final static List< String > results = new ArrayList<> ();
public static void main( String[] args ) {
try( Stream< String > lines = Files.lines( Paths.get( FNAME ) ) ) {
lines.map( line -> diagnoseAndAddResultString(line) )
.forEach( System.out::println );
} catch( IOException ex ) {
System.out.println( ex );
}
// ok ... now can write List of results to file ...
try {
Files.write( Paths.get( RESULTS_FNAME ), results, Charset.defaultCharset() );
} catch( IOException ex ) {
System.out.println( ex );
}
}
static String diagnoseAndAddResultString( String line ) {
BP bp = new BP( line );
String tmp = bp.toString(); // tmp now holds diagnosis also //
results.add( tmp ); // Note that results is a (ref. to a) List< String > object //
return tmp;
}
}
/**
* @version: 2017-12-05
* @author: dwz
* class BP handles Blood Pressure data
* and methods to get and show a diagnosis
*/
// structure of data file ...
// BP.txt //
/*
Smith Jane 133/76
Jones Rob 98/70
Costello Judy-Ann 144/90
Frank-Anderson Bibby-Bonnie 190/30
Sue Peggy 10/5
James-Thomas Andrew 190/111
*/
public class BP {
private final static String UNKNOWN = " <--\n Unrecognized diagnosis. \n" +
"* CHECK VALIDITY of DATA entered. *";
private String lname, fname;
private int sys, dia;
// ctor from passed in String line of data (from file) //
public BP( String line ) {
String[] ary = line.split( "[/ ]" ); // split on char '/' or char ' ' //
//System.out.println( "ary.length = " + ary.length ); //debugging//
lname = ary[0];
fname = ary[1];
try {
sys = Integer.parseInt( ary[2] );
dia = Integer.parseInt( ary[3] );
} catch( NumberFormatException ex ) {
System.out.println( ex );
}
}
// Note: this returned string is also returned as part of below toString() //
public String getDiagnosis() {
if( sys < 120/2 || dia < 80/2 ) // ?? //
return UNKNOWN;
if( sys > 120*2 || dia > 80*2 ) // ?? //
return UNKNOWN;
if( sys >= 160 || dia > 100 )
return " <--\n CHECK AGE re. possible Stage 2 Hypertension.";
if( sys >= 140 || dia >= 90 )
return " <--\n CHECK AGE re. possible Stage 1 Hypertension.";
if( sys >= 120 || dia >= 80 )
return " <--\n CHECK AGE re. possible Pre-Hypertension.";
// else ...
return "\n Normal";
}
// Note that this method calls above method getDiagnosis() //
@Override
public String toString() {
// left adjust string of combined lname + ", " + fname in 40 spaces,
// right adjust each number in 3 spaces
return String.format( "%-40s %3d/%3d %s", (lname + ", " + fname),
sys, dia, getDiagnosis() );
}
}
Smith Jane 133/76
Jones Rob 98/70
Costello Judy-Ann 144/90
Frank-Anderson Bibby-Bonnie 190/30
Sue Peggy 10/5
James-Thomas Andrew 190/111
/**
* @version: 2017-12-05
* @author: dwz
* This demos using the latest Java 8 Stream with map, filter and collect methods ...
* to read a .txt data file of names and blood pressures
* and then to print a possible diagnosis to console and file
* for all NOT Normal results ... sorted by names (Note result String starts with lname, fname).
*/
// structure of data file: BP.txt //
/*
Smith Jane 133/76
Jones Rob 98/70
Costello Judy-Ann 144/90
Frank-Anderson Bibby-Bonnie 190/30
Sue Peggy 10/5
James-Thomas Andrew 190/111
*/
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.io.IOException;
import java.util.stream.Stream;
import java.util.stream.Collectors;
import java.util.List;
//import java.util.Collections;
public class BloodPressure2a {
final static String FNAME = "BP.txt";
final static String RESULTS_FNAME = "Results" + FNAME;
public static void main( String[] args ) {
List< String > results = null;
try( Stream< String > lines = Files.lines( Paths.get( FNAME ) ) ) {
// filter out all Normal results //
results = lines.map( line -> (new BP( line )).toString() )
.filter( diaText -> ! diaText.substring(diaText.length()-6).equals("Normal") )
.sorted()
.collect( Collectors.toList() );
} catch( IOException ex ) {
System.out.println( ex );
}
// Now can sort result strings by names (that appear at front of each result string) //
//Collections.sort( results );
//for( String result : results ) // Now can print list of (NOT Normal) results to Console //
//System.out.println( result );
// or //
results.stream().forEach( System.out::println );
try { // And now can write List of results to file //
Files.write( Paths.get( RESULTS_FNAME ), results, Charset.defaultCharset() );
} catch( IOException ex ) {
System.out.println( ex );
}
}
}