4月3日IO流
删除文件夹
public static void main(String[] args) {
// new File("testIO").delete();
deleteDir(new File("testIO"));
}
public static void deleteDir(File dir){
if(dir.isDirectory()){
File[] listFiles = dir.listFiles();
for (File sub : listFiles) {
deleteDir(sub);
}
}
dir.delete();
}
复制文件夹
public static void main(String[] args) throws IOException {
File src = new File("D:/testIO");
File dest = new File("testIO");
copyDir(src, dest);
}
public static void copyDir(File src, File dest) throws IOException{
if(dest.isFile()){
throw new RuntimeException(dest +"不是文件夹");
}
if(src.isFile()){
File destFile = new File(dest.getPath()+"/" + src.getName());
copyFile(src, destFile);
}else if(src.isDirectory()){
File destFile = new File(dest.getPath()+"/" + src.getName());
destFile.mkdir();
File[] listFiles = src.listFiles();
for (File sub : listFiles) {
copyDir(sub,destFile);
}
}
}
public static void copyFile(File srcFile, File destFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] data = new byte[1024];
int len;
while((len = fis.read(data)) !=-1){
fos.write(data, 0, len);
}
fis.close();
fos.close();
}
递归遍历文件夹
public static void main(String[] args) {
File file = new File("C:/zzk/zzk_code");
ArrayList<String> all = listAllSubs(file);
for (String string : all) {
System.out.println(string);
}
}
public static ArrayList<String> listAllSubs(File file) {
ArrayList<String> list = new ArrayList<>();
if (file.isFile()) {
if (file.getName().endsWith(".java")) {
list.add(file.getPath());
}
} else if (file.isDirectory()) {
File[] listFiles = file.listFiles();
for (File sub : listFiles) {
list.addAll(listAllSubs(sub));
}
}
return list;
}
java_cmd 和 file 做 IO
Scanner input = new Scanner(System.in);
PrintStream ps = new PrintStream("words.txt");
for (int i = 0; i < 3; i++) {
System.out.println("请输入第" + (i + 1) + "句要对柴老师说的话:");
String content = input.nextLine();
ps.println(content);
}
input.close();
ps.close();
Scanner input2 = new Scanner(new File("words.txt"));
while (input2.hasNextLine()) {
System.out.println(input2.nextLine());
}
input2.close();
把对象存入 ROM
public static void main(String[] args) throws IOException, ClassNotFoundException {
test01();
test02();
}
public static void test01() throws IOException{
Message msg = new Message("柴老师", "佟老师", "加工资", System.currentTimeMillis());
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("message.dat"));
oos.writeObject(msg);
oos.close();
}
public static void test02() throws IOException, ClassNotFoundException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("message.dat"));
Object msg = ois.readObject();
System.out.println(msg);
ois.close();
}
数据在 RAM 和 ROM 之间 IO
int a = 10;
char c = 'a';
double d = 2.5;
boolean b = true;
String str = "尚硅谷";
// output 把程序中的变量 write 到 ROM
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.dat"));
dos.writeInt(a);
dos.writeChar(c);
dos.writeDouble(d);
dos.writeBoolean(b);
dos.writeUTF(str);
dos.close();
// input 从 file 中 read 数据到 RAM
DataInputStream dis = new DataInputStream(new FileInputStream("data.dat"));
System.out.println(dis.readInt());
System.out.println(dis.readChar());
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
System.out.println(dis.readUTF());
dis.close();
复制时修改字符编码
try(
FileInputStream fis = new FileInputStream("D:/testIO/我想对你说.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis,"GBK");
// 分三层的IO
FileOutputStream fos = new FileOutputStream("testIO/柴老师的话.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF-8");
){
char[] data = new char[1024];
int len;
while((len = isr.read(data))!=-1){
osw.write(data, 0, len);
}
}catch(IOException e){
e.printStackTrace();
}
复制一个文件
try (
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream("D:/testIO/test.txt")); // 源文件
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("testIO/test.txt")); // 目标文件
) {
byte[] data = new byte[1024];
int len;
// 分批读入
while ((len = bis.read(data)) != -1) {
bos.write(data, 0, len); // 最后一批长度不足1024
}
} catch (IOException e) {
e.printStackTrace();
}
删除某个文件或文件夹都是delete
File file4 = new File("d:/testIO/a.txt");
file4.delete();
判断是不是一个文件或文件夹
File file3 = new File("d:/testIO");
if (file3.isFile()) {
System.out.println(file3 + "是一个文件。");
} else if (file3.isDirectory()) {
System.out.println(file3 + "是一个文件夹");
}
获取文件属性
System.out.println("文件名:" + file2.getName());
System.out.println("文件大小:" + file2.length());
System.out.println("文件的绝对路径:" + file2.getAbsolutePath());
System.out.println("文件的父目录:" + file2.getParent());
新建 file 时,考虑异常
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
throws IOException
文件是否存在
if (file.exists()) {
}
新建一个文件夹或文件:
File dir = new File("D:/testIO"); // 用 String 指定绝对路径
dir.mkdir(); // make directory 开一个没后缀的文件夹目录
File file = new File("D:/testIO/test.txt");
file.createNewFile(); // 开一个有后缀的新文件
dir = new File("testIO"); // 用 String 指定相对路径
dir.mkdir();
file = new File("testIO/test.txt");
file.createNewFile();
IO流:
dir file
input output reader writer stream
四个顶级抽象父类 {字节流IO, 字符流IO}
4月3日
昨晚 war3 到12点,今天精神还可以
周三去医院抽血
犹豫 Redmi G i7 3050 + Redmi手机