Created
January 19, 2016 18:14
-
-
Save andrewZee/b5162610bc09b73c0229 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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