A: 构造方法
FileOutputStream(File file)
FileOutputStream(String name)
B:案例演示
FileOutputStream写出数据
public class MyTest {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("a.txt");
FileOutputStream out = new FileOutputStream(file);
FileOutputStream out2 = new FileOutputStream("b.txt");
}
}
注意事项:
创建字节输出流对象了做了几件事情?
a:调用系统资源创建a.txt文件
b:创建了一个fos对象
c:把fos对象指向这个文件
为什么一定要close()?
a: 通知系统释放关于管理a.txt文件的资源
b: 让Io流对象变成垃圾,等待垃圾回收器对其回收
IO流(FileOutputStream的三个write()方法)
A:FileOutputStream的三个write()方法
public void write(int b):写一个字节 超过一个字节 砍掉前面的字节
public void write(byte[] b):写一个字节数组
public void write(byte[] b,int off,int len):写一个字节数组的一部分
B:案例演示: FileOutputStream的三个write()方法
public class MyTest {
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("a.txt");
out.write(99);
out.write(100);
out.write(120);
out.write(105);
String str="我爱你中国";
byte[] bytes = str.getBytes();
out.write(bytes);
String str2="中国加油,武汉加油";
byte[] bytes1 = str2.getBytes();
out.write(bytes1,0,18);
out.close();
}
}
IO流(FileOutputStream写出数据实现换行和追加写入)
A:案例演示: FileOutputStream写出数据如何实现数据的换行
windows下的换行符只用是 \r\n
Linux \n
Mac \r
public class MyTest {
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("b.txt");
out.write("沅水通波接武冈,".getBytes());
out.write("\r\n".getBytes());
out.write("送君不觉有离伤。".getBytes());
out.write("\r\n".getBytes());
out.write("青山一道同云雨,".getBytes());
out.write("\r\n".getBytes());
out.write("明月何曾是两乡。".getBytes());
out.close();
}
}
B:案例演示: FileOutputStream写出数据如何实现数据的追加写入
public class MyTest {
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("c.txt",true);
out.write("沅水通波接武岗,".getBytes());
out.write("\r\n".getBytes());
out.write("送君不觉有离伤。".getBytes());
out.write("\r\n".getBytes());
out.write("青山一道同云雨,".getBytes());
out.write("\r\n".getBytes());
out.write("明月何曾是两乡。".getBytes());
out.write("\r\n".getBytes());
out.close();
}
}
IO流(FileOutputStream写出数据加入异常处理)
A:案例演示:FileOutputStream写出数据加入异常处理
public class MyTest {
public static void main(String[] args) {
FileOutputStream out = null;
try {
out = new FileOutputStream("c.txt");
out.write("你好".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(out!=null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
IO流(FileInputStream读取数据一次一个字节)
A:案例演示: int read():一次读取一个字节
如果没有数据返回的就是-1
public class MyTest {
public static void main(String[] args) throws IOException {
FileInputStream in= new FileInputStream("c.txt");
int by = in.read();
System.out.println(by);
by = in.read();
System.out.println(by);
by = in.read();
System.out.println(by);
by = in.read();
System.out.println(by);
by = in.read();
System.out.println(by);
by = in.read();
System.out.println(by);
by = in.read();
System.out.println(by);
in.close();
}
}
IO流(字节流复制文本文件)
A:案例演示: 字节流一次读写一个字节复制文本文件
分析:
- a: 创建字节输入流对象和字节输出流对象
- b: 频繁的读写操作
- c: 释放资源
public class MyTest {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("MyTest.java");
FileOutputStream out = new FileOutputStream("C:\\Users\\ShenMouMou\\Desktop\\MyTestCopy.java");
int by=0;
while ((by=in.read())!=-1){
out.write(by);
}
in.close();
out.close();
System.out.println("复制完成");
}
}
IO流(字节流复制MP3)
A:案例演示: 字节流一次读写一个字节复制MP3
public class MyTest {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("C:\Users\HP\Music\You.mp3");
FileOutputStream out = new FileOutputStream("C:C:\Users\HP\Desktop\You.mp3");
long start = System.currentTimeMillis();
int by=0;
while ((by=in.read())!=-1){
out.write(by);
out.flush();
}
in.close();
out.close();
long end = System.currentTimeMillis();
System.out.println("复制完成耗时"+(end-start)+"毫秒");
}
}
IO流(FileInputStream读取数据一次一个字节数组)
A:案例演示: int read(byte[] b):一次读取一个字节数组
返回的int类型的值表示的意思是读取到的字节的个数,如果没有数据了就返回-1
public class MyTest {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("C:\Users\HP\Music\You.mp3");
FileOutputStream out = new FileOutputStream("C:C:\Users\HP\Desktop\You.mp3");
int len=0;
byte[] bytes = new byte[1024*8];
long start = System.currentTimeMillis();
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
in.close();
out.close();
long end = System.currentTimeMillis();
System.out.println("复制完成耗时" + (end - start) + "毫秒");
}
}
IO流(字节流复制文本文件2)
A:案例演示: 字节流一次读写一个字节数组复制文本文件
public class MyTest2 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("D:\\搜狗高速下载\\DREAM.mp4");
FileOutputStream out = new FileOutputStream("C:\Users\HP\Desktop\\视频.mp4");
int len=0;
byte[] bytes = new byte[1024 * 8];
long start = System.currentTimeMillis();
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
in.close();
out.close();
long end = System.currentTimeMillis();
System.out.println("复制完成耗时" + (end - start) + "毫秒");
}
}