String urlParameters = "param1=a¶m2=b¶m3=c"; byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 ); int postDataLength = postData.length; String request = "http://example.com/index.php"; URL url = new URL( request ); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setDoOutput( true ); conn.setInstanceFollowRedirects( false ); conn.setRequestMethod( "POST" ); conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty( "charset", "utf-8"); conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength )); conn.setUseCaches( false ); try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) { wr.write( postData ); }
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(yourFile)); bos.write(fileBytes); bos.flush(); bos.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream w = new DataOutputStream(baos); w.writeInt(100); w.write(byteArray); w.flush(); byte[] result = baos.toByteArray();
public static byte[] zipBytes(String filename, byte[] input) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); ZipEntry entry = new ZipEntry(filename); entry.setSize(input.length); zos.putNextEntry(entry); zos.write(input); zos.closeEntry(); zos.close(); return baos.toByteArray(); }
FileOutputStream fos = new FileOutputStream("hello-world.zip"); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos); try { for (int i = 0; i < 10; i++) { // not available on BufferedOutputStream zos.putNextEntry(new ZipEntry("hello-world." + i + ".txt")); zos.write("Hello World!".getBytes()); // not available on BufferedOutputStream zos.closeEntry(); } } finally { zos.close(); }
new ByteArrayOutputStream(dataToCompress.length); try try zipStream.write(dataToCompress); new FileOutputStream("C:/Users/UserName/Desktop/zip_file.gz"); try
String fileString; //.. //read from the file //.. //for windows fileString = fileString.replaceAll("\\r\\n", "\n"); fileString = fileString.replaceAll("\\r", "\n"); //.. //write to file in binary mode.. something like: DataOutputStream os = new DataOutputStream(new FileOutputStream("fname.txt")); os.write(fileString.getBytes()); //.. //send file //..
ZipOutputStream append = new ZipOutputStream(new FileOutputStream("append.zip")); System.out.println("append: " + e.getName()); append.putNextEntry(e); append.write("42\n".getBytes()); append.closeEntry();
@GET @Path("/helloWorldZip") @Produces(MediaType.APPLICATION_OCTET_STREAM) public StreamingOutput helloWorldZip() throws Exception { return new StreamingOutput(){ @Override public void write(OutputStream arg0) throws IOException, WebApplicationException { // TODO Auto-generated method stub BufferedOutputStream bus = new BufferedOutputStream(arg0); try { //ByteArrayInputStream reader = (ByteArrayInputStream) Thread.currentThread().getContextClassLoader().getResourceAsStream(); //byte[] input = new byte[2048]; java.net.URL uri = Thread.currentThread().getContextClassLoader().getResource(""); File file = new File("D:\\Test1.zip"); FileInputStream fizip = new FileInputStream(file); byte[] buffer2 = IOUtils.toByteArray(fizip); bus.write(buffer2); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }
+ Boundary); DataOutputStream httpOut = new DataOutputStream(theUrlConnection.getOutputStream()); + "\r\n"; httpOut.write(str.getBytes()); : new byte[availableBytesToRead]; uploadFileReader.read(bufferBytesRead); httpOut.write(bufferBytesRead); httpOut.flush(); httpOut.write(("--" + Boundary + "--\r\n").getBytes()); httpOut.write(("--" + Boundary + "--\r\n").getBytes()); httpOut.close();
BufferedOutputStream bos = new BufferedOutputStream(f); bos.write(inputMsg); //bos.flush(); stop. always. flushing. close. does. that. bos.close();
private void writeTextFile(String fName, String text) { DataOutputStream os = null; FileConnection fconn = null; try { fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE); if (!fconn.exists()) fconn.create(); os = fconn.openDataOutputStream(); os.write(text.getBytes()); } catch (IOException e) { System.out.println(e.getMessage()); } finally { try { if (null != os) os.close(); if (null != fconn) fconn.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } }
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "your_folder_on_sd", "file_name"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); byte[] yourKey = generateKey("password"); byte[] filesBytes = encodeFile(yourKey, yourByteArrayContainigDataToEncrypt); bos.write(fileBytes); bos.flush(); bos.close();
String s = "hello world"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try(ZipOutputStream zos = new ZipOutputStream(baos)) { /* File is not on the disk, test.txt indicates only the file name to be put into the zip */ ZipEntry entry = new ZipEntry("test.txt"); zos.putNextEntry(entry); zos.write(s.getBytes()); zos.closeEntry(); /* use more Entries to add more files and use closeEntry() to close each file entry */ } catch(IOException ioe) { ioe.printStackTrace(); }
String filename= "C:/SO/SOBufferedOutputStreamAnswer"; BufferedOutputStream bos = null; try { //create an object of FileOutputStream FileOutputStream fos = new FileOutputStream(new File(filename)); //create an object of BufferedOutputStream bos = new BufferedOutputStream(fos); KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = kgen.generateKey(); byte[] encoded = key.getEncoded(); bos.write(encoded); } // catch and handle exceptions...
ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.write(uuid.getMostSignificantBits()); dos.write(uuid.getLeastSignificantBits()); dos.flush(); // May not be necessary byte[] data = dos.toByteArray();
FileOutputStream fileOut = new FileOutputStream("foo.dat"); DataOutputStream dataOut = new DataOutputStream(fileOut); dataOut.writeInt(numFiles); dataOut.writeInt(numBytesInName); dataOut.writeUTF(filename); dataOut.writeLong(numBytesInFile); dataOut.write(fileBytes);
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class BigJarCreator { public static void main(String[] args) throws IOException { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File("big.jar"))); zos.putNextEntry(new ZipEntry("resource.txt")); zos.write("not too much in here".getBytes()); zos.closeEntry(); zos.putNextEntry(new ZipEntry("largeFile.out")); for (int i=0 ; i<10000000 ; i++) { zos.write((int) (Math.round(Math.random()*100)+20)); } zos.closeEntry(); zos.close(); } }
public class FilterServletOutputStream extends ServletOutputStream { DataOutputStream output; public FilterServletOutputStream(OutputStream output) { this.output = new DataOutputStream(output); } @Override public void write(int arg0) throws IOException { output.write(arg0); } @Override public void write(byte[] arg0, int arg1, int arg2) throws IOException { output.write(arg0, arg1, arg2); } @Override public void write(byte[] arg0) throws IOException { output.write(arg0); } }
/** * Writes the byte[] to the stream, prefixed by an ASCII STX character and * terminated with an ASCII ETX character. */ @Override public void serialize(byte[] bytes, OutputStream outputStream) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(outputStream); bos.write(STX); bos.write(bytes); bos.write(ETX); bos.flush(); }