private void saveBin() { fileChooser.resetChoosableFileFilters(); fileChooser.addChoosableFileFilter(binFilter); fileChooser.setFileFilter(binFilter); if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) { try { File file = fileChooser.getSelectedFile(); if (fileChooser.getFileFilter() == binFilter && !binFilter.accept(file)) { file = new File(file.getAbsolutePath() + binFilter.getExtensions()[0]); } if (file.exists()) { if (JOptionPane.showConfirmDialog( frame, "File exists. Overwrite?", "Confirm", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { return; } } FileOutputStream output = new FileOutputStream(file); for (char i : binary) { output.write(i & 0xff); output.write((i >> 8) & 0xff); } output.close(); } catch (IOException e1) { JOptionPane.showMessageDialog( frame, "Unable to open file", "Error", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } }
/** * Write text into a file * * @param fInp the File * @param txt the text to be written into the file */ private void WriteFile(File fInp, String txt) { // Output to file try { FileOutputStream Writer = new FileOutputStream(fInp); // Write text to file byte[] toWrite = txt.getBytes(); Writer.write(toWrite); } catch (Exception e) { } }
private void physWriteTextFile(File file, String text) { // physically write file try { FileOutputStream fo = new FileOutputStream(file); fo.write(text.getBytes()); fo.close(); } catch (FileNotFoundException e) { // not sure how this can happen showErrorDialog("Saving failed due to file not found error?"); } catch (IOException e) { // This happens if e.g. file already exists and // we do not have write permissions showErrorDialog("Saving failed due I/O error."); } }
private static String writeFileBytes(String path, byte[] data) { try { if (data.length>=524288 && !path.endsWith("JmolApplet.jar") ){ //gzip it path += ".gz"; GZIPOutputStream gzFile = new GZIPOutputStream(new FileOutputStream(path)); gzFile.write(data); LogPanel.log(" ..." + GT._("compressing large data file to") + "\n"); gzFile.flush(); gzFile.close(); } else { FileOutputStream os = new FileOutputStream(path); os.write(data); os.flush(); os.close(); } } catch (IOException e) { LogPanel.log(e.getMessage()); } return path; }
public void actionPerformed(ActionEvent ae) { Debug.message("saveimage", "SaveAsImageMenuItem: actionPerformed"); if (mapHandler == null) { Debug.output("SaveAsImageMenuItem: mapHandler = null, returning"); return; } MapBean mb = (MapBean) mapHandler.get("com.bbn.openmap.MapBean"); if (mb != null) { Debug.message("saveimage", "MapBean found, creating image"); try { while (true) { SaveAsImageFileChooser chooser = new SaveAsImageFileChooser(mb.getWidth(), mb.getHeight()); int returnVal = chooser.showSaveDialog(getParent()); if (returnVal == JFileChooser.APPROVE_OPTION) { String filename = chooser.getSelectedFile().getAbsolutePath(); if (formatter == null) { break; } filename = checkFileName(filename, formatter.getFormatLabel().toLowerCase()); if (filename == null) { // This is the reason for the while // loop, the name didn't really pass // muster, so we'll try again. continue; } int imageHeight = chooser.getImageHeight(); int imageWidth = chooser.getImageWidth(); byte[] imageBytes = formatter.getImageFromMapBean(mb, imageWidth, imageHeight); FileOutputStream binFile = new FileOutputStream(filename); binFile.write(imageBytes); binFile.close(); if (Debug.debugging("saveimage")) { com.bbn.openmap.proj.Projection proj = mb.getProjection(); Debug.output( "Created image at " + filename + "where projection covers " + proj.getUpperLeft() + " to " + proj.getLowerRight()); } break; } else if (returnVal == JFileChooser.CANCEL_OPTION) { break; } } } catch (IOException e) { Debug.error("SaveAsImageMenuItem: " + e); } } return; }
public static void main(String[] arg) { if (arg.length != 2) { System.err.println("usage: java ScpFrom user@remotehost:file1 file2"); System.exit(-1); } FileOutputStream fos = null; try { String user = arg[0].substring(0, arg[0].indexOf('@')); arg[0] = arg[0].substring(arg[0].indexOf('@') + 1); String host = arg[0].substring(0, arg[0].indexOf(':')); String rfile = arg[0].substring(arg[0].indexOf(':') + 1); String lfile = arg[1]; String prefix = null; if (new File(lfile).isDirectory()) { prefix = lfile + File.separator; } 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(); // exec 'scp -f rfile' remotely String command = "scp -f " + 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(); byte[] buf = new byte[1024]; // send '\0' buf[0] = 0; out.write(buf, 0, 1); out.flush(); while (true) { int c = checkAck(in); if (c != 'C') { break; } // read '0644 ' in.read(buf, 0, 5); long filesize = 0L; while (true) { if (in.read(buf, 0, 1) < 0) { // error break; } if (buf[0] == ' ') break; filesize = filesize * 10L + (long) (buf[0] - '0'); } String file = null; for (int i = 0; ; i++) { in.read(buf, i, 1); if (buf[i] == (byte) 0x0a) { file = new String(buf, 0, i); break; } } // System.out.println("filesize="+filesize+", file="+file); // send '\0' buf[0] = 0; out.write(buf, 0, 1); out.flush(); // read a content of lfile fos = new FileOutputStream(prefix == null ? lfile : prefix + file); int foo; while (true) { if (buf.length < filesize) foo = buf.length; else foo = (int) filesize; foo = in.read(buf, 0, foo); if (foo < 0) { // error break; } fos.write(buf, 0, foo); filesize -= foo; if (filesize == 0L) break; } fos.close(); fos = null; if (checkAck(in) != 0) { System.exit(0); } // send '\0' buf[0] = 0; out.write(buf, 0, 1); out.flush(); } session.disconnect(); System.exit(0); } catch (Exception e) { System.out.println(e); try { if (fos != null) fos.close(); } catch (Exception ee) { } } }