Java Code
I've made several useful Java methods for anyone to freely use and modify.
PrettyPrinter - Good formatting.
Hilite - Colorization of code.
Ideone - Allowing Java code to be easily ran.
PrettyPrinter - Good formatting.
Hilite - Colorization of code.
Ideone - Allowing Java code to be easily ran.
Gets a string value from a string array from a specified string keyword.
// To see this being run, go here: http://ideone.com/PeSWbr public static String getNameFromKey(String[] contents, String key) { String result = null; if (contents != null && key != null) { for (String line : contents) { int keyLength = key.length(); if (line.length() > keyLength + 2 && line.substring(0, keyLength).equals(key) && line.substring(keyLength, keyLength + 1).equals("=")) { result = line.substring(keyLength + 1); break; } } } return result; }
Gets string values from a string array from specified keywords in another string array.
// To see the code being run, go here: http://ideone.com/fnnBhP public static String[] getNamesFromKeys(String[] contents, String[] keys) { String[] result = new String[keys.length]; if (contents != null && keys != null) { for (String line : contents) { for (int index = 0; index < keys.length; index++) { int keyLength = keys[index].length(); if (line.length() > keyLength + 2 && line.substring(0, keyLength).equals(keys[index]) && line.substring(keyLength, keyLength + 1).equals("=")) { result[index] = line.substring(keyLength + 1); } } } } return result; }
Gets the HTML contents of a website. Throws an IOException if something goes wrong, must be caught and handled properly in order to prevent a crash.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; // Note: This code will not run on Ideone public static String[] getContentsOfURL(String address) throws IOException { ArrayList<String> list = new ArrayList<String>(); URL url = new URL(address); URLConnection connection = url.openConnection(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { list.add(line); } reader.close(); String[] result = new String[list.size()]; for (int index = 0; index < result.length; index++) { result[index] = list.get(index); } return result; }
Gets the contents of a specified file in the form of a string array. Throws an IOException is the process fails and must be caught and handled to prevent a crash.
import java.io.File; import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; // Note: This code will not run on Ideone public static String[] getContentsOfFile(File file) throws IOException { ArrayList<String> list = new ArrayList<String>(); if (file != null && file.canRead()) { BufferedReader reader = new BufferedReader(new FileReader(file)); String line; while ((line = reader.readLine()) != null) { list.add(line); } reader.close(); } String[] result = new String[list.size()]; for (int index = 0; index < result.length; index++) { result[index] = list.get(index); } return result; }