使用IO流 都会有下面的套路:
//1.File类的实例化
//2.流的实例化
//3.读入的操作
//4.关闭流
节点流有
FileInputStream FileOutputStream FileReader FileWriter
节点流抛异常为什么用try-catch-finally 而不用 thows呢
怕file读文件时候突然出现阻塞 文件读不下去 然后就抛出IOexception的异常 文件就不往下执行了 然后 流没有关闭 造成资源浪费 存在泄漏的问题
说明点:
1. read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
2.读入的文件一定要存在,否则就会报FiLeNotFoundException.
3.异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
节点流抛异常为什么用try-catch-finally 而不用 thows呢
怕file读文件时候突然出现阻塞 文件读不下去 然后就抛出IOexception的异常 文件就不往下执行了 然后 流没有关闭 造成资源浪费 存在泄漏的问题
从内存中写出数据到文件硬盘里
说明
* 1输出操作,对应的File可以不存在 并不会报异常
* 2 File对应的磁盘中如果文件不存在,在输出的过程中,会自动创建此文件
* File对应的磁盘中如果文件存在 则
* 1.如果构造器是FileWriter(file,flase)或FileWriter(file)则自动覆盖原来的
2.如果构造器是FileWriter(file,ture)则在原来基础上添加新内容
*测试FileInputStream和Fileoutpustream的使用
- 结论:
- 1.对于文本文件(. txt, .java,.c,.cpp), 使用字符流理
- 2.对于非文本文件(. jpg, . mp3, . mp4,.avi, . doc, .ppt…), 使用字节流处理
public class FileReaderWriteTest {
//流的使用
@Test
public void testFileReader() throws IOException{
//1.首先实例化file对象
File file = new File("src\\hello.txt");
//2.提供具体的流
FileReader fr = new FileReader(file);
//3.数据的读入
int data = fr.read();
while (data != -1) {
System.out.print((char)data);
data = fr.read();
String a = new String();
}//这个while循环可以相当于 while((data = fr.read !=-1)) 这是一个语法上的优化
//4.关闭流
fr.close();
}
@Test
public void Test1() {
File file = new File("D:\\eclipse2\\eclipse\\IO流\\src\\hello.txt");
System.out.println(file.getAbsolutePath());
}
@Test
//read的升级操作 read其他重载的方法
public void Test2() {
//1.File类的实例化
//2.流的实例化
FileReader fr = null;
try {
File file = new File("src//hello.txt");
fr = new FileReader(file);
//3.读入的操作
//read(char[] cubf):返回每次读入的cbuf数组中字符的个数 如果达到文件末尾 输出-1
char[] cbuf = new char[5];
int len ;
while((len = fr.read(cbuf)) != -1) {
for(int i = 0; i < len; i++ ) {
System.out.print(cbuf[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if(fr !=null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
//实现文件文本的复制
public void Test3() {
//创建FileReader的实例化对象
FileReader Fr = null;
//创建FileWrite的实例化对象
FileWriter Fw = null;
try {
//创建Flie类的对象
File file = new File("src//hello.txt");
File file1 = new File("src//hello1.txt");
Fr = new FileReader(file);
Fw = new FileWriter(file1);
//读取流
char[] cbuf = new char[5];
int len;//记录每次读到cbuf数组中的个数
while((len = Fr.read(cbuf)) != -1){
Fw.write(cbuf, 0, len);
}
}catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流
if(Fr != null) {
try {
Fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(Fw != null) {
try {
Fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}}
/*写一个copy方法 实现图片文件的复制
*/
public class FileInputOutputStream {
@Test
public void Test1() {
long start = System.currentTimeMillis();
String srcpath = "D:\\eclipse2\\eclipse\\IO流\\src\\1.jpg";
String destPath = "D:\\eclipse2\\eclipse\\IO流\\src\\2.jpg";
copyFile(srcpath, destPath);
long end =System.currentTimeMillis();
System.out.println("用的时间为:"+(end - start));
}
public void copyFile(String srcpath , String destPath) {
//1
File f1 ;
File f2 ;
//2
FileInputStream fis = null;
FileOutputStream fos = null;
try {
f1 = new File(srcpath);
f2 = new File(destPath);
fis = new FileInputStream(f1);
fos = new FileOutputStream(f2);
//3
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1) {
fos.write(buffer,0,len); }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos !=null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
}}