A1

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:

  • Could this program be run as is? If not, what is it lacking?
  • Does this program modify the contents of an input stream? In what way?
  • What are the results of running this code?

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;
ArrayList al = 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();
}
}

A2

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;
ArrayList al = 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.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.