Revert "Stream test"

This commit is contained in:
eugenefischer
2020-06-11 14:03:27 -05:00
committed by GitHub
parent 87cf8a3e05
commit 435a9ef718
12 changed files with 47 additions and 88 deletions

View File

@@ -1,14 +1,9 @@
package Sorting;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.io.BufferedWriter;
import java.util.Random;
import java.io.File;
import java.io.IOException;
//import java.io.File;
//import java.io.FileWriter;
//import java.lang.Math;
import java.io.FileWriter;
import java.lang.Math;
/**
* This class will allow the user to make a file of random integers.
@@ -29,27 +24,6 @@ public class RandomNumberFileMaker {
max=M+1;
}
//new version using streams and a try-with-resouces block
public void writeFile(){
try(BufferedWriter writer = Files.newBufferedWriter(Path.of(filename), StandardOpenOption.CREATE_NEW)){
Random random = new Random();
random.ints(count, min, max+1)
.forEach(num ->
{
try{
writer.write(Integer.toString(num)); writer.newLine();
}catch (IOException ex){
System.out.println("An error occurred.");
}
});
}catch (IOException ex){
System.out.println("Could not make new file named "+filename);
}
}
//previous version retained for reference.
/*
public void writeFile(){
try{
File randomNumbers = new File(filename);
@@ -68,6 +42,6 @@ public class RandomNumberFileMaker {
}catch (IOException ex){
System.err.println(ex);
}
}*/
}
}