List of usage examples for java.io FileOutputStream close
public void close() throws IOException
From source file:SerializationUtilsTrial.java
public static void main(String[] args) { try {//from ww w . j a v a 2 s.c om // File to serialize object to String fileName = "testSerialization.ser"; // New file output stream for the file FileOutputStream fos = new FileOutputStream(fileName); // Serialize String SerializationUtils.serialize("SERIALIZE THIS", fos); fos.close(); // Open FileInputStream to the file FileInputStream fis = new FileInputStream(fileName); // Deserialize and cast into String String ser = (String) SerializationUtils.deserialize(fis); System.out.println(ser); fis.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:shutdown.java
License:asdf
public static void main(String[] args) throws ClassNotFoundException, IOException { FileOutputStream fos = new FileOutputStream("objects.tmp"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject("asdf"); oos.flush();/* w w w . j a va 2s. c o m*/ fos.close(); FileInputStream fis = new FileInputStream("objects.tmp"); ObjectInputStream ois = new ObjectInputStream(fis); String t = (String) ois.readObject(); fis.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { int sChunk = 8192; String zipname = "a.txt.gz"; String source = "a.txt"; FileInputStream in = new FileInputStream(zipname); GZIPInputStream zipin = new GZIPInputStream(in); byte[] buffer = new byte[sChunk]; FileOutputStream out = new FileOutputStream(source); int length;//from w w w . jav a 2 s . co m while ((length = zipin.read(buffer, 0, sChunk)) != -1) out.write(buffer, 0, length); out.close(); zipin.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String zipname = "data.txt.gz"; String source = "data.txt.gz"; GZIPInputStream zipin;/*from w w w.jav a 2 s . c o m*/ FileInputStream in = new FileInputStream(zipname); zipin = new GZIPInputStream(in); byte[] buffer = new byte[sChunk]; // decompress the file FileOutputStream out = new FileOutputStream(source); int length; while ((length = zipin.read(buffer, 0, sChunk)) != -1) out.write(buffer, 0, length); out.close(); zipin.close(); }
From source file:FileIOApp.java
public static void main(String args[]) throws IOException { FileOutputStream outStream = new FileOutputStream("test.txt"); String s = "This is a test."; for (int i = 0; i < s.length(); ++i) outStream.write(s.charAt(i));/* w w w . j a v a 2s . co m*/ outStream.close(); FileInputStream inStream = new FileInputStream("test.txt"); int inBytes = inStream.available(); System.out.println("inStream has " + inBytes + " available bytes"); byte inBuf[] = new byte[inBytes]; int bytesRead = inStream.read(inBuf, 0, inBytes); System.out.println(bytesRead + " bytes were read"); System.out.println("They are: " + new String(inBuf)); inStream.close(); File f = new File("test.txt"); f.delete(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String keystoreFile = "keyStoreFile.bin"; String caAlias = "caAlias"; String certToSignAlias = "cert"; String newAlias = "newAlias"; char[] password = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; char[] caPassword = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; char[] certPassword = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; FileInputStream input = new FileInputStream(keystoreFile); KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(input, password);//from www. ja v a2 s .c o m input.close(); PrivateKey caPrivateKey = (PrivateKey) keyStore.getKey(caAlias, caPassword); java.security.cert.Certificate caCert = keyStore.getCertificate(caAlias); byte[] encoded = caCert.getEncoded(); X509CertImpl caCertImpl = new X509CertImpl(encoded); X509CertInfo caCertInfo = (X509CertInfo) caCertImpl.get(X509CertImpl.NAME + "." + X509CertImpl.INFO); X500Name issuer = (X500Name) caCertInfo.get(X509CertInfo.SUBJECT + "." + CertificateIssuerName.DN_NAME); java.security.cert.Certificate cert = keyStore.getCertificate(certToSignAlias); PrivateKey privateKey = (PrivateKey) keyStore.getKey(certToSignAlias, certPassword); encoded = cert.getEncoded(); X509CertImpl certImpl = new X509CertImpl(encoded); X509CertInfo certInfo = (X509CertInfo) certImpl.get(X509CertImpl.NAME + "." + X509CertImpl.INFO); Date firstDate = new Date(); Date lastDate = new Date(firstDate.getTime() + 365 * 24 * 60 * 60 * 1000L); CertificateValidity interval = new CertificateValidity(firstDate, lastDate); certInfo.set(X509CertInfo.VALIDITY, interval); certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber((int) (firstDate.getTime() / 1000))); certInfo.set(X509CertInfo.ISSUER + "." + CertificateSubjectName.DN_NAME, issuer); AlgorithmId algorithm = new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid); certInfo.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, algorithm); X509CertImpl newCert = new X509CertImpl(certInfo); newCert.sign(caPrivateKey, "MD5WithRSA"); keyStore.setKeyEntry(newAlias, privateKey, certPassword, new java.security.cert.Certificate[] { newCert }); FileOutputStream output = new FileOutputStream(keystoreFile); keyStore.store(output, password); output.close(); }
From source file:FileOutputStreamDemo.java
public static void main(String args[]) throws Exception { FileOutputStream fos = new FileOutputStream(args[0]); // Write 12 bytes to the file for (int i = 0; i < 12; i++) { fos.write(i);/*from w w w. j a va 2 s .c o m*/ } // Close file output stream fos.close(); }
From source file:Main.java
static public void main(String[] arg) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("foo.xml"); TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); NodeList list = doc.getFirstChild().getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node element = list.item(i).cloneNode(true); if (element.hasChildNodes()) { Source src = new DOMSource(element); FileOutputStream fs = new FileOutputStream("k" + i + ".xml"); Result dest = new StreamResult(fs); aTransformer.transform(src, dest); fs.close(); }//from w ww .j a v a 2 s . c o m } }
From source file:com.npower.dm.util.XMLPrettyFormatter.java
/** * TestCase & Demo.// w w w .j a va2 s. c o m * @param args */ public static void main(String[] args) { try { //String text = "<A><B></B></A>"; //XMLPrettyFormatter formatter = new XMLPrettyFormatter(text); File file = new File("D:/Zhao/MyWorkspace/nWave-DM-Common/metadata/ddf/sony_ericsson/M600_DDF.xml"); XMLPrettyFormatter formatter = new XMLPrettyFormatter(file); String result = formatter.format(); FileOutputStream out = new FileOutputStream("C:/temp/M600_DDF.xml"); out.write(result.getBytes()); out.close(); } catch (DocumentException e) { log.info("XMLPrettyFormatter:" + e); } catch (IOException e) { log.info("XMLPrettyFormatter:" + e); } }
From source file:com.streamreduce.util.CAGenerator.java
public static void main(String[] args) throws Exception { KeyStore store = KeyStore.getInstance("JKS"); // store.load(CAGenerator.class.getResourceAsStream("/mmc-keystore.jks"), "ion-mmc".toCharArray()); store.load(null);/*from ww w . j a va 2s. c o m*/ KeyPair keypair = generateKeyPair(); X509Certificate cert = generateCACert(keypair); char[] password = "nodeable-agent".toCharArray(); store.setKeyEntry("nodeable", keypair.getPrivate(), password, new Certificate[] { cert }); store.store(new FileOutputStream("nodeable-keystore.jks"), password); byte[] certBytes = getCertificateAsBytes(cert); FileOutputStream output = new FileOutputStream("nodeable.crt"); IOUtils.copy(new ByteArrayInputStream(certBytes), output); output.close(); }