Discussion Question 2. Think of a programming task—other than the individual or team assignments in this course—in which you would choose to use a data file. Write the code to read or write the file. Explain the activity and your code. Answer: In this activity, file “input.txt, which contains some numbers in the random order is read and numbers are sorted with bubble sort algorithm and sorted numbers are written to another file “output.txt”. Here is the code : import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Scanner; import java.util.Vector; public class FileReadAndWrite { private static String inputFileName = "input.txt"; private static String outputFileName = "output.txt"; public static void main(String[] args) { Vector<Integer> integersVector = readDataFromFile(); if(integersVector!=null) { int numberOfElements = integersVector.size(); int integersArray[] = new int[numberOfElements]; for(int i=0;i<numberOfElements;i++) { integersArray[i] = ((Integer)integersVector.elementAt(i)).intValue(); } bubbleSort(integersArray, numberOfElements);
}
writeToFile(integersArray);
} private static Vector<Integer> readDataFromFile() { Vector<Integer> integers = new Vector<Integer>(); try { InputStream fileStream; DataInputStream inputStream; Scanner scanner; // getting InputStream object try { fileStream = new FileInputStream(inputFileName); } // if file is not found catch (FileNotFoundException e) { System.out.println("Input file not found."); return null; } // data input stream inputStream = new DataInputStream(fileStream); scanner = new Scanner(new InputStreamReader(inputStream)); while(scanner.hasNext()) { integers.addElement(new Integer(scanner.nextInt())); } // closing data input stream inputStream.close();
}
} catch(IOException e) { e.printStackTrace(); return null; } return integers;
// function to write data to file
private static void writeToFile(int[] integerArray) { // Create file FileWriter fstream = null; try { fstream = new FileWriter(outputFileName); } catch (IOException e1) { e1.printStackTrace(); return; } BufferedWriter out = new BufferedWriter(fstream); try {
output.txt");
}
for(int i=0;i<integerArray.length;i++) { out.write("" + integerArray[i]); out.write("\r\n"); } out.close(); System.out.println("Numbers have been sorted and written to
} catch (IOException e) { e.printStackTrace(); }
private static void bubbleSort(int[] unsortedNumbersArray, int numberOfElements) { int temp, i, j; for(i=0; i<numberOfElements-1; i++) { for(j=0; j<numberOfElements-1-i; j++) { if(unsortedNumbersArray[j] > unsortedNumbersArray[j+1]) { temp = unsortedNumbersArray[j]; unsortedNumbersArray[j] = unsortedNumbersArray[j+1];
} } }
}
Snapshot out output :
}
unsortedNumbersArray[j+1] = temp;
Input file contains the following contents : 9 8 4 3 5 6 2 1 7 10
And after the program runs, number are sorted and stored in output.txt Which contains the following contents. 1 2 3 4 5 6 7 8 9 10