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); } }
/** * 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(); } }
/** 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);; } }
private void openBin() { fileChooser.resetChoosableFileFilters(); fileChooser.addChoosableFileFilter(binFilter); fileChooser.setFileFilter(binFilter); if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) { try { FileInputStream inbinf = new FileInputStream(fileChooser.getSelectedFile()); int len = inbinf.available(); if (len % 2 == 1) throw new IOException(String.format("Odd file size (0x%x)\n", len)); len /= 2; if (len > 0x10000) throw new IOException(String.format("Too large file (0x%x)\n", len)); binary = new char[len]; for (int i = 0; i < len; i++) { int lo = inbinf.read(); int hi = inbinf.read(); if (lo == -1 || hi == -1) throw new IOException("Unable to read\n"); binary[i] = (char) ((hi << 8) | lo); } asmMap = new AsmMap(); Disassembler dasm = new Disassembler(); dasm.init(binary); // TODO attach asmmap StringBuilder sb = new StringBuilder(); while (dasm.getAddress() < binary.length) { int addr = dasm.getAddress(); sb.append(String.format("%-26s ; [%04x] =", dasm.next(true), addr)); int addr2 = dasm.getAddress(); while (addr < addr2) { char i = binary[addr++]; sb.append( String.format(" %04x '%s'", (int) i, (i >= 0x20 && i < 0x7f) ? (char) i : '.')); } sb.append("\n"); } srcBreakpoints.clear(); sourceRowHeader.breakpointsChanged(); sourceTextarea.setText(sb.toString()); } catch (IOException e1) { JOptionPane.showMessageDialog( frame, "Unable to open file: %s" + e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } }
public static void zipDirectory(File directory, File base, ZipOutputStream zos) throws IOException { File[] files = directory.listFiles(); byte[] buffer = new byte[8192]; int read = 0; for (int i = 0, n = files.length; i < n; i++) { if (files[i].isDirectory()) { zipDirectory(files[i], base, zos); } else { FileInputStream in = new FileInputStream(files[i]); ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1)); zos.putNextEntry(entry); while (-1 != (read = in.read(buffer))) { zos.write(buffer, 0, read); } in.close(); } } }
public static void main(String[] arg) { if (arg.length != 2) { System.err.println("usage: java ScpTo file1 user@remotehost:file2"); System.exit(-1); } FileInputStream fis = null; try { String lfile = arg[0]; String user = arg[1].substring(0, arg[1].indexOf('@')); arg[1] = arg[1].substring(arg[1].indexOf('@') + 1); String host = arg[1].substring(0, arg[1].indexOf(':')); String rfile = arg[1].substring(arg[1].indexOf(':') + 1); JSch jsch = new JSch(); Session session = jsch.getSession(user, host, 22); // username and password will be given via UserInfo interface. UserInfo ui = new MyUserInfo(); session.setUserInfo(ui); session.connect(); boolean ptimestamp = true; // exec 'scp -t rfile' remotely String command = "scp " + (ptimestamp ? "-p" : "") + " -t " + rfile; Channel channel = session.openChannel("exec"); ((ChannelExec) channel).setCommand(command); // get I/O streams for remote scp OutputStream out = channel.getOutputStream(); InputStream in = channel.getInputStream(); channel.connect(); if (checkAck(in) != 0) { System.exit(0); } File _lfile = new File(lfile); if (ptimestamp) { command = "T " + (_lfile.lastModified() / 1000) + " 0"; // The access time should be sent here, // but it is not accessible with JavaAPI ;-< command += (" " + (_lfile.lastModified() / 1000) + " 0\n"); out.write(command.getBytes()); out.flush(); if (checkAck(in) != 0) { System.exit(0); } } // send "C0644 filesize filename", where filename should not include '/' long filesize = _lfile.length(); command = "C0644 " + filesize + " "; if (lfile.lastIndexOf('/') > 0) { command += lfile.substring(lfile.lastIndexOf('/') + 1); } else { command += lfile; } command += "\n"; out.write(command.getBytes()); out.flush(); if (checkAck(in) != 0) { System.exit(0); } // send a content of lfile fis = new FileInputStream(lfile); byte[] buf = new byte[1024]; while (true) { int len = fis.read(buf, 0, buf.length); if (len <= 0) break; out.write(buf, 0, len); // out.flush(); } fis.close(); fis = null; // send '\0' buf[0] = 0; out.write(buf, 0, 1); out.flush(); if (checkAck(in) != 0) { System.exit(0); } out.close(); channel.disconnect(); session.disconnect(); System.exit(0); } catch (Exception e) { System.out.println(e); try { if (fis != null) fis.close(); } catch (Exception ee) { } } }