1,字节流,(1)读取纯文本文件优先使用字符流;(2)图片、音频优先使用字节流;(3)不确定是啥,就使用字节流。
字节就是byte,字符就是“A”,“@”这种,不同的编码方式占用不同的字节。
2,FIleOutputStreat(String s),可以用来写入数据。在IO操作时可以使用.write(。。。)写入数据;在IO操作之后要使用.close()方法来释放系统资源。
3,字节流写数据实现换行,在写完数据之后再写一句换行符号,window是“\r\n”,linux是“\n”,mac是“\r”,
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("G:\\JAVA\\love.txt");
for(int i=0;i<10;i++){
fos.write("hello".getBytes());
fos.write("\r\n".getBytes());
}
fos.close();
}
}
3,字节流写入数据追加写入,在创建对象时使用构造方法,
FileOutputStream fos=new FileOutputStream("G:\\JAVA\\love.txt",true);
就是以后写文件时在末尾追加写入。
4,字节流写数据的异常处理,
(1)
try{
// IO流
// 执行语句
}catch (/*异常类 变量名,比如:IOException ioException*/ ){
// catch的执行语句,异常处理代码
}finally {
// 执行所有清除操作
}
(2),通过alt+enter选择使用throws抛出异常。
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("G:\\JAVA\\love.txt",true);
for(int i=0;i<10;i++){
fos.write("love".getBytes());
fos.write("\r\n".getBytes());
}
fos.close();
}