package byteOperate;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
public class FileOutputStreamDemo {
/**
* 文件的写入
1.建立联系 File对象
2.选择流 输出流 OutputStream(抽象父类) + FileOutputStream
3.操作 write(byte[]) + flush():刷新强制写出数据
4.释放资源:关闭输出流
* @param NicolasKs
*/
public static void main(String[] args) {
File file=new File("E:/iotest.txt");
OutputStream outputStream=null;
try {
outputStream= new FileOutputStream(file,true);
String content="\n红红好姑娘";
byte cache[]=content.getBytes();
outputStream.write(cache);
outputStream.flush();
} catch (IOException e) {
System.out.println("文件写入异常");
e.printStackTrace();
}
finally{
try {
if(outputStream!=null)
outputStream.close();
System.out.println("文件写入成功");
} catch (IOException e) {
System.out.println("输出流关闭异常");
e.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
public class FileOutputStreamDemo {
/**
* 文件的写入
1.建立联系 File对象
2.选择流 输出流 OutputStream(抽象父类) + FileOutputStream
3.操作 write(byte[]) + flush():刷新强制写出数据
4.释放资源:关闭输出流
* @param NicolasKs
*/
public static void main(String[] args) {
File file=new File("E:/iotest.txt");
OutputStream outputStream=null;
try {
outputStream= new FileOutputStream(file,true);
String content="\n红红好姑娘";
byte cache[]=content.getBytes();
outputStream.write(cache);
outputStream.flush();
} catch (IOException e) {
System.out.println("文件写入异常");
e.printStackTrace();
}
finally{
try {
if(outputStream!=null)
outputStream.close();
System.out.println("文件写入成功");
} catch (IOException e) {
System.out.println("输出流关闭异常");
e.printStackTrace();
}
}
}
}