Question: 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

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.

Here is the analyze code assignment that we can use to aid us in the process, to have something to go off of.

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!