IO流基础使用,留着复习
8位1字节,2个字节1个字符
字节流:主要有InputStream、OutputStream作为基类
字符流:主要有Reader、Writer作为基类
输入流
InputStream:
int read();
int read(byte[] b);
int read(byte[] b,int off,int len);
Reader:
int read();
int read(char[] cbuf);
int read(char[] cbuf,int off,int len);
输出流
OutputStream:
void write(int c);
void write(byte[]/char[] buf);
void write(byte[]/char[] buf,int off,int len);
Write:
void write(String str);
void write(String str,int off,int len);
转换流
InputStreamReader:将字节输入流转换成字符输入流
OutputStreamReader:将字节输出流转换成字符输出流
缓冲流
字节缓冲流
BufferedInputStream
BufferedOutputStream
字符缓冲流
BufferedReader
BufferedWriter
基本使用:
public class FileDemo {
public static void main(String[] args) throws IOException {
byteStream();
charStream();
streamReader();
inputBuffer();
streamReaderBuffer();
}
/**
* 字节流
* @throws IOException
*/
public static void byteStream() throws IOException {
//字节流复制文件
/*InputStream fis = new FileInputStream("F:/xuehao.jpg");
OutputStream fos = new FileOutputStream("F:/shawanyi.jpg");;
int read = 0;
while(read != -1) {
read = fis.read();
fos.write(read);
}
fis.close();
fos.close();*/
InputStream fis = new FileInputStream("F:/xuehao.jpg");
OutputStream fos = new FileOutputStream("F:/byteStream.jpg");;
byte[] bytes = new byte[1024 * 4];
int read;
while((read = fis.read(bytes)) != -1) {
fos.write(bytes,0,read);
}
fis.close();
fos.close();
}
/**
* 字符流 字符流不要复制图片,会出现图片写入错误
* @throws IOException
*/
public static void charStream() throws IOException {
// File f = new File("F:/demo.txt");
// Writer out=new FileWriter(f);
// out.write("demo text~~~~~");
// out.close();
//字符流复制文本文件,不要复制图片
File f = new File("F:/demo.txt");
File outFile = new File("F:/charStream.txt");
Writer out = new FileWriter(outFile);
Reader reader=new FileReader(f);
char[] c = new char[1024*4];
int len;
while((len = reader.read(c)) != -1) {
out.write(c,0,len);
}
reader.close();
out.close();
}
/**
* 字节流字符流之间转换
* @throws IOException
*/
public static void streamReader() throws IOException {
File file = new File("F:/demo.txt");
FileInputStream fis = new FileInputStream(file);//字节流
InputStreamReader isr = new InputStreamReader(fis);//转换流
Writer writer = new FileWriter("F:/streamReader.txt");
char[] c = new char[1024 * 4];
int len;
while((len = isr.read(c)) != -1) {
writer.write(c,0,len);
}
writer.close();
isr.close();
fis.close();
}
/**
* 字节流用缓冲流形式输出
* @throws IOException
*/
public static void inputBuffer() throws IOException {
File file = new File("F:/xuehao.jpg");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
File fileout = new File("F:/inputBuffer.jpg");
FileOutputStream fos = new FileOutputStream(fileout);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len;
byte[] b = new byte[1024 * 2];
while((len = bis.read(b)) != -1) {
bos.write(b,0,len);
}
bos.flush();
bos.close();
fos.close();
bis.close();
fis.close();
}
/**
* 字节流转换字符流,用缓冲流形式输出
* @throws IOException
*/
public static void streamReaderBuffer() throws IOException {
File file = new File("F:/demo.txt");
FileInputStream fis = new FileInputStream(file);//字节流
InputStreamReader isr = new InputStreamReader(fis);//转换流
BufferedReader bufferedReader = new BufferedReader(isr);
Writer writer = new FileWriter("F://streamReaderBuffer.txt");
BufferedWriter bufferedWriter = new BufferedWriter(writer);
char[] c = new char[1024 * 4];
int len ;
while((len = bufferedReader.read(c)) != -1) {
bufferedWriter.write(c,0,len);
}
//关流的时候 先开后关
bufferedWriter.flush();
bufferedWriter.close();
writer.close();
bufferedReader.close();
isr.close();
fis.close();
}
}