为什么学习io?
为了实现java程序与本地磁盘,键盘,网络等之间的交互
IO的概念
package com.lu.day10.io;
public class IoTest {
public static void main(String[] args) {
/*
文本分为:
纯文本文件:txt,java->只有字符 ->字符流
富文本文件:excel,word->不仅有文字字符,图片,声音,视屏
*/
}
}
流的使用步骤
字节流
1.字节输入流基本使用
package com.lu.day10.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class IoTest {
public static void main(String[] args) {
/*
文本分为:
纯文本文件:txt,java->只有字符 ->字符流
富文本文件:excel,word->不仅有文字字符,图片,声音,视屏
*/
try {
FileInputStream in = new FileInputStream("src/com/lu/day09/Computer.java");
byte[] bytes = in.readAllBytes();
// 字节数组转字符串
String s = new String(bytes);
System.out.println(s);
//关闭流
in.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package com.lu.day10.io;
import java.io.FileInputStream;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) {
//自动关闭流资源的try方法 jdk1.7引入
try (FileInputStream in = new FileInputStream("resource/a.txt")){
byte[] bytes = new byte[1024];//为什么是1024因为操作系统默认存储的最小单位是1bk
//read变量的作用:-1:终止读取,不是-1代表正常每次读取的有效长度
int read = 0;
while ((read = in.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, read));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
2.字节输出流基本使用
package com.lu.day10.io;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test3 {
public static void main(String[] args) {
String s = "Hello Word!";
//如果没有文件会自动创建一个文件
//默认创建的没有开启续写,第二个参数指定true就可以。
try(FileOutputStream out = new FileOutputStream("resource/c.txt",true)) {
// 将字符串转换为字节数组
// 写入字节
out.write(s.getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
3.字节输入输出流实现文件的复制
package com.lu.day10.io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileStreamUtil {
private FileStreamUtil() {
}
/**
* 复制文件
* @param srcFile
* @param destFile
*/
public static void copyFile(String srcFile, String destFile) {
try (FileInputStream in = new FileInputStream(srcFile);FileOutputStream out = new FileOutputStream(destFile)) {
byte[] bytes = new byte[1024];
int read;
while ((read = in.read(bytes))!= -1) {
out.write(bytes,0,read);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
4.字节输入缓冲流
package com.lu.day10.io;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 输入缓冲流加快字节读取速度
*/
public class Test5 {
public static void main(String[] args) {
long l = System.currentTimeMillis();
try (FileInputStream in = new FileInputStream("resource/1.作业讲解.mp4")
;BufferedInputStream buffered = new BufferedInputStream(in)) {
int read;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1){
}
System.out.println(System.currentTimeMillis()-l);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
5.字节输出缓冲流
package com.lu.day10.io;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test6 {
public static void main(String[] args) {//true开启续写
try(FileOutputStream out = new FileOutputStream("resource/a.txt",true);
BufferedOutputStream buffered = new BufferedOutputStream(out)){
buffered.write("hello".getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
6.使用缓冲流实现文件的复制
package com.lu.day10.test;
import java.io.*;
public class Test1 {
public static void main(String[] args) {
copyFile("resource/2.jpg", "resource/2_copy.jpg");
}
public static void copyFile(String srcFile, String destFile) {
try(BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destFile))){
byte[] bytes = new byte[1024];
int read;
while ((read = in.read(bytes))!= -1) {
out.write(bytes,0,read);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}