Java Code Snippets Flashcards
(1 cards)
1
Q
/** * Read input file and save * Catch FileNotFoundException and * IOException */ public String readFile(String filePath);
A
public String readFile(String filePath) { StringBuilder contenets = new StringBuilder); try { FileInputStream inStream = new FileInputStream(filePath); int character = inStream.read(); while (character != -1) { contents.append((char) character); character = inStream.read(); } } catch (FileNotFoundException e) { System.out.println("The file was not found at " + filePath); return ""; } catch (IOException e) { System.out.println("Unable to read file " + filePath); return ""; } return contents.toString; }