Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Autonomous Database In Enterprise Architecture

Authors: Bal Mukund Sharma, Krishnakumar KM, Rashmi Panda

1st Edition

1801072248, 978-1801072243

More Books

Students also viewed these Databases questions