用于文件下载
import java.io.File;import java.io.FileOutputStream;import java.io.IOException;public class FileOutPutStreamStudy {public static void main(String[] args) {// TODO Auto-generated method stub// firstWriteMethod();secondWriteMethod();}/*** 一批一批的写*/private static void secondWriteMethod() {// TODO Auto-generated method stubString path = "D:\\movie\\japan\\dj\\xzm.txt";File file = new File(path);//定义输出流FileOutputStream fos = null;try {//存不存在if(!file.exists()) {file.createNewFile();}//实例化输出流fos = new FileOutputStream(file);String str = "你老了,可以退休,因为没人看了!!!!!";byte[] data = str.getBytes("UTF-8");//向文件中一次型写入内容fos.write(data);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally {try {fos.close();//关闭流} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/*** 一个字节一个字节的写*/private static void firstWriteMethod() {// TODO Auto-generated method stubString path = "D:\\movie\\japan\\dj\\bdy.txt";File file = new File(path);//定义输出流FileOutputStream fos = null;try {//存不存在if(!file.exists()) {file.createNewFile();}//实例化输出流fos = new FileOutputStream(file);String str = "这是今年最好的一部电影!!!";byte[] data = str.getBytes("UTF-8");for (byte c : data) {//操作流fos.write(c);}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally {try {fos.close();//关闭流} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
