For this assignment, you will analyze code that uses a file input stream and a f
ID: 3917762 • Letter: F
Question
For this assignment, you will analyze code that uses a file input stream and a file output stream.
1. Could this program be run as is? If not, what is it lacking?
2. Does this program modify the contents of an input stream? In what way?
3. What are the results of running this code?
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();
}
}
Explanation / Answer
1) This programme is correct I ran it on my MAC and output is below with the expected output.
NOTE: If the input file is not provided or not present at the appropriate location then IO exception will occur and the programme will not run any further
2) Input Stream here is FileInputStream which streams data from a file mentioned. Then a Bufferedreader is used to buffer the streamed data until it is read in memory.
3) a) reads every line and put in ArrayList
b) Use the collection framework method to sort the ArrayList of strings.
c) Then every String in ArrayList is printed in the file and then two new lines are printed.
d) Step c is repeated until all string in ArrayList are traversed
The output file is shown below
***************************************INPUT FILE***************************************
***************************************INPUT FILE***************************************
***************************************OUTPUT FILE***************************************
***************************************OUTPUT FILE***************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.