Java自学笔记——IO流
分类
- 文件流
流:数据在数据源(文件)和程序(内存)之间经历的路径
输入输出流(以程序为主体
文件与IO流的区别
分类
字节流 8bit
InputStream
OutputStream
字节文件如视频图片等
字符流 按字符Reader
Writer
文本文件
都是抽象类,不能实例化
结点流 处理流(包装流)
##FileInputStream
package com.hz.file.FileInputStream;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author Lspero
* @version 1.0
* 字节输入流,文件->程序
*
public class FileInputStream_ {
public static void main(String[] args) {
}
/*
read()单个字节读取,效率低
*/
@Test
public void readFile01(){
String filePath = "c:\\D\\hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
//创建FileInputStream文件,用于读取文件
fileInputStream = new FileInputStream(filePath);
//如果返回-1,就到达文件尾
while ((readData = fileInputStream.read()) != -1){
System.out.print((char)readData);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流,释放文件
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//read(Byte[] b
@Test
public void readFile02(){
String filePath = "c:\\D\\hello.txt";
int readLen = 0;
byte[] buf = new byte[8];//一次读8个字节
FileInputStream fileInputStream = null;
try {
//创建FileInputStream文件,用于读取文件
fileInputStream = new FileInputStream(filePath);
//如果读取正常,返回实际读取的字节数,如果返回-1,就到达文件尾
while ((readLen = fileInputStream.read(buf)) != -1){
System.out.print(new String(buf, 0, readLen));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流,释放文件
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream
package com.hz.file;
import org.junit.Test;
import java.io.*;
import java.io.FileOutputStream;
/**
* @author Lspero
* @version 1.0
*/
public class FileOutputStream01 {
public static void main(String[] args) {
}
/*
使用FileOutputStream将数据写到文件中
如果不存在,就创建
*/
@Test
public void writeFile(){
String filePath = "c:\\D\\a.txt";
FileOutputStream fileOutputStream = null;
try {
// new FileOutputStream(filePath)创建方式,写入内容时是覆盖
//new FileOutputStream(filePath,true)追加到后面
fileOutputStream = new FileOutputStream(filePath,true);
//写入一个字节
fileOutputStream.write('H');
//写入字符串
String s = "Hello world!";
//getBytes()字符串转为字节数组
fileOutputStream.write(s.getBytes());
//从off开始写len个
fileOutputStream.write(s.getBytes(),0,2);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
拷贝的完成
package com.hz.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Lspero
* @version 1.0
*/
public class FileCopy {
public static void main(String[] args) {
/*完成文件拷贝a.txt拷贝到A目录下
1.创建输入流,读入程序
2.创建输出流,写入磁盘
执行行,读取部分数据就写入
*/
String srcFilePath = "c:\\D\\a.txt";
String destFilePath = "c:\\D\\A\\a.txt";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(destFilePath);
//定义数组
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(buf)) != -1){
fileOutputStream.write(buf,0,readLen);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if(fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}