//Reads an input file containing numbers or strings and sorts them and writes to a new file import java.io.*; import java.util.*; public class FileSort { private Scanner input; private Formatter output; private List<Integer> list; private void openFile(String fileName) { try{ input = new Scanner(new File(fileName)); } catch(FileNotFoundException fileNotFoundException) { System.err.println("Error opening the file. File Not Found"); System.exit(1); } } private void readFile(String fileName) { try{ list = new ArrayList<Integer>(); while(input.hasNext()) { list.add(Integer.valueOf(input.next())); } Collections.sort(list); //System.out.printf("\nThe list is: %s",list); } catch(NoSuchElementException elementException) { System.err.println("File improperly formatted."); input.close(); System.exit(1); } } private void writeFile(String fileName) { try{ output = new Formatter(fileName); output.format("%s", "Hello"); } catch(Exception e) { System.err.println(e.getMessage()); output.close(); System.exit(1); } } public static void main(String[] args) { FileSort fs = new FileSort(); fs.openFile("input.txt"); fs.readFile("input.txt"); fs.writeFile("output.txt"); } }
try{ output = new Formatter(fileName); output.format("%s", "Hello"); }
try { FileWriter writer = new FileWriter("output.txt"); writer.write("hello"); writer.flush(); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)