Skip to content

Instantly share code, notes, and snippets.

@andrewZee
Created January 19, 2016 18:14
Show Gist options
  • Save andrewZee/b5162610bc09b73c0229 to your computer and use it in GitHub Desktop.
Save andrewZee/b5162610bc09b73c0229 to your computer and use it in GitHub Desktop.
package Stream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileDemo {
public static void main(String[] args) throws IOException {
File file = new File("test.txt");
copyFile(file);
}
public static void copyFile(File f) throws IOException {
File newFile = new File("test_copy.txt");
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(f);
output = new FileOutputStream(newFile);
byte[] buf = new byte[256];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment