Skip to content

Instantly share code, notes, and snippets.

View awwsmm's full-sized avatar

Andrew awwsmm

View GitHub Profile
@awwsmm
awwsmm / addCSVcols.bat
Created July 19, 2018 16:29
Add constant columns to CSV with batch script
@echo off
setlocal enabledelayedexpansion
set row=0
set inFile=merged.csv
set outFile=final.csv
if exist "%outFile%" del "%outFile%"
@awwsmm
awwsmm / CountLines.java
Last active August 2, 2018 12:48
Count the number of lines in a flat text file
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
// NOTE: adapted from http://bit.ly/2LOQwIP
public class CountLines {
// private constructor in utility class
@awwsmm
awwsmm / SimplestCSVParser.java
Last active August 2, 2018 13:09
The simplest way to parse a CSV in Java, period. < 30 lines, including comments and imports.
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SimplestCSVParser {
// private constructor -- utility class
private SimplestCSVParser(){}
// where the magic happens
@awwsmm
awwsmm / LastLine.java
Created August 2, 2018 13:14
Finds the last non-empty line/row of a CSV or XLS(X) file
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
@awwsmm
awwsmm / Settings.java
Last active August 17, 2018 14:10
Load Java program configuration / settings at runtime (key-value pairs)
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
// Compile and then run! Change the settings.conf file and rerun without recompiling!
// Settings are updated without needing to recompile.
public class Settings {
public static boolean DEBUG = false;
@awwsmm
awwsmm / Entries.java
Created August 17, 2018 14:11
Read external file at Java runtime
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Entries {
public static void main (String[] args) {
@awwsmm
awwsmm / Signature.java
Last active August 18, 2018 16:43
Unshuffle a Java method signature that's mis-ordered
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Signature {
public static void main(String[] args) {
// slightly more complex example to show off capabilities of method
String oldSignature = "synchronized static final native public void barFooer(int foo, double bar)";
@awwsmm
awwsmm / firstNPrimes.py
Last active September 14, 2018 13:55
Get first N primes in a list
import math
import itertools
# number of primes to generate, beginning with 2, 3, 5, ...
N = 100
def firstNPrimes(N):
primes = [2]
prime = (x for x in itertools.count(2) if not any ([x%y==0 for y in range(2, int(math.ceil(math.sqrt(x)))+1)]))
for n in range(1, int(math.ceil(N))):
@awwsmm
awwsmm / regex_escapeSequences.md
Last active September 30, 2018 13:19
Notes on escape sequences in Java Strings

To check if a string contains any escape characters:

jshell> String mytest1 = "this is a string with no escape sequences"
mytest1 ==> "this is a string with no escape sequences"

jshell> String mytest2 = "this string has a \n line break"
mytest2 ==> "this string has a \n line break"

jshell> Collections.disjoint(Arrays.asList(mytest1.split("")), Arrays.asList("\n"))
$21 ==> true
@awwsmm
awwsmm / ParseFileName.java
Created November 9, 2018 11:52
Parse any file name with Java and get path, name, and extension
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;
public class ParseFileName {
public static void main (String[] args) {
System.out.println(Arrays.toString(parseFileName("test").get()));
System.out.println(Arrays.toString(parseFileName("test.a").get()));