IO/OutputStream

1.import java.io.FileOutputStream;
import java.io.IOException;

public class IoTest2 {
public static void main(String[] args) throws IOException {
String path = “c:\a.txt”;
writeTxtFile(path);
}

private static void writeTxtFile(String path) throws IOException {
    // 1:打開文件輸出流,流的目的地是指定的文件
    FileOutputStream fos = new FileOutputStream(path);

    // 2:通過流向文件寫數據
    fos.write('j');
    fos.write('a');
    fos.write('v');
    fos.write('a');
    // 3:用完流後關閉流
    fos.close();

//將c盤下的a.txt文件刪除,發現當文件不存在時,會自動創建一個,但是創建不了多級目錄
}
}
2.public class IoTest2 {
public static void main(String[] args) throws IOException {
String path = “c:\a.txt”;
writeTxtFile(path);
}

private static void writeTxtFile(String path) throws IOException {
    // 1:打開文件輸出流,流的目的地是指定的文件
    FileOutputStream fos = new FileOutputStream(path);

    // 2:通過流向文件寫數據
    byte[] byt = "java".getBytes();
    fos.write(byt);
    // 3:用完流後關閉流
    fos.close();
}

}
ileOutputStream(File file, boolean append) 第二個參數,append - 如果爲 true,則將字節寫入文件末尾處,而不是寫入文件開始處,不會覆蓋原來數據

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章