Example usage for java.io FileOutputStream write

List of usage examples for java.io FileOutputStream write

Introduction

In this page you can find the example usage for java.io FileOutputStream write.

Prototype

public void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this file output stream.

Usage

From source file:FileSplitter.java

public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream(args[0]);
    int size = 1024;
    byte buffer[] = new byte[size];

    int count = 0;
    while (true) {
        int i = fis.read(buffer, 0, size);
        if (i == -1)
            break;

        String filename = args[1] + count;
        FileOutputStream fos = new FileOutputStream(filename);
        fos.write(buffer, 0, i);
        fos.flush();//from  www . j  a  v  a 2s.co  m
        fos.close();

        ++count;
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    byte[] b = new byte[1];
    URL url = new URL("http://www.server.com/a.gif");
    URLConnection urlConnection = url.openConnection();
    urlConnection.connect();//from  w ww . j  a va  2  s  . c o  m
    DataInputStream di = new DataInputStream(urlConnection.getInputStream());

    FileOutputStream fo = new FileOutputStream("a.gif");
    while (-1 != di.read(b, 0, 1))
        fo.write(b, 0, 1);
    di.close();
    fo.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {

    byte[] b = { 65, 66, 67, 68, 69 };
    int i = 0;//from   w  w w  .j a va 2 s.  c o  m

    FileOutputStream fos = new FileOutputStream("C://test.txt");

    // writes byte to the output stream
    fos.write(b, 2, 3);

    // flushes the content to the underlying stream
    fos.flush();

    // create new file input stream
    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;
        System.out.print(c);
    }
}

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;//  w w w .  j  av a2s.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   www.j a v  a  2s. c om

    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: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;//w  w  w .j a  v a  2 s.c o 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:Main.java

public static void main(String[] args) throws Exception {

    FileInputStream fin = null;//from  w  ww .  ja  v  a  2s.  c o  m
    FileOutputStream fout = null;

    File file = new File("C:/myfile1.txt");

    fin = new FileInputStream(file);
    fout = new FileOutputStream("C:/myfile2.txt");

    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = fin.read(buffer)) > 0) {
        fout.write(buffer, 0, bytesRead);
    }
    fin.close();
    fout.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    Deflater def = new Deflater();
    byte[] input = new byte[1024];
    byte[] output = new byte[1024];

    FileInputStream fin = new FileInputStream("a.dat");
    FileOutputStream fout = new FileOutputStream("b.dat");

    int numRead = fin.read(input);

    def.setInput(input, 0, numRead);/*  www .  j  a  v  a2  s.  c  o  m*/

    while (!def.needsInput()) {
        int numCompressedBytes = def.deflate(output, 0, output.length);
        if (numCompressedBytes > 0) {
            fout.write(output, 0, numCompressedBytes);
        }
    }
    def.finish();
    fin.close();
    fout.flush();
    fout.close();
    def.reset();
}

From source file:com.graphhopper.tools.Bzip2.java

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        throw new IllegalArgumentException("You need to specify the bz2 file!");
    }//  www. j a  v a  2s . c  om

    String fromFile = args[0];
    if (!fromFile.endsWith(".bz2")) {
        throw new IllegalArgumentException("You need to specify a bz2 file! But was:" + fromFile);
    }
    String toFile = Helper.pruneFileEnd(fromFile);

    FileInputStream in = new FileInputStream(fromFile);
    FileOutputStream out = new FileOutputStream(toFile);
    BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
    try {
        final byte[] buffer = new byte[1024 * 8];
        int n = 0;
        while (-1 != (n = bzIn.read(buffer))) {
            out.write(buffer, 0, n);
        }
    } finally {
        out.close();
        bzIn.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String destinationname = "d:\\";
    byte[] buf = new byte[1024];
    ZipInputStream zipinputstream = null;
    ZipEntry zipentry;/*w ww .  j a  va  2 s.  co  m*/
    zipinputstream = new ZipInputStream(new FileInputStream("fileName"));
    zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        String entryName = zipentry.getName();
        FileOutputStream fileoutputstream;
        File newFile = new File(entryName);
        String directory = newFile.getParent();

        if (directory == null) {
            if (newFile.isDirectory())
                break;
        }
        fileoutputstream = new FileOutputStream(destinationname + entryName);
        int n;
        while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
            fileoutputstream.write(buf, 0, n);
        }
        fileoutputstream.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();
    }
    zipinputstream.close();
}