For this assignment, you will analyze code that uses a file input stream and a file output stream.
Read through the linked Java code.
In a Microsoft Word document, answer the following questions:
Source code:
/**********************************************************************
* Program: Datasort
* Purpose: Java code that sorts, extracts data and saves it to a collection
* Programmer: I am student
* Class:
* Instructor:
* Creation Date: 12/01/2017
*
* Comments:
* Extracts data from a file, sorts it, displays it onscreen, and saves it.
*
***********************************************************************/
// import the needed classes
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Datasort {
public static void main (String [] args) throws IOException {
File fin = new File("e:\input.txt"); // input file on e: drive (with data)
File fout = new File("e:\sorted.txt"); // create an out file on e: drive
// Java FileInputStream class obtains input bytes from a file
FileInputStream fis = new FileInputStream(fin);
FileOutputStream fos = new FileOutputStream(fout);
// buffering characters so as to provide for the efficient reading of characters, arrays, and lines
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos));
// declare an array in-line, ready for the sort
String aLine;
ArrayListal = new ArrayList ();
int i = 0;
while ((aLine = in.readLine()) != null) {
// set the sort for values is greater than 0
if (!aLine.trim().startsWith("-") && aLine.trim().length() > 0) {
al.add(aLine);
i++;
}
}
Collections.sort(al); // sorted content to the output file
for (String s : al) {
System.out.println(s);
out.write(s);
out.newLine();
out.newLine();
}
// close the 2 files
in.close();
out.close();
}
}
For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text flie, sort and store the contents of the text file into an ArrayList, then write the sorted contents via an ouput stream to a separate output text file.
Copy and paste the following Java code into a JAVA source file in NetBeans:
import java.io.BufferedReader;
import java.io.BufferedWriter;
public class Datasort {
public static void main (String [] args) {
File fin = // input file
File fout = // create an out file
// Java FileInputStream class obtains input bytes from a file
FileInputStream fis = new FileInputStream(fin);
// buffering characters so as to provide for the efficient reading of characters, arrays, and lines
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
// declare an array in-line, ready for the sort
String aLine;
ArrayListal = new ArrayList ();
int i = 0;
while ((aLine = in.readLine()) != null) {
// set the sort for values is greater than 0
Collections.sort(al); // sorted content to the output file
{
System.out.println(s);
}
// close the 2 files
}
}
Add code as indicated in the comments.
Run and debug your modified program in NetBeans until it satisfies the requirements described above.