Created
April 24, 2015 09:47
-
-
Save philipschwarz/23822bab32c2ac2071d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.io.LineNumberReader; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class JavaTest | |
{ | |
public static void main(String[] args) | |
{ | |
List salaries = readSalaries( "input.txt" ); | |
Collections.sort( salaries ); | |
writeSalaries( salaries, "output.txt" ); | |
} | |
private static List readSalaries(String fileName) | |
{ | |
LineNumberReader lineReader = openInputFile(fileName);; | |
String line; | |
List salaries = new LinkedList(); | |
while ( (line = readLine(lineReader,fileName)) != null) | |
{ | |
salaries.add(getSalary(split(line))); | |
} | |
return salaries; | |
} | |
private static String readLine(LineNumberReader lineReader, String fileName) | |
{ | |
String line = null; | |
try | |
{ | |
line = lineReader.readLine(); | |
} | |
catch (IOException e) | |
{ | |
handleIOException("IOException wile reading from " + fileName + ".", e); | |
} | |
return line; | |
} | |
private static LineNumberReader openInputFile(String fileName) | |
{ | |
File inputFile = new File( fileName); | |
FileReader fileReader = null; | |
try | |
{ | |
fileReader = new FileReader(inputFile); | |
} | |
catch (FileNotFoundException e) | |
{ | |
handleIOException("IOException wile opening " + fileName + ".", e); | |
} | |
return new LineNumberReader(fileReader); | |
} | |
private static String getSalary(String[] records) | |
{ | |
return records[2]; | |
} | |
private static String[] split(String line) | |
{ | |
return line.split(","); | |
} | |
private static void writeSalaries(List salaries, String fileName) | |
{ | |
FileWriter outputFileWriter = open(fileName); | |
write("Salaries - Java \n", outputFileWriter, fileName); | |
for (Iterator iterator = salaries.iterator(); iterator.hasNext();) | |
{ | |
String salary = (String) iterator.next(); | |
write(salary, outputFileWriter, fileName); | |
} | |
close(outputFileWriter, fileName); | |
} | |
private static void write(String salary, FileWriter outputFileWriter, String fileName) | |
{ | |
try | |
{ | |
outputFileWriter.write(salary + "\n"); | |
} | |
catch (IOException e) | |
{ | |
handleIOException("IOException wile writing to " + fileName + ".", e); | |
} | |
} | |
private static FileWriter open(String fileName) | |
{ | |
FileWriter outputFileWriter = null; | |
try | |
{ | |
outputFileWriter = new FileWriter( new File( fileName ) ); | |
} | |
catch (IOException e) | |
{ | |
handleIOException("IOException wile opening " + fileName + ".", e); | |
} | |
return outputFileWriter; | |
} | |
private static void close(FileWriter outputFileWriter, String fileName) | |
{ | |
try | |
{ | |
outputFileWriter.close(); | |
} | |
catch (IOException e) | |
{ | |
handleIOException("IOException wile closing " + fileName + ".", e); | |
} | |
} | |
private static void handleIOException(String message, IOException e) | |
{ | |
e.printStackTrace(); | |
throw new RuntimeException(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment