Using JAVA
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:
********************************************** CODE TO ANALYZE ********************************************************
/**********************************************************************
* Program: Datasort
* Purpose: Java code that sorts, extracts
data and saves it to a collection
* Programmer: I am student
* Class: PRG/421r13,
Java Programming II
* 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<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();
}
}
1)
Ans:- This program runs and executed successfully but only works for string data not for integer data.
for example file contains only integer data then
/**********input.txt**************/
12
132
0
34
-5
7
344
654
98
/**********************output*********************/
0
12
132
34
344
654
7
98
Ans:- it will ignore the line in the file that length is zero and start with "-" character. if line contains extra space then trim the spaces of the line and put it into the array list of string type and sort it.
Q3)
/*********************input.txt***********************/
Jugal Kishor
Virat Kohli
MS Dhoni
-JKS
Thanks
/*********************output************************/
Jugal Kishor
MS Dhoni
Thanks
Virat Kohli
Please let me know if you have any doubt or modify the answer, Thanks :)
Get Answers For Free
Most questions answered within 1 hours.