学习笔记-java IO字节流和字符流
字节流
可以处理一切文件包括二进制、音频、视频、doc等
节点流 InputStream FileInputStream
OutputStream FileOutputStream
一、读取文件
建立联系
File对象
选择流
文件输入流 InputStream FileInputStream
操作
byte[] car = new byte[1024] + read + 读取大小
输出
释放资源
关闭
Code
package com.iotest.byteIO;
import java.io.*;
public class Demo01 {
/**
* 文件的读取2
* @param args
*/
public static void main(String[] args) {
File src = new File("D:/Code/Java/IO/src/main/resources/2.txt");
InputStream is = null;
try {
is = new FileInputStream(src);
byte[] car = new byte[10];
int len = 0;
while ((len = is.read(car)) != -1) {
String info = new String(car, 0, len);
System.out.println(info);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("Not found");
} catch (IOException e) {
e.printStackTrace();
System.out.println("read fail");
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("file close");
}
}
}
}
二、写出文件
建立联系
File对象
选择流
文件输出流 OutputStream FileOutputStream
操作
write() + flush
释放资源
关闭
Code
package com.iotest.byteIO;
import java.io.*;
public class Demo02 {
public static void main(String[] args) {
File dest = new File("D:/Code/Java/IO/src/main/resources/2.txt");
OutputStream os = null;
try {
os = new FileOutputStream(dest, true);
String str = "hello hello hello \r\n";
byte[] data = str.getBytes();
os.write(data, 0, data.length);
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("not found");
} catch (IOException e) {
e.printStackTrace();
System.out.println("write fail");
} finally {
try {
if (os != null) {
os.close();
}
} catch (Exception e2) {
System.out.println("close fail");
}
}
}
}
三、文件拷贝
程序为桥梁
建立联系
File对象 源头 目的地
选择流
文件输入流 InputStream FileInputStream
文件输出流 OutputStream FileOutputStream
操作
拷贝
byte[] flush = new byte[1024];
int len = 0;
while(-1 != (len=输入流.read(flush))){
输出流.write(flush,0,len)
}
释放资源
关闭 两个流
Code
package com.iotest.byteIO;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import java.io.*;
public class Demo03CopyFile {
public static void main(String[] args) {
File src = new File("D:/Code/Java/IO/src/main/resources/test.png");
File dest = new File("D:/Code/Java/IO/src/main/resources/100.png");
try {
copyFile(src,dest);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copyFile(File src,File dest) throws IOException {
if (!src.isFile()) {
System.out.println("only file");
throw new IOException("only file");
}
// 建立联系 源(存在且为文件) + 目的地(文件可以不存在)
// 选择流
InputStream is = new FileInputStream(src);
OutputStream os = new FileOutputStream(dest);
if (!src.isFile()) {
System.out.println("only file");
}
// 文件拷贝 循环+读取+写出
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = is.read(flush))) {
os.write(flush, 0, len);
}
os.flush();
os.close();
is.close();
}
}
四、文件夹拷贝
-
递归查找子孙级文件|文件夹
-
文件 复制(IO流复制)
文件夹 创建
Code
package com.iotest.byteIO;
import java.io.File;
import java.io.IOException;
public class Demo04CopyDir {
public static void main(String[] args) {
// 源目录
String srcPath = "D:/Code/Java/IO/src/main/resources/testfile";
// 目标目录
String destPath = "D:/Code/Java/IO/src/main/resources/1";
File src = new File(srcPath);
File dest = new File(destPath);
copyDir(src,dest);
}
/**
* 拷贝文件夹
* @param src 源File对象
* @param dest 目标File对象
*/
public static void copyDir(File src, File dest) {
if (src.isDirectory()) {
dest = new File(dest,src.getName());
}
copyDirDetail(src,dest);
}
/**
* 拷贝文件夹细节
* @param src
* @param dest
*/
private static void copyDirDetail(File src, File dest) {
if (src.isFile()){
try {
Demo03CopyFile.copyFile(src,dest);
} catch (IOException e) {
e.printStackTrace();
}
}else if (src.isDirectory()) {
// 确保目标文件夹存在
dest.mkdirs();
// 获取下一级目录|文件
for (File sub:src.listFiles()) {
copyDirDetail(sub,new File(dest,sub.getName()));
}
}
}
}
字符流
智能处理纯文本、全部为可见字符 .txt .html
节点流 Reader FileReader
Writer FileWriter
一、纯文本读取
建立联系
选择流
Reader FileReader
读取
char[] flush = new char[1024]
关闭
code
package com.iotest.charIO;
import java.io.*;
public class Demo01 {
public static void main(String[] args) {
File src = new File("D:/Code/Java/IO/src/main/resources/2.txt");
Reader reader =null;
try {
reader = new FileReader(src);
char[] flush = new char[10];
int len =0 ;
while (-1 != (len = reader.read(flush))) {
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("not found");
} catch (IOException e) {
e.printStackTrace();
System.out.println("read fail");
}finally {
if (null!=reader) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
二、纯文本写出
建立联系
选择流
Writer FileWriter
读取
write(字符数组,0,长度) + flush
write(字符串)
append(字符串)
关闭
Code
package com.iotest.charIO;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Demo02 {
public static void main(String[] args) {
File dest = new File("D:/Code/Java/IO/src/main/resources/2.txt");
Writer wr = null;
try {
wr = new FileWriter(dest);
String msg = "重写:哈哈哈哈哈哈哈哈哈 啊哈哈哈哈 \r\n嘻嘻嘻嘻嘻嘻 \r\nh和急吼吼";
wr.write(msg);
wr.append("append");
wr.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (wr != null) {
try {
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}