Welcome to AssignmentCache!

PRG 421 Week 3 Individual Analyze Assignment Java Code That Sorts Extracts Data and Saves It To a Collection

Availability: In stock

$6.00

Quick Overview

PRG 421 Week 3 Individual Analyze Assignment Java Code That Sorts Extracts Data and Saves It To a Collection


"Java Code That Sorts, Extracts Data and Saves It To a Collection" text file
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?
Submit your completed Word document to the Assignment Files tab.
Here is the code to answer the questions with:


// 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<String> al = new ArrayList<String> ();


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

PRG 421 Week 3 Individual Analyze Assignment Java Code That Sorts Extracts Data and Saves It To a Collection

Double click on above image to view full picture

Zoom Out
Zoom In

More Views

  • PRG 421 Week 3 Individual Analyze Assignment Java Code That Sorts Extracts Data and Saves It To a Collection
$6.00

Details

PRG 421 Week 3 Individual Analyze Assignment Java Code That Sorts Extracts Data and Saves It To a Collection

"Java Code That Sorts, Extracts Data and Saves It To a Collection" text file
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?
Submit your completed Word document to the Assignment Files tab.
Here is the code to answer the questions with:

// 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<String> al = new ArrayList<String> ();

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

Additional Information

Special Price $4.00

Product Tags

Use spaces to separate tags. Use single quotes (') for phrases.

[profiler]
Memory usage: real: 18350080, emalloc: 17905336
Code ProfilerTimeCntEmallocRealMem