-I/O流的一般操作流程:-创建或者选择数据源-选择I/O流-创建操作对象-创建读取or存储的缓存区-具体操作,写入,读取,转换等等-**出现乱码原因**:字节转字符,**字节数不够**,导致乱码;**编码不一致**导致乱码-注意事项-`FileInput/OutputStream`最适合用于操作非文本文件(如图片、视频等)-`FileReader/Writer`最适合用于操作文本文件(如txt文件)-`BufferStream`可以加速节点流操作文件的速度```*抽象基类节点流(文件流)缓冲流字节流*InputStreamFileInputStreamBufferedInputStream*OutputStreamFileOutputStreamBufferedOutputStream字符流*ReaderFileReaderBufferedReader*WriterFileWriterBufferedWriter```-示例:文件<----->字节流:`BufferedInputStream(newFileInputStream(src))`----->`ByteArrayOutputStream`; `ByteArrayInputStream`---->`newBufferedOutputStream(newFileOutputStream(dest)`-示例:```javapublicstaticbyte[] fileToByteArray(StringfilePath){
Filefile=newFile(filePath);
InputStreamis=null;
ByteArrayOutputStreamos=null;
try {
is=newFileInputStream(file);
os=newByteArrayOutputStream();
byte[] flush=newbyte[1024*10];
intlen=-1;
while ((len=is.read(flush))!=-1){
Stringstr=newString(flush, 0, len);
os.write(str.getBytes());
}
os.flush();
returnos.toByteArray();
} catch (FileNotFoundExceptione) {
e.printStackTrace();
} catch (IOExceptione) {
e.printStackTrace();
} finally {
try {
if (null!=os){
is.close();
}
} catch (IOExceptione) {
e.printStackTrace();
}
try {
if (null!=is){
is.close();
}
} catch (IOExceptione) {
e.printStackTrace();
}
}
returnnull;
}
``````javapublicstaticbooleanbyteArrayToFile(byte[] bytesArray, StringfilePath){
Filefile=newFile(filePath);
ByteArrayInputStreamis=null;
FileOutputStreamos=null;
try {
is=newByteArrayInputStream(bytesArray);
os=newFileOutputStream(file);
byte[] flush=newbyte[1024*10];
intlen=-1;
while ((len=is.read(flush))!=-1){
os.write(flush, 0, len);
}
os.flush();
returntrue;
} catch (FileNotFoundExceptione) {
e.printStackTrace();
} catch (IOExceptione) {
e.printStackTrace();
} finally {
try {
if (null!=os){
is.close();
}
} catch (IOExceptione) {
e.printStackTrace();
}
try {
if (null!=is){
is.close();
}
} catch (IOExceptione) {
e.printStackTrace();
}
}
returnfalse;
}
``````javapublicstaticbooleanbyteArrayToFile(byte[] bytesArray, StringfilePath){
Filefile=newFile(filePath);
try (ByteArrayInputStreamis=newByteArrayInputStream(bytesArray); OutputStreamos=newBufferedOutputStream(newFileOutputStream(file))){
byte[] flush=newbyte[1024*10];
intlen=-1;
while ((len=is.read(flush))!=-1){
os.write(flush, 0, len);
}
os.flush();
returntrue;
} catch (FileNotFoundExceptione) {
e.printStackTrace();
} catch (IOExceptione) {
e.printStackTrace();
}
returnfalse;
}
```-字节流,字符流转换;页面源代码读取```javaimportjava.io.*;
importjava.net.URL;
publicclassConvertTest {
publicstaticvoidmain(String[] args) {
try (
BufferedReaderisr=newBufferedReader(newInputStreamReader(System.in));
BufferedWriterosw=newBufferedWriter(newOutputStreamWriter(System.out));
BufferedReaderis=newBufferedReader(newInputStreamReader(newURL("http://www.baidu.com").openStream(), "UTF-8"));
BufferedWriterbw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream("baidu.html"), "UTF-8"));
) {
inttemp;
Stringtemp2;
Stringtemp1;
while ((temp=is.read())!=-1){
System.out.print((char) temp);
bw.write(temp);
}
while ((temp1=is.readLine())!=null){
bw.write(temp1);
bw.newLine();
}
bw.flush();
while (!"exit".equals(temp2=isr.readLine())){
osw.write(temp2);
osw.newLine();
osw.flush();
} } catch (IOExceptione) {
System.out.println(e.toString());
}
}
}
```