要求:对文件操作的字节流和字符流(复制一个图片从C盘到D盘)
字节流方法
字节输入流(读)
InputStream
read()
read(byte[])
read(byte[],off,len)
close()
FileInputStream
new FileInputStream(File file)
new FileInputStream(String path)
字节输出流(写)
OutputStream
write(int)
write(byte[])
write(byte[],off,len)
close()
flush():强制将缓冲区清空
FileOutputStream
new FileOutputStream(File file)
new FileOutputStream(String path)
new FileOutputStream(String path,boolean append):可以指定覆盖或追加文件内容
InputStream:字节输入流(抽象类)–》FileInputStream
int read():
从输入流一个字节一个字节的读,返回的是该字节的整数表示形式,如果到了输入流的末尾,返回-1
int read(byte[] b):
从输入流读取若干字节,把这些字节保存到数组b中,返回的是读取到的字节数,如果到了输入流的末尾,返回-1
int read(byte[] b,int off ,int len):
从输入流读取若干字节,把这些字节保存到数组b中,off指的是字节数组中开始保存数据的起始下标,len指读取的字节数目。返回的是读取到的字节数,如果到了输入流的末尾,返回-1
package cn.kgc.tangcco.chengxiangjian.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//字节流方法
public class Test2 {
public static void main(String[] args) {
// 创建源文件和目标文件
File file1 = new File("C:\\dog.jpg");
File file2 = new File("D:\\dog.jpg");
// 创建字节输入流,字节输出流
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
// 依次 打开字节输入流,字节输出流
inputStream = new FileInputStream(file1);
outputStream = new FileOutputStream(file2);
/*创建字节数组,inputStream.available()读取文件流长度不完整,
这个方法从本地文件读取数据时一般不会出现问题,但是通过网路传输就会出现图片传输不完整的情况,
因为网络通讯是间断性的一串字节往往分几批进行发送。本地程序调用available()方法有时得到0,
这可能是对方还没有响应,也可能是对方已经响应了,但是数据还没有送达本地。
*/
byte[] bts = new byte[(int)file1.length()];
// 读取和写入信息(边读取边写入)
//方法1:
// inputStream.read(bts);
// inputStream.close();
// outputStream.write(bts);
// outputStream.close();
//方法2:
while (inputStream.read(bts) != -1) {
outputStream.write(bts);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 依次关闭流(先开后关,后开先关)(如采用方法1,此处应省略)
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符流方法(注意写入的数据会有丢失)
字符输入流(读)
Reader
read()
read(char[])
read(char[],off,len)
close()
InputStreamReader:可以指定字符编码格式
new InputStreamReader(InputStream)
new InputStreamReader(InputStream,String charSetName)
FileReader
new FileReader(File file)
new FileReader(String path)
常见问题:中文乱码
原因:文件编码格式 和 程序环境的编码格式不一致
解决方案:
字符流去读的时候,指定字符流的编码格式
FileReader 无法指定编码格式,会按照文件系统默认编码格式读
System.out.println(System.getProperty(“file.encoding”));
所以使用InputStreamReader
缓冲流BufferedReader
readLine()
字符输出流(写)
Writer
package cn.kgc.tangcco.chengxiangjian.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
//字符流方法,写入的数据会有丢失
public class Test1 {
public static void main(String[] args) {
// 创建源文件和目标文件
File file1 = new File("C:\\dog.jpg");
File file2 = new File("D:\\dog.jpg");
// 创建字符输入流,字符输出流
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
// 依次打开字符输入流,字符输出流
fileReader = new FileReader(file1);
fileWriter = new FileWriter(file2);
// 创建字符数组
char[] chs = new char[(int)file1.length()];
// 读取和写入信息(边读取边写入)
//方法1:
// fileReader.read(chs);
// fileReader.close();
// fileWriter.write(chs);
// fileWriter.close();
//方法2:
while (fileReader.read(chs) != -1) {
fileWriter.write(chs);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 依次 关闭流(先开后关,后开先关)(如采用方法1,此处应省略)
fileWriter.close();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}