IO/OutputStream

来源:互联网 发布:海量数据排序 编辑:程序博客网 时间:2024/06/06 00:49

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,则将字节写入文件末尾处,而不是写入文件开始处,不会覆盖原来数据