In: Computer Science
Using JAVA and NETBEANS
Assignment Content
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 file, sort and store the contents of the text file into an ArrayList, then write the sorted contents via an output 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<String> al = new ArrayList<String> ();
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.
Note: Refer to this week's Individual assignment, "Week Three Analyze Assignment," and to Ch. 8, "IO," in OCP: Oracle® Certified Professional Java® SE 8 Programmer II Study Guide.
Run and debug your modified program in NetBeans until it satisfies the requirements described above.
If you have any doubts, please give me comment...
import java.io.*;
import java.util.*;
public class Datasort {
public static void main(String[] args) throws IOException {
File fin = new File("input.txt"); // input file
File fout = new File("output.txt"); // create an out file
// 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) {
al.add(aLine);
}
// set the sort for values is greater than 0
if (al.size() > 0){
Collections.sort(al);
// sorted content to the output file
for(i=0; i<al.size(); i++)
{
out.write(al.get(i)+"\n");
}
}
// close the 2 files
out.close();
in.close();
}
}