//for Classes check if the value is SCALAR public static boolean is_scalar(Object var) { if( (var instanceof Number) || (var instanceof String) || (var instanceof Boolean)) return true; return false; }
java
Java : join()
Join list of strings :
//joins Iterable or Collection into a String
public static <T> String join(String delimiter, Iterable<T> lst) {
StringBuilder str = new StringBuilder();
Iterator<T> it = lst.iterator();
while ( it.hasNext() ) {
str.append(it.next().toString());
if (it.hasNext()) str.append(delimiter);
}
return str.toString();
}
Java slurping and more …
Isn’t annoying to remember all the mumbo jumbo just to open a file and what about reading a file into a string, colloquially known as slurping.
package com.java.files; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class files { // open resource as BufferedReader // Example usage : // BufferedReader br = open("file_name.txt"); // .... do something .. br.readLine() ... // br.close(); public static BufferedReader open(Class ref, String file_name) throws FileNotFoundException { InputStream stream = ref.getResourceAsStream(file_name); if (stream == null) throw new FileNotFoundException("Can't locate : " + file_name); BufferedReader br = new BufferedReader(new InputStreamReader(stream)); return br; } // open file as BufferedReader public static BufferedReader open(String file_name) throws FileNotFoundException { BufferedReader br = new BufferedReader(new FileReader(file_name)); return br; } public static void write2file(String file_name, String str) throws IOException { BufferedWriter wr = new BufferedWriter(new FileWriter(file_name)); wr.write(str); wr.close(); } //internal function to process a file for the slurps... private static String slurpit(BufferedReader br, String skip, String eol) throws Exception { StringBuilder sb = new StringBuilder(); String line; while( (line = br.readLine() ) != null ) { if(skip == null || (skip != null && !line.matches(skip)) ) { if (eol == null) { sb.append( line ); } else { sb.append(line + eol); }; } }; br.close(); return sb.toString(); } //read a whole file into a string, but skip lines that match a regex public static String slurp_skip(String file_name, String skip, String eol) throws Exception { BufferedReader br = open(file_name); return slurpit(br, skip, eol); } //read a whole file into a string, from a "jar" directories i.e. relative. // but skip lines that match a regex public static String slurp_skip(Class ref, String file_name, String skip, String eol) throws Exception { BufferedReader br = open(ref,file_name);//read resource return slurpit(br, skip, eol); } //just read the whole file in a string public static String slurp(String file_name) throws Exception { return slurp_skip(file_name, null, null); } public static String slurp(Class ref, String file_name) throws Exception { return slurp_skip(ref, file_name, null, null); } public static String slurp(Class ref, String file_name, String eol) throws Exception { return slurp_skip(ref, file_name, null, eol); } public static String slurp(String file_name, String eol) throws Exception { return slurp_skip(file_name, null, eol); } }
Example usage :
//print the first line of a file BufferedReader br = open("file_name.txt"); System.out.println( br.readLine() ) br.close(); //print a file System.out.println( slurp("file_name.txt") ) //print a file from directory relative to the .jar root or resources dir System.out.println( slurp(this.getClass(), "file_name.txt") )
Java invention of the decade
Ladies and gentleman,
I present to you the biggest invention in Java since sliced bread :
public static <ARG> void say(ARG arg) { System.out.println(arg); }
Now you can do :
say("This is less annoying than System.out.whatever"; }
… where is my award :”)