/** * Loads the bytes in a file. * * @param name the file name * @return an array with the bytes in the file */ public byte[] loadBytes(String name) throws IOException { FileInputStream in = null; in = new FileInputStream(name); try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int ch; while ((ch = in.read()) != -1) buffer.write(ch); return buffer.toByteArray(); } finally { in.close(); } }
public String readFile(java.io.File f) { StringBuffer buffer = new StringBuffer(); try { FileInputStream fis = new FileInputStream(f); InputStreamReader isr = new InputStreamReader(fis); int c; while ((c = isr.read()) != -1) buffer.append((char) c); isr.close(); fis.close(); } catch (Exception ex) { ex.printStackTrace(); } return buffer.toString(); }
// Function use to load all Records from File when Application Execute. void populateArray() { try { fis = new FileInputStream("Bank.dat"); dis = new DataInputStream(fis); // Loop to Populate the Array. while (true) { for (int i = 0; i < 6; i++) { records[rows][i] = dis.readUTF(); } rows++; } } catch (Exception ex) { total = rows; if (total == 0) { JOptionPane.showMessageDialog( null, "Records File is Empty.\nEnter Records First to Display.", "BankSystem - EmptyFile", JOptionPane.PLAIN_MESSAGE); btnEnable(); } else { try { dis.close(); fis.close(); } catch (Exception exp) { } } } }
private void laden(Path saveName) throws IOException { Properties prop = new Properties(); FileInputStream in = new FileInputStream(saveName.toString()); prop.load(in); for (int i = 0; prop.containsKey(String.format("quellMenu%d", i)); i++) quellListModel.addElement( new ListItem( Paths.get(prop.getProperty(String.format("quellMenu%d", i))), Paths.get(prop.getProperty(String.format("quellMenu%d", i))))); for (int i = 0; prop.containsKey(String.format("zielMenu%d", i)); i++) zielListModel.addElement( new ListItem( Paths.get(prop.getProperty(String.format("zielMenu%d", i))), Paths.get(prop.getProperty(String.format("zielMenu%d", i))))); in.close(); }
public void openFile(String filename) { try { FileInputStream input = new FileInputStream(filename); char x; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { x = (char) input.read(); if (x == 'P') { puzzleBox[i][j].setText(String.valueOf((char) input.read())); puzzleBox[i][j].setEnabled(false); } else { x = (char) input.read(); if (x == 'X') { puzzleBox[i][j].setText(""); puzzleBox[i][j].setEnabled(true); } else { puzzleBox[i][j].setText(String.valueOf(x)); puzzleBox[i][j].setEnabled(true); } } } x = (char) input.read(); x = (char) input.read(); } input.close(); } catch (IOException ioException) { JOptionPane.showMessageDialog(this, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE); } }
protected static boolean vnmrjPassword(String strUser, char[] password) { boolean blogin = false; try { PasswordService objPassword = PasswordService.getInstance(); String encrPassword = objPassword.encrypt(new String(password)); if (pwprops == null) { String strPath = FileUtil.openPath(WUserUtil.PASSWORD); if (strPath == null) return blogin; pwprops = new Properties(); FileInputStream fis = new FileInputStream(strPath); pwprops.load(fis); fis.close(); } String stoPassword = pwprops.getProperty(strUser); if (encrPassword.equals(stoPassword)) blogin = true; } catch (Exception e) { // e.printStackTrace(); Messages.writeStackTrace(e); } return blogin; }
/** Procedure to set value for the code-swap strings */ void getCodeSwapString() { File inputFile = new File(fileStr1); FileInputStream input = null; try { input = new FileInputStream(inputFile); } catch (FileNotFoundException fe) { System.err.println(fileStr1 + " not found. " + fe); // System.exit(-1);; } byte bt[] = new byte[(int) inputFile.length()]; try { input.read(bt); textStr1 = new String(bt); input.close(); } catch (IOException ie) { System.err.println("Can't read from the input stream " + ie); // System.exit(-1);; } inputFile = new File(fileStr2); try { input = new FileInputStream(inputFile); } catch (FileNotFoundException fe) { System.err.println(fileStr2 + " not found. " + fe); // System.exit(-1);; } try { bt = new byte[(int) inputFile.length()]; input.read(bt); textStr2 = new String(bt); input.close(); } catch (IOException ie) { System.err.println("Can't read from the input stream " + ie); // System.exit(-1);; } }
public void actionPerformed(ActionEvent e) { if (e.getSource() == jbSaveLayer) { try { FileOutputStream fout = new FileOutputStream(jtfCengMing.getText() + ".wyf"); ObjectOutputStream oout = new ObjectOutputStream(fout); oout.writeObject(itemArray); oout.close(); fout.close(); } catch (Exception ea) { ea.printStackTrace(); } } else if (e.getSource() == jbLoadLayer) { try { FileInputStream fin = new FileInputStream(jtfCengMing.getText() + ".wyf"); ObjectInputStream oin = new ObjectInputStream(fin); itemArray = (Item[][]) oin.readObject(); oin.close(); fin.close(); this.flush(); } catch (Exception ea) { ea.printStackTrace(); } lvp.repaint(); } else if (e.getSource() == jbLoadAll) { // 全部铺上当前选中 for (int row = 0; row < 40; row++) { for (int col = 0; col < 60; col++) { Item item = ((Item) (jl.getSelectedValue())).clone(); itemArray[row][col] = item; if (item != null) { item.setPosition(col, row); } } } lvp.repaint(); } else if (e.getSource() == jbCreate) { // 生成源代码 try { FileOutputStream fout = null; DataOutputStream dout = null; fout = new FileOutputStream("maps.so"); dout = new DataOutputStream(fout); int totalBlocks = 0; for (int i = 0; i < 40; i++) { for (int j = 0; j < 60; j++) { Item item = itemArray[i][j]; if (item != null) { totalBlocks++; } } } System.out.println("totalBlocks=" + totalBlocks); // 写入不空块的数量 dout.writeInt(totalBlocks); for (int i = 0; i < 40; i++) { for (int j = 0; j < 60; j++) { Item item = itemArray[i][j]; if (item != null) { int w = item.w; // 元素的图片宽度 int h = item.h; // 元素的图片高度 int col = item.col; // 元素的地图列 int row = item.row; // 元素的地图行 int pCol = item.pCol; // 元素的占位列 int pRow = item.pRow; // 元素的占位行 String leiMing = item.leiMing; // 类名 int[][] notIn = item.notIn; // 不可通过 int[][] keYu = item.keYu; // 可遇矩阵 // 计算图片下标 int outBitmapInxex = 0; if (leiMing.equals("Grass")) { outBitmapInxex = 0; } else if (leiMing.equals("XiaoHua1")) { outBitmapInxex = 1; } else if (leiMing.equals("MuZhuang")) { outBitmapInxex = 2; } else if (leiMing.equals("XiaoHua2")) { outBitmapInxex = 3; } else if (leiMing.equals("Road")) { outBitmapInxex = 4; } else if (leiMing.equals("Jing")) { outBitmapInxex = 5; } dout.writeByte(outBitmapInxex); // 记录图片下标 dout.writeByte(0); // 记录可遇标志 0-不可遇 底层都不可遇 dout.writeByte(w); // 图片宽度 dout.writeByte(h); // 图片高度 dout.writeByte(col); // 总列数 dout.writeByte(row); // 总行数 dout.writeByte(pCol); // 占位列 dout.writeByte(pRow); // 占位行 int bktgCount = notIn.length; // 不可通过点的数量 dout.writeByte(bktgCount); // 写入不可通过点的数量 for (int k = 0; k < bktgCount; k++) { dout.writeByte(notIn[k][0]); dout.writeByte(notIn[k][1]); } } } } dout.close(); fout.close(); } catch (Exception ea) { ea.printStackTrace(); } } }
public void runEvaluator() { int nSize = r.getNodeCount(); nodeValues = new Double[nSize]; allValues = new Double[nSize][]; for (int i = 0; i < nSize; i++) { allValues[i] = new Double[nSize]; } // get Oj's double[] o = new double[nSize]; if (orgFile == null) { for (int i = 0; i < nSize; i++) { o[i] = 1.0; } } else { try { FileInputStream fis = new FileInputStream(orgFile); InputStreamReader isr = new InputStreamReader(fis); BufferedReader in = new BufferedReader(isr, 8096); String line = in.readLine(); // try to guess delimiters String delim = " "; if (line.indexOf('\t') != 0) { delim = "\t"; } else if (line.indexOf(',') != 0) { delim = ","; } Hashtable values = new Hashtable(); while (line != null) { StringTokenizer st = new StringTokenizer(line, delim); line = in.readLine(); String key = st.nextToken().trim(); if (st.hasMoreTokens()) { try { Double d = Double.valueOf(st.nextToken()); values.put(key, d); } catch (NumberFormatException nfe) { // ignore? } } } fis.close(); TreeSet missingActors = new TreeSet(); for (int i = 0; i < nSize; i++) { String actorName = r.getParent().getActor(i).getName(); Double oValue = (Double) values.get(actorName); if (oValue == null) { missingActors.add(actorName); o[i] = 1.0; } else { o[i] = oValue.doubleValue(); } } if (missingActors.size() > 0) { String errorMsg = "Couldn't find organization values for "; if (missingActors.size() <= 10) { Iterator iter = missingActors.iterator(); errorMsg += "'" + iter.next() + "'"; while (iter.hasNext()) { errorMsg += ", '" + iter.next() + "'"; } errorMsg += "."; } else { errorMsg += missingActors.size() + " actors."; } JFrame f = new JFrame(); JOptionPane.showMessageDialog( f, errorMsg, "Missing Values", JOptionPane.WARNING_MESSAGE); } } catch (IOException ioe) { Application.handleNonFatalThrowable(ioe); } } // i think i need to calculate all p_ij's double[][] p = new double[nSize][]; for (int i = 0; i < nSize; i++) { p[i] = new double[nSize]; double i_out = 0.0; for (int j = 0; j < nSize; j++) { if ((i != j) || (!ignoreDiagonals)) { if (investments == OUTBOUND || investments == BOTH) { i_out += r.getTieStrength(i, j); } if (investments == INBOUND || investments == BOTH) { i_out += r.getTieStrength(j, i); } } } for (int j = 0; j < nSize; j++) { p[i][j] = 0.0; if (i_out != 0.0) { if (investments == OUTBOUND || investments == BOTH) { p[i][j] += r.getTieStrength(i, j) / i_out; } if (investments == INBOUND || investments == BOTH) { p[i][j] += r.getTieStrength(j, i) / i_out; } } } } switch (type) { // implementation of Burt (1992: 64) equation 2.7 case CONSTRAINT: for (int i = 0; i < nSize; i++) { double i_total = 0.0; allValues[i][i] = new Double(Double.NaN); for (int j = 0; j < nSize; j++) { if (i != j) { double c_sum = p[i][j]; for (int q = 0; q < nSize; q++) { if ((q != i) && (q != j)) { c_sum += p[i][q] * p[q][j]; } } double c_ij = c_sum * c_sum * o[j]; i_total += c_ij; allValues[i][j] = new Double(c_ij); } } nodeValues[i] = new Double(i_total); } break; case EFFECTIVE_SIZE: break; } }