Skip to content

Instantly share code, notes, and snippets.

@the-arkhive
Created June 12, 2015 16:45
Show Gist options
  • Save the-arkhive/4678b04e16abb0f2cd00 to your computer and use it in GitHub Desktop.
Save the-arkhive/4678b04e16abb0f2cd00 to your computer and use it in GitHub Desktop.
The newest version of my data processing program, let's call it DataReader 2.0...
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileData {
public static void main(String[] args) throws IOException {
//Creates the scanners and varaibles needed to take user input for the source file.
System.out.println("Please drag and drop the source data file into the colsole now.");
Scanner Input = new Scanner(System.in);
String file_path_in = Input.nextLine();
//Creates the scanners and varaibles needed to take user input for the output file.
String file_path_out = "NEEDS A DEFAULT LOCATION AND I DIDN'T WANT YOU TO SEE MINE SO ADD ONE";
Scanner Output = new Scanner(System.in);
System.out.println("The default output file is:");
System.out.println(" " + file_path_out);
System.out.println("If you would like to change the output file enter '1'");
int Output_check = Input.nextInt();
if (Output_check == 1) {
System.out.println("Please drag and drop the new output file into the console now.");
file_path_out = Output.nextLine();
} else {
file_path_out = file_path_out;
}
//If for some reason you want to empty the output file each time you process a source file set this to false, otherwise leave it as true.
Boolean append = true;
//These coordinates define the desired rectangle, Lat1 will be the northern bound, Lat2 the southern, Lon1 the western and Lon2 the eastern.
double Lat1 = 76.00;
double Lat2 = 76.8;
double Lon1 = -69.17;
double Lon2 = -65.93;
//Executes ReadFile if able otherwise throws an exception
try {
ReadFile file = new ReadFile(file_path_in, file_path_out, append, Lat1, Lat2, Lon1, Lon2);
String[] aryLines = file.OpenFile();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.PrintWriter;
public class ReadFile {
//Variable declarations
private int processedCount = 0;
private double ThicknessBound = -9999.00;
private String path;
private String path_out;
private boolean append;
private double botLat;
private double topLat;
private double botLon;
private double topLon;
public ReadFile (String file_path_in, String file_path_out , boolean append_value, double lowerLat, double upperLat, double lowerLon, double upperLon) {
path = file_path_in;
path_out = file_path_out;
append = append_value;
botLat = lowerLat;
topLat = upperLat;
botLon = lowerLon;
topLon = upperLon;
}
//Method to read the file and store it in an array
public String[] OpenFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
for (int i = 0; i < numberOfLines; i++) {
textData[i] = textReader.readLine();
//Extracts the first two pieces of data and line number
String fullData = textData[i];
String[] splitData = fullData.split(",");
String textLat = splitData[0];
String textLon = splitData[1];
Double numLat = new Double("0");
Double numLon = new Double("0");
String testTickness = splitData[3];
Double numThickness = new Double("0");
//Converts the contents of the text data into doubles
double Lat = numLat.valueOf(textLat);
double Lon = numLon.valueOf(textLon);
double Thickness = numThickness.valueOf(testTickness);
//Checks for data that falls within the user defined bounds
if (Lat <= topLat && Lat >= botLat && Lon <= topLon && Lon >= botLon && Thickness != ThicknessBound) {
WriteFile output = new WriteFile(path_out, append);
output.writeToFile(textData[i]);
processedCount++;
System.out.println(Lat + " " + Lon);
} else {
processedCount++;
}
if (processedCount == numberOfLines) {
System.out.println("There is no more data to process in this file.");
}
}
textReader.close();
return textData;
}
int readLines() throws IOException {
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while ((aLine = bf.readLine()) != null) {
numberOfLines++;
}
bf.close();
return numberOfLines;
}
}
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class WriteFile{
private String path;
private Boolean append_to_file = true;
public WriteFile(String file_path_out , boolean append_value) {
path = file_path_out;
append_to_file = append_value;
}
public void writeToFile(String textData) throws IOException {
FileWriter write = new FileWriter(path , append_to_file);
PrintWriter print_line = new PrintWriter(write);
//Appends the text file
print_line.printf( "%s" + "%n" , textData);
print_line.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment