用于文件下载

    1. import java.io.File;
    2. import java.io.FileOutputStream;
    3. import java.io.IOException;
    4. public class FileOutPutStreamStudy {
    5. public static void main(String[] args) {
    6. // TODO Auto-generated method stub
    7. // firstWriteMethod();
    8. secondWriteMethod();
    9. }
    10. /**
    11. * 一批一批的写
    12. */
    13. private static void secondWriteMethod() {
    14. // TODO Auto-generated method stub
    15. String path = "D:\\movie\\japan\\dj\\xzm.txt";
    16. File file = new File(path);
    17. //定义输出流
    18. FileOutputStream fos = null;
    19. try {
    20. //存不存在
    21. if(!file.exists()) {
    22. file.createNewFile();
    23. }
    24. //实例化输出流
    25. fos = new FileOutputStream(file);
    26. String str = "你老了,可以退休,因为没人看了!!!!!";
    27. byte[] data = str.getBytes("UTF-8");
    28. //向文件中一次型写入内容
    29. fos.write(data);
    30. } catch (Exception e) {
    31. // TODO: handle exception
    32. e.printStackTrace();
    33. }finally {
    34. try {
    35. fos.close();//关闭流
    36. } catch (IOException e) {
    37. // TODO Auto-generated catch block
    38. e.printStackTrace();
    39. }
    40. }
    41. }
    42. /**
    43. * 一个字节一个字节的写
    44. */
    45. private static void firstWriteMethod() {
    46. // TODO Auto-generated method stub
    47. String path = "D:\\movie\\japan\\dj\\bdy.txt";
    48. File file = new File(path);
    49. //定义输出流
    50. FileOutputStream fos = null;
    51. try {
    52. //存不存在
    53. if(!file.exists()) {
    54. file.createNewFile();
    55. }
    56. //实例化输出流
    57. fos = new FileOutputStream(file);
    58. String str = "这是今年最好的一部电影!!!";
    59. byte[] data = str.getBytes("UTF-8");
    60. for (byte c : data) {
    61. //操作流
    62. fos.write(c);
    63. }
    64. } catch (Exception e) {
    65. // TODO: handle exception
    66. e.printStackTrace();
    67. }finally {
    68. try {
    69. fos.close();//关闭流
    70. } catch (IOException e) {
    71. // TODO Auto-generated catch block
    72. e.printStackTrace();
    73. }
    74. }
    75. }
    76. }