List of usage examples for java.io FileOutputStream close
public void close() throws IOException
From source file:GenSig.java
public static void main(String[] args) { /* Generate a DSA signature */ if (args.length != 1) { System.out.println("Usage: GenSig nameOfFileToSign"); } else// w w w .j av a 2 s .c o m try { /* Generate a key pair */ KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); keyGen.initialize(1024, random); KeyPair pair = keyGen.generateKeyPair(); PrivateKey priv = pair.getPrivate(); PublicKey pub = pair.getPublic(); /* * Create a Signature object and initialize it with the private * key */ Signature dsa = Signature.getInstance("SHA1withDSA", "SUN"); dsa.initSign(priv); /* Update and sign the data */ FileInputStream fis = new FileInputStream(args[0]); BufferedInputStream bufin = new BufferedInputStream(fis); byte[] buffer = new byte[1024]; int len; while (bufin.available() != 0) { len = bufin.read(buffer); dsa.update(buffer, 0, len); } ; bufin.close(); /* * Now that all the data to be signed has been read in, generate * a signature for it */ byte[] realSig = dsa.sign(); /* Save the signature in a file */ FileOutputStream sigfos = new FileOutputStream("sig"); sigfos.write(realSig); sigfos.close(); /* Save the public key in a file */ byte[] key = pub.getEncoded(); FileOutputStream keyfos = new FileOutputStream("suepk"); keyfos.write(key); keyfos.close(); } catch (Exception e) { System.err.println("Caught exception " + e.toString()); } }
From source file:examples.StatePersistence.java
public static void main(String[] args) throws Exception { // Connect to localhost and use the travel-sample bucket final Client client = Client.configure().hostnames("localhost").bucket(BUCKET).build(); // Don't do anything with control events in this example client.controlEventHandler(new ControlEventHandler() { @Override// w w w . j a v a 2 s . c om public void onEvent(ChannelFlowController flowController, ByteBuf event) { if (DcpSnapshotMarkerRequest.is(event)) { flowController.ack(event); } event.release(); } }); // Print out Mutations and Deletions client.dataEventHandler(new DataEventHandler() { @Override public void onEvent(ChannelFlowController flowController, ByteBuf event) { if (DcpMutationMessage.is(event)) { System.out.println("Mutation: " + DcpMutationMessage.toString(event)); // You can print the content via DcpMutationMessage.content(event).toString(CharsetUtil.UTF_8); } else if (DcpDeletionMessage.is(event)) { System.out.println("Deletion: " + DcpDeletionMessage.toString(event)); } event.release(); } }); // Connect the sockets client.connect().await(); // Try to load the persisted state from file if it exists File file = new File(FILENAME); byte[] persisted = null; if (file.exists()) { FileInputStream fis = new FileInputStream(FILENAME); persisted = IOUtils.toByteArray(fis); fis.close(); } // if the persisted file exists recover, if not start from beginning client.recoverOrInitializeState(StateFormat.JSON, persisted, StreamFrom.BEGINNING, StreamTo.INFINITY) .await(); // Start streaming on all partitions client.startStreaming().await(); // Persist the State ever 10 seconds while (true) { Thread.sleep(TimeUnit.SECONDS.toMillis(10)); // export the state as a JSON byte array byte[] state = client.sessionState().export(StateFormat.JSON); // Write it to a file FileOutputStream output = new FileOutputStream(new File(FILENAME)); IOUtils.write(state, output); output.close(); System.out.println(System.currentTimeMillis() + " - Persisted State!"); } }
From source file:TestSign.java
/** * Method main/* w ww . j a v a 2 s. c om*/ * * @param unused * @throws Exception */ public static void main(String unused[]) throws Exception { //J- String keystoreType = "JKS"; String keystoreFile = "data/org/apache/xml/security/samples/input/keystore.jks"; String keystorePass = "xmlsecurity"; String privateKeyAlias = "test"; String privateKeyPass = "xmlsecurity"; String certificateAlias = "test"; File signatureFile = new File("signature.xml"); //J+ KeyStore ks = KeyStore.getInstance(keystoreType); FileInputStream fis = new FileInputStream(keystoreFile); ks.load(fis, keystorePass.toCharArray()); PrivateKey privateKey = (PrivateKey) ks.getKey(privateKeyAlias, privateKeyPass.toCharArray()); javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder(); org.w3c.dom.Document doc = db.newDocument(); String BaseURI = signatureFile.toURL().toString(); XMLSignature sig = new XMLSignature(doc, BaseURI, XMLSignature.ALGO_ID_SIGNATURE_DSA); doc.appendChild(sig.getElement()); { ObjectContainer obj = new ObjectContainer(doc); Element anElement = doc.createElementNS(null, "InsideObject"); anElement.appendChild(doc.createTextNode("A text in a box")); obj.appendChild(anElement); String Id = "TheFirstObject"; obj.setId(Id); sig.appendObject(obj); Transforms transforms = new Transforms(doc); transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS); sig.addDocument("#" + Id, transforms, Constants.ALGO_ID_DIGEST_SHA1); } { X509Certificate cert = (X509Certificate) ks.getCertificate(certificateAlias); sig.addKeyInfo(cert); sig.addKeyInfo(cert.getPublicKey()); System.out.println("Start signing"); sig.sign(privateKey); System.out.println("Finished signing"); } FileOutputStream f = new FileOutputStream(signatureFile); XMLUtils.outputDOMc14nWithComments(doc, f); f.close(); System.out.println("Wrote signature to " + BaseURI); for (int i = 0; i < sig.getSignedInfo().getSignedContentLength(); i++) { System.out.println("--- Signed Content follows ---"); System.out.println(new String(sig.getSignedInfo().getSignedContentItem(i))); } }
From source file:controller.tempClass.java
/** * @desc Image manipulation - Conversion * * @filename ImageManipulation.java//from www . j a v a2 s . co m * @author Jeevanandam M. (jeeva@myjeeva.com) * @copyright myjeeva.com */ public static void main(String[] args) { File file = new File("E:\\Photograph\\Temporary\\mshien.jpg"); try { // Reading a Image file from file system FileInputStream imageInFile = new FileInputStream(file); byte imageData[] = new byte[(int) file.length()]; imageInFile.read(imageData); // Converting Image byte array into Base64 String System.out.println(encodeImage(imageData)); String imageDataString = encodeImage(imageData); // Converting a Base64 String into Image byte array byte[] imageByteArray = decodeImage(imageDataString); // Write a image byte array into file system FileOutputStream imageOutFile = new FileOutputStream("C:\\Users\\doanvanthien\\Desktop\\LOGO.png"); imageOutFile.write(imageByteArray); imageInFile.close(); imageOutFile.close(); System.out.println("Image Successfully Manipulated!"); } catch (FileNotFoundException e) { System.out.println("Image not found" + e); } catch (IOException ioe) { System.out.println("Exception while reading the Image " + ioe); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection(url, username, password); String sql = "SELECT name, description, image FROM pictures "; PreparedStatement stmt = conn.prepareStatement(sql); ResultSet resultSet = stmt.executeQuery(); while (resultSet.next()) { String name = resultSet.getString(1); String description = resultSet.getString(2); File image = new File("D:\\java.gif"); FileOutputStream fos = new FileOutputStream(image); byte[] buffer = new byte[1]; InputStream is = resultSet.getBinaryStream(3); while (is.read(buffer) > 0) { fos.write(buffer);/*from w ww . ja va 2 s.c o m*/ } fos.close(); } conn.close(); }
From source file:Example2.java
public static void main(String args[]) throws IOException, ParseException, DScabiClientException, DScabiException, java.text.ParseException { System.out.println("Example2"); DMeta meta = new DMeta("localhost", "5000"); DFile f = new DFile(meta); f.setNamespace("MyOrg.MyFiles"); Date date = new Date(); f.put("scabi:MyOrg.MyFiles:myfile1.txt", "myfile1.txt"); f.put("myfile2.txt", "myfile2.txt"); FileInputStream fis = new FileInputStream("myfile3.txt"); f.put("scabi:MyOrg.MyFiles:myfile3.txt", fis); fis.close();/*from ww w.jav a 2 s . co m*/ f.copy("scabi:MyOrg.MyFiles:myfile4.txt", "scabi:MyOrg.MyFiles:myfile1.txt"); f.copy("myfile5.txt", "scabi:MyOrg.MyFiles:myfile1.txt"); f.get("scabi:MyOrg.MyFiles:myfile1.txt", "fileout1" + "_" + date.toString() + ".txt"); f.get("myfile2.txt", "fileout2" + "_" + date.toString() + ".txt"); FileOutputStream fos = new FileOutputStream("fileout3" + "_" + date.toString() + ".txt"); f.get("scabi:MyOrg.MyFiles:myfile1.txt", fos); fos.close(); FileOutputStream fos2 = new FileOutputStream("fileout4" + "_" + date.toString() + ".txt"); f.get("myfile2.txt", fos2); fos2.close(); f.close(); meta.close(); }
From source file:MainClass.java
public static void main(String[] args) { File originFile = new File("c:\\file1.txt"); File destinationFile = new File("c:\\file1.txt"); if (!originFile.exists() || destinationFile.exists()) { return;//from ww w . ja va2s. co m } try { byte[] readData = new byte[1024]; FileInputStream fis = new FileInputStream(originFile); FileOutputStream fos = new FileOutputStream(destinationFile); int i = fis.read(readData); while (i != -1) { fos.write(readData, 0, i); i = fis.read(readData); } fis.close(); fos.close(); } catch (IOException e) { System.out.println(e); } }
From source file:FileLocking.java
public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("file.txt"); FileLock fl = fos.getChannel().tryLock(); if (fl != null) { System.out.println("Locked File"); Thread.sleep(100);//from w w w .j a v a2 s . c o m fl.release(); System.out.println("Released Lock"); } fos.close(); }
From source file:com.sight.facialrecog.util.ImageManipulation.java
/** * @param args/* w ww . jav a 2s .c o m*/ */ public static void main(String[] args) { File file = new File("/home/sight/Pictures/imagetest/close.jpg"); try { /* * Reading an Image file from file system */ FileInputStream imageInFile = new FileInputStream(file); byte imageData[] = new byte[(int) file.length()]; imageInFile.read(imageData); /* * Converting Image byte array into Base64 String */ String imageDataString = encodeImage(imageData); System.out.println(imageDataString); /* * Converting a Base64 String into Image byte array */ byte[] imageByteArray = decodeImage(imageDataString); /* * Write an image byte array into file system */ FileOutputStream imageOutFile = new FileOutputStream( "/home/sight/Pictures/imagetest/close-after-convert.jpg"); imageOutFile.write(imageByteArray); imageInFile.close(); imageOutFile.close(); System.out.println("Image Successfully Manipulated!"); } catch (FileNotFoundException e) { System.out.println("Image not found" + e); } catch (IOException ioe) { System.out.println("Exception while reading the Image " + ioe); } }
From source file:Bzip2Uncompress.java
public static void main(final String[] args) { try {/*w w w . j a v a2 s .c om*/ if (2 != args.length) { System.out.println("java Bzip2Uncompress <input> <output>"); System.exit(1); } final File source = new File(args[0]); final File destination = new File(args[1]); final FileOutputStream output = new FileOutputStream(destination); final BZip2CompressorInputStream input = new BZip2CompressorInputStream(new FileInputStream(source)); copy(input, output); input.close(); output.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } }