/* for an existing file */ private void load() { /* create temporary buffer */ byte[] buffer = new byte[MAX_FILE_SIZE]; try { /* create a FileInputStream */ FileInputStream fin = new FileInputStream(file); /* read the data into a byte array */ fin.read(buffer); /* close the stream */ fin.close(); } catch (Exception e) { System.out.println("IO Error: " + e); } data = new String(buffer); }
private void readImage() throws FileNotFoundException, IOException, NumberFormatException { bytes = null; char buffer; String id = new String(); String dim = new String(); int count = 0; File f = new File(filename); FileInputStream isr = new FileInputStream(f); boolean weird = false; do { buffer = (char) isr.read(); id = id + buffer; count++; } while (buffer != '\n' && buffer != ' '); if (id.charAt(0) == 'P') { buffer = (char) isr.read(); count++; if (buffer == '#') { do { buffer = (char) isr.read(); count++; } while (buffer != '\n'); count++; buffer = (char) isr.read(); } do { dim = dim + buffer; buffer = (char) isr.read(); count++; } while (buffer != ' ' && buffer != '\n'); width = Integer.parseInt(dim); dim = new String(); buffer = (char) isr.read(); count++; do { dim = dim + buffer; buffer = (char) isr.read(); count++; } while (buffer != ' ' && buffer != '\n'); height = Integer.parseInt(dim); do { buffer = (char) isr.read(); count++; } while (buffer != ' ' && buffer != '\n'); bytes = new byte[height * width]; doubles = new double[height * width]; if ((height * width + count * 2) < f.length()) weird = true; if ((id.charAt(1) == '5') || (id.charAt(1) == '6')) { if (!weird) isr.read(bytes, 0, height * width); else { int v = 0; for (int i = 0; i < height * width; i++) { v = isr.read(); v = v + isr.read(); v = v + isr.read(); v = v / 3; bytes[i] = (byte) (v & 0xFF); } } } if (id.charAt(1) == '2') { int i = 0; for (i = 0; i < width * height; i++) { dim = new String(); do { buffer = (char) isr.read(); if (buffer != ' ' && buffer != '\n') dim = dim + buffer; } while (buffer != ' ' && buffer != '\n'); bytes[i] = (byte) (Integer.parseInt(dim) & 0xFF); } } for (int i = 0; i < height * width; i++) doubles[i] = (double) (bytes[i] & 0xFF); isr.close(); } else { width = height = 0; doubles = new double[0]; bytes = new byte[0]; throw new NumberFormatException("Wrong header information!"); } }
/** Close the file we've read. */ public void closeRead() throws Exception { fileInput.close(); }
public static void main(String[] args) { File file = new File("./shapesInput.txt"); int ch; StringBuffer strContent = new StringBuffer(""); FileInputStream fin = null; try { fin = new FileInputStream(file); while ((ch = fin.read()) != -1) { strContent.append((char) ch); } fin.close(); } catch (FileNotFoundException e) { System.out.println("File" + file.getAbsolutePath() + " could not be found on filesystem"); } catch (IOException ioe) { System.out.println("Exception while reading the file" + ioe); } String fileOutput = strContent.toString(); String[] commands = fileOutput.split(";"); List<Shape> shapes = new ArrayList<Shape>(); List<Double> areas = new ArrayList<Double>(); List<Double> areasD = new ArrayList<Double>(); for (int i = 0; i <= commands.length - 1; i++) { String codeLine = commands[i]; String shapeType = codeLine.substring(17, 20); switch (shapeType) { case "Cir": String rad = codeLine.substring(23, codeLine.length()); rad = rad.replace("(", ""); rad = rad.replace(")", ""); int radius; radius = Integer.parseInt(rad); shapes.add(new Circle(radius)); break; case "Rec": String len = codeLine.substring(26, codeLine.length()); len = len.replace("(", ""); len = len.replace(")", ""); String[] params = len.split(","); String h = params[0]; String l = params[1]; int height = Integer.parseInt(h); int length = Integer.parseInt(l); shapes.add(new Rectangle(height, length)); break; case "Rho": String stuff = codeLine.substring(24, codeLine.length()); stuff = stuff.replace("(", ""); stuff = stuff.replace(")", ""); String[] parame = stuff.split(","); String first = parame[0]; String second = parame[1]; int fir = Integer.parseInt(first); int sec = Integer.parseInt(second); shapes.add(new Rhombus(fir, sec)); break; case "Tra": String total = codeLine.substring(26, codeLine.length()); total = total.replace("(", ""); total = total.replace(")", ""); String[] seperated = total.split(","); String onep = seperated[0]; String twop = seperated[1]; String threep = seperated[2]; int op = Integer.parseInt(onep); int tp = Integer.parseInt(twop); int thp = Integer.parseInt(threep); shapes.add(new Trapezoid(op, tp, thp)); break; case "Tri": String measures = codeLine.substring(25, codeLine.length()); measures = measures.replace("(", ""); measures = measures.replace(")", ""); String[] listp = measures.split(","); String aaa = listp[0]; String bbb = listp[1]; int aa = Integer.parseInt(aaa); int bb = Integer.parseInt(bbb); shapes.add(new Triangle(aa, bb)); break; } } for (int q = 0; q <= shapes.size(); q++) { for (int i = 0; i <= shapes.size() - 2; i++) { if (shapes.get(i).getArea() > shapes.get(i + 1).getArea()) { Shape temp; temp = shapes.get(i); shapes.set(i, shapes.get(i + 1)); shapes.set(i + 1, temp); } else { } } } for (Shape s : shapes) { System.out.println("\nCalculating " + s.getShapeName() + " area:"); System.out.println("Area = " + s.getArea()); System.out.println("Printing " + s.getShapeName() + " description:"); s.printDescription(); } }
/* public methods */ public DirectedGraph parse() { /* add the parts of speech */ addPOS(); /* update all node to include parts of speech */ System.out.println("Parsing file for parts of speech analysis..."); byte[] buffer = new byte[100000]; // 100 kb try { /* retrieve the data from the file */ FileInputStream fin = new FileInputStream(file); fin.read(buffer); fin.close(); } catch (Exception e) { System.out.println("IO Error: " + e); } /* transfer data to a string */ String data = new String(buffer); /* create a tokenizer to parse the data */ StringTokenizer st = new StringTokenizer( data, " :;\"\n\t\r_,.!?`\u2015\u2012\u2014\u2013\u2212"); // unicode for dash /* temporary variables */ String pos = ""; String wordString = ""; Word word = new Word(""); Node node = new Node(word); while (st.hasMoreTokens()) { /* take care of extraneous hiphens */ String test = st.nextToken(); while (test.equals("-")) { test = st.nextToken(); } while (test.equals("--")) { test = st.nextToken(); } /* put the string to lowercase */ wordString = test; wordString = wordString.toLowerCase(); /* get the POS */ if (st.hasMoreTokens()) { pos = st.nextToken(); } /* if we have the possessive case */ if (wordString.equals("'s")) { /* create a node object from the previous iteration */ Word possessiveWord = new Word(word.toString() + "'s"); node = new Node(possessiveWord); /* get the position of the node in the graph */ int index = result.findIndex(node); /* add the possessive node to the graph */ if (index >= 0) { /* make sure we get all the associations */ node = result.nodeAt(index); /* transfer the part of speech */ node.getWord().setPartOfSpeech(word.getPartOfSpeech()); /* add the possessive quality */ node.getWord().setPossessive(); /* insert the node into our array at the right position */ (result.getNodes())[index] = node; } } else { word = new Word(wordString); /* lots of if statements */ if (pos.equals("AFX")) { word.setPartOfSpeech(1); } else if (pos.equals("CC")) { word.setPartOfSpeech(2); } else if (pos.equals("CD")) { word.setPartOfSpeech(3); } else if (pos.equals("DT")) { word.setPartOfSpeech(4); } else if (pos.equals("EX")) { word.setPartOfSpeech(5); } else if (pos.equals("FW")) { word.setPartOfSpeech(6); } else if (pos.equals("IN")) { word.setPartOfSpeech(7); } else if (pos.equals("JJ")) { word.setPartOfSpeech(8); } else if (pos.equals("JJR")) { word.setPartOfSpeech(9); } else if (pos.equals("JJS")) { word.setPartOfSpeech(10); } else if (pos.equals("LS")) { word.setPartOfSpeech(11); } else if (pos.equals("MD")) { word.setPartOfSpeech(12); } else if (pos.equals("NN")) { word.setPartOfSpeech(13); } else if (pos.equals("NNP")) { word.setPartOfSpeech(14); } else if (pos.equals("NNPS")) { word.setPartOfSpeech(15); } else if (pos.equals("NNS")) { word.setPartOfSpeech(16); } else if (pos.equals("PDT")) { word.setPartOfSpeech(17); } else if (pos.equals("POS")) { word.setPartOfSpeech(18); } else if (pos.equals("PRP")) { word.setPartOfSpeech(19); } else if (pos.equals("PRP$")) { word.setPartOfSpeech(20); } else if (pos.equals("RB")) { word.setPartOfSpeech(21); } else if (pos.equals("RBR")) { word.setPartOfSpeech(22); } else if (pos.equals("RBS")) { word.setPartOfSpeech(23); } else if (pos.equals("RP")) { word.setPartOfSpeech(24); } else if (pos.equals("SYM")) { word.setPartOfSpeech(25); } else if (pos.equals("TO")) { word.setPartOfSpeech(26); } else if (pos.equals("UH")) { word.setPartOfSpeech(27); } else if (pos.equals("VB")) { word.setPartOfSpeech(28); } else if (pos.equals("VBD")) { word.setPartOfSpeech(29); } else if (pos.equals("VBG")) { word.setPartOfSpeech(30); } else if (pos.equals("VBN")) { word.setPartOfSpeech(31); } else if (pos.equals("VBP")) { word.setPartOfSpeech(32); } else if (pos.equals("VBZ")) { word.setPartOfSpeech(33); } else if (pos.equals("WDT")) { word.setPartOfSpeech(34); } else if (pos.equals("WP")) { word.setPartOfSpeech(35); } else if (pos.equals("WPS")) { word.setPartOfSpeech(36); } else if (pos.equals("WRB")) { word.setPartOfSpeech(37); } node = new Node(word); int index = result.findIndex(node); if (index >= 0) { /* make sure we get all the associations */ node = result.nodeAt(index); /* transfer the part of speech */ node.getWord().setPartOfSpeech(word.getPartOfSpeech()); /* insert the node into our array at the right position */ (result.getNodes())[index] = node; } } } // end while return result; }