版权声明:转载请注明出处 https://blog.csdn.net/doubleguy/article/details/87026951
同输入流,我还是用一个案例来体现输出的基本用法。
功能:用io流将数据写入指定路径的文件当中
代码如下:
package com.test9;
import java.io.*;
public class OutputStreamTest {
public static void main(String [] args){
File f = new File("D:\\z.txt");
if(f.exists()){
System.out.println("it has existed");
}else{
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
String s = "你好,Hello World!\r\n";
String s1 = "wo是你。";
try {
fos.write(s.getBytes());
fos.write(s1.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
//关闭输出流
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}