/** * takes an individual line and cleans it by removing comments and extra whitespace * * @param line * @return cleaned line with comments removed and extra whitespace removed * <p>NOTE: can handle spaces, can't currently handle tab characters */ public static String cleanLine(String line) { line = line.replaceAll("\\s+", " "); line = line.replaceAll(" =", "="); line = line.replaceAll("= ", "="); StringBuilder str = new StringBuilder(); boolean lastCharSpace = true; for (int i = 0; i < line.length(); i++) { // exit the loop if we're at a comment if (line.charAt(i) == '#') { break; } else if (line.charAt(i) == ' ' && !lastCharSpace) { lastCharSpace = true; str.append(line.charAt(i)); } else if (line.charAt(i) == ' ') { lastCharSpace = true; } else if (!lastCharSpace) { str.append(line.charAt(i)); } else { lastCharSpace = false; str.append(line.charAt(i)); } } // now we're going to loop through again and just delete all the // extra spaces that might exist at the end of the string for (int i = str.length() - 1; i >= 0; --i) { if (str.charAt(str.length() - 1) == ' ') { str.deleteCharAt(str.length() - 1); } else { break; } } return str.toString(); }
/** * this is called by the pingballServer * * @param file - a file * @return a string representation of the file */ public static String fileToString(File file) { StringBuilder str = new StringBuilder(); try { BufferedReader reader = new BufferedReader(new FileReader(file)); String line; try { while ((line = reader.readLine()) != null) { str.append(line); str.append("\n"); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } return str.toString(); }