zoukankan      html  css  js  c++  java
  • FileOutputStream

    OutputStream:

        FileOutputStream

        BufferedOutputStream  缓冲输出流

     1 package file;
     2 
     3 import java.io.File;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 public class Demo2 {
     8     public static void main(String[] args) throws IOException {
     9 //        writeTest1();
    10 //        writeTest2();
    11         writeTest3();
    12     }
    13     
    14 
    15     //使用字节数组把数据写出
    16     public static void writeTest3() throws IOException   {
    17         File file = new File("F:/2.txt");
    18         FileOutputStream fileOutputStream = new  FileOutputStream(file);
    19         //数据写出
    20         String data = "abcd";
    21         byte[] buf = data.getBytes();
    22         fileOutputStream.write(buf, 0, 2);    //写出2个字节
    23         fileOutputStream.close();
    24     }
    25     
    26     //使用字节数组把数据写出
    27     public static void writeTest2() throws IOException   {
    28         File file = new File("F:/2.txt");
    29         //使用FileOutputStream(File),如果文件不存在,会自动创建目标文件。如果存在,则会先把目标文件的内容清空,再写内容
    30         //使用FileOutputStream(File,true)构造函数,则会在末尾追加
    31         FileOutputStream fileOutpubStream = new FileOutputStream(file);
    32         //数据写出
    33         String data = "hello world";
    34         fileOutpubStream.write(data.getBytes());
    35         //关闭资源         
    36         fileOutpubStream.close();
    37     }
    38     
    39     //每次只能写一个字节数据
    40     public static void writeTest1() throws IOException   {
    41         File file = new File("F:\2.txt");
    42         FileOutputStream fileOutpubStream = new FileOutputStream(file);
    43         fileOutpubStream.write('h');
    44         fileOutpubStream.write('e');
    45         fileOutpubStream.write('l');
    46         fileOutpubStream.write('l');
    47         fileOutpubStream.write('o');
    48         
    49         fileOutpubStream.close();
    50     }
    51 }
  • 相关阅读:
    Root of AVL Tree
    04-树4 是否同一棵二叉搜索树
    03-树3 Tree Traversals Again
    03-树2 List Leaves
    283. Move Zeroes
    506. Relative Ranks
    492. Construct the Rectangle
    476. Number Complement
    461. Hamming Distance
    389. Find the Difference
  • 原文地址:https://www.cnblogs.com/linst/p/5660333.html
Copyright © 2011-2022 走看看