/* * Parts of the driver to be used in PA3 * This is *not* a working driver * Contents should be integrated with your driver from the 15min compiler example * */ // Imports you will need import java.io.BufferedReader; import java.io.FileReader; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import mjparser.*; // This goes in the main function try { // I found the syntax for loading a text resource file here: // http://www.goldenstudios.or.id/forum/archive/index.php/thread-685.html final InputStream mainPrologue = MJPA3Driver.class.getResourceAsStream("avrH.rtl.s"); // If doing this within a non-static method use // this.getClass().getClassLoader() instead of MJPA3Driver.class. System.out.println(convertStreamToString(mainPrologue)); mainPrologue.close(); System.out.println("### BODY of main"); final InputStream mainEpilogue = MJPA3Driver.class.getResourceAsStream("avrF.rtl.s"); System.out.println(convertStreamToString(mainEpilogue)); mainEpilogue.close(); } catch (final IOException e) { e.printStackTrace(); } // This doesn't! // This method was copied from http://www.kodejava.org/examples/266.html. public static String convertStreamToString(InputStream is) { /* * To convert the InputStream to String we use the * BufferedReader.readLine() method. We iterate until the BufferedReader * return null which means there's no more data to read. Each line will * appended to a StringBuilder and returned as String. */ final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); final StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (final IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (final IOException e) { e.printStackTrace(); } } return sb.toString(); }