当前位置: 首页> 文旅> 文化 > 短视频获客_项目管理系统平台_fifa世界排名最新_河南郑州做网站的公司

短视频获客_项目管理系统平台_fifa世界排名最新_河南郑州做网站的公司

时间:2025/7/10 2:18:18来源:https://blog.csdn.net/Flipped_275/article/details/144408204 浏览次数:0次
短视频获客_项目管理系统平台_fifa世界排名最新_河南郑州做网站的公司

1.  File类文件操作

1.1  File对象

File 文件

File 对操作系统中的文件或者文件夹进行增删改

 1.2  File对象的创建和路径分隔符

在不重要的盘符下  创建一个  文件夹  里面在创建一个文件夹  (不要有中文)

import java.io.File;public class Test {public static void main(String[] args) {/*new File()要操作的文件路径*/File file = new File("D:\\aaa666\\123.txt");/*** 1. 在Windows 下,路径分隔符是 \,在Linux下是 /* 2. 编程中 \ 是转义字符,需要用 \\ 来表示 \* 3. java是一种跨平台的语言,所以路径分隔符最好用 / 因为所有平台都支持 /* 4. File类提供了 静态成员变量 separator,可以获取当前操作系统的路径分隔符* */File file1 = new File("D:/aaa666/123.txt");new File("D:" + File.separator + "aaa666" + File.separator + "123.txt")System.out.println(File.separator);System.out.println(File.pathSeparator);}
}

 1.3  文件和文件夹的创建

public void test02() throws IOException {File file = new File("D:/aaa666/456.txt");/* 创建文件*/file.createNewFile();File file1 = new File("D:/aaa666/haha");/*创建文件夹*/file1.mkdir();/*** 注意:到底是创建文件还是文件夹,取决于调用的函数* */File file2 = new File("D:/aaa666/hehe");file2.createNewFile();/* 创建多级文件夹*/File file3 = new File("D:/aaa666/123/456/789");file3.mkdirs();}

1.4  文件修改与删除操作

public void test03(){File file = new File("D:/aaa666/456.txt");/* 设置文件为只读*/file.setReadOnly();/* 设置文件最后修改时间*/file.setLastModified(1000);/* 重命名文件*/file.renameTo(new File("D:/aaa666/haha.txt"));/* 剪切操作*/File file1 = new File("D:/aaa666/123.txt");file1.renameTo(new File("D:/123.txt"));/* 删除文件操作*/File file2 = new File("D:/aaa666/123.txt");file2.delete();/* 删除文件夹*/File file3 = new File("D:/aaa666/haha");file3.delete();/* 注意: 删除文件夹,必须为空文件夹,才能删除*/File file4 = new File("D:/aaa666/123");file3.delete();}

1.5 File类查询文件信息

public void test04() throws IOException {/* 判断是当前file对象否为一个文件*/File file = new File("D:/aaa666/haha.txt");boolean isfile = file.isFile();System.out.println(isfile);/* 判断是当前file对象否为一个文件夹*/boolean directory = file.isDirectory();System.out.println(directory);/* 判断文件是否隐藏*/boolean hidden = file.isHidden();System.out.println(hidden);/* getName()获取文件名 getAbsolutePath()获取绝对路径 getCanonicalPath()获取规范路径*/String name = file.getName();String absolutePath = file.getAbsolutePath();String canonicalPath = file.getCanonicalPath();System.out.println(name + "-----" + absolutePath + "-----" + canonicalPath);/* 获取父级路径 getPath()获取路径*/String parent = file.getParent();String path = file.getPath();System.out.println(parent + "-----" + path);/* 判断文件是否可读,可写,可执行*/boolean b = file.canRead();boolean b1 = file.canWrite();boolean b2 = file.canExecute();System.out.println(b + "-----" + b1 + "-----" + b2);/* 获取当前文件夹下的所有文件*/File file1 = new File("D:/aaa666/123");String[] list = file1.list();File[] files = file1.listFiles();System.out.println(Arrays.toString(list));System.out.println(Arrays.toString(files));}

File递归查找题

找到指定目录下所有的java文件。

 

public class FindJava {public static void main(String[] args) {File file = new File("D:/aaa");Scanner scanner = new Scanner(System.in);System.out.println("请输入要查找的文件类型(.xxx):");String type = scanner.next();find(file, type);}public static void find(File file, String type) {if (file.isFile()) {String name = file.getName();if (name.endsWith(type)) {System.out.println(name);}} else if (file.isDirectory()) {File[] files = file.listFiles();for (File f : files) {find(f, type);}}}
}

2.  Java中的IO操作

2.1  IO入门简介

java的I0操作 称之为 java的输入输出流。
            I指的是 Inputstrem 输入流
            0指的是 Outputstream 输出流

java的I0操作简单的说就是通过 java代码对文件内容进行读写操作。

Java流的分类
按流向分:
            输入流:程序可以从中读取数据的流。
            输出流:程序能向其中写入数据的流。

技数据传输单位分:
            字节流:以字节为单位传输数据的流。
            字符流:以字符为单位传输数据的流。

按功能分:
            节点流:用于直接操作目标设备的流。
            过滤流:是对一个已存在的流的链接和封装,通过对数据进行处理为程序提供功能强大、灵活的读写功能。

四大基流:
            字节输入流 Inputstream
            字节输出流 QutputStream  

            字符输入流 Reader
            字符输出流 Writer

输出流:写到文件(硬盘)中去

输入流:读进内存中来  

2.2  字节输出流

public abstract class OutputStream
extends Object
implements Closeable, Flushable
这个抽象类是表示字节输出流的所有类的超类

public void test() throws IOException {/* 1. 创建一个流*/FileOutputStream out = new FileOutputStream("D:/aaa666/123.txt");/* 2. out.write(int类型的值); 代表的是字符编码  ASCII码  d 100 e 101*/out.write(100);out.write(101);out.write(102);out.write(103);out.write(104);/* 3. 关闭流*/out.close();}public void test() throws IOException {/* 1. 创建一个流*/FileOutputStream out = new FileOutputStream("D:/aaa666/123.txt");byte[] data = "hello world".getBytes();/* out.write(data); 参数为 byte[] 数组的格式*/out.write(data);/* 3. 关闭流*/out.close();}public void test() throws IOException {/*** 1. 创建一个流* FileOutputStream out = new FileOutputStream(目标路径, true代表追加内容);* */FileOutputStream out = new FileOutputStream("D:/aaa666/123.txt", true);byte[] data = "hello world".getBytes();/* out.write(byte数组, 起始索引位置, 长度);  */out.write(data,1,3);/* 3. 关闭流*/out.close();}

2.3  字节输入流

public abstract class InputStream
extends Object
implements Closeable这个抽象类是表示输入字节流的所有类的超类。 

//第一种读取方式
public void test2() throws IOException {/*** 1. 创建一个流* FileInputStream out = new FileInputStream(目标路径);* */FileInputStream in = new FileInputStream("D:/aaa666/123.txt");/* 2. 读取内容 in.read(): 读取文件中的第一个字节,返回其字符编码code值*/int data = in.read();System.out.println(data);int data1 = in.read();System.out.println(data1);/* 3. 关闭流*/in.close();}//第二种读取方式
public void test2() throws IOException {/*** 1. 创建一个流* FileInputStream out = new FileInputStream(目标路径);* */FileInputStream in = new FileInputStream("D:/aaa666/123.txt");/* 2. 读取内容 in.read(): 读取文件中的第一个字节,返回其字符编码code值*/byte[] data = new byte[50];/* 当前函数返回值 代表读取内容的长度 in.read(byte数组);*/int len = in.read(data);//14System.out.println(len);System.out.println(Arrays.toString(data));/* 把字节数组转换成字符串  */String str = new String(data, 0, len);System.out.println(str);/* 3. 关闭流*/in.close();}public void test2() throws IOException {/*** 1. 创建一个流* FileInputStream out = new FileInputStream(目标路径);* */FileInputStream in = new FileInputStream("D:/aaa666/123.txt");/* 2. 读取内容 in.read(): 读取文件中的第一个字节,返回其字符编码code值*/byte[] data = new byte[5];/* 当前函数返回值 代表读取内容的长度 in.read(byte数组);*/int len = in.read(data);System.out.println(len);/* 把字节数组转换成字符串  */String str = new String(data, 0, len);System.out.println(str);int len1 = in.read(data);System.out.println(len);/* 把字节数组转换成字符串  */String str1 = new String(data, 0, len);System.out.println(str1);/* 3. 关闭流*/in.close();}//文件内容的循环读取
public void test2() throws IOException {/*** 1. 创建一个流* FileInputStream out = new FileInputStream(目标路径);* */FileInputStream in = new FileInputStream("D:/aaa666/123.txt");/* 2. 读取内容 in.read(): 读取文件中的第一个字节,返回其字符编码code值*/byte[] data = new byte[5];int len = 0;StringBuilder sb = new StringBuilder();/***  首先执行 in.read(data) 读取一次诗句到data数组中*  然后将本次读取内容的长度赋值给 len*  最后 判断本次读取的长度是否大于0,大于0就继续执行,否则就退出循环* */while ((len = in.read(data)) > 0){String str = new String(data,0,len);sb.append(str);}System.out.println(sb);/* 3. 关闭流*/in.close();}

2.4  字符输出流

public abstract class Writer
extends Object
implements Appendable, Closeable, Flushable、
用于写入字符流的抽象类。 子类必须实现的唯一方法是write(char [],int,int),flush()和close()。 然而,大多数子类将覆盖这里定义的一些方法,以便提供更高的效率,附加的功能或两者。

public class Test09 {public static void main(String[] args) throws IOException {Writer w=new FileWriter("demo02/f.txt");w.write("你好,李焕英!",0,3);w.close();}
}

2.5  字符输入流

public abstract class Reader
extends Object
implements Readable, Closeable用于读取字符流的抽象类。 子类必须实现的唯一方法是read(char [],int,int)和close()。 然而,大多数子类将覆盖这里定义的一些方法,以便提供更高的效率,附加的功能或两者。

public class Test10 {public static void main(String[] args) throws Exception {//字符输入流 ReaderReader r=new FileReader("demo02/f.txt");/*//一次读取一个字符int read=-1;while (  (read=r.read())!=-1  ){System.out.print((char)read);}*//*一次读取多个字符*/char[] cs=new char[50];int size=-1;while (  (size=r.read(cs))!=-1  ) {System.out.println(new String(cs, 0, size));}r.close();}
}

2.6  小结

字节流:适合读取的单位为字节类型。适合读取音频,视频,压缩等。

字符流:适合读取文本类。一定不适合音频和视频操作。

2.7  缓存流

缓存流就是在基础流上再一次做了封装。提高io的效率。默认大小[8192](8k)

BufferInputStreamBufferOutputStream

public class Buffer {public static void main(String[] args) throws IOException {long l = System.currentTimeMillis();FileInputStream in = new FileInputStream("demo01/IMG_9977.JPG");BufferedInputStream bis = new BufferedInputStream(in);FileOutputStream os = new FileOutputStream("demo02/1189.JPG");BufferedOutputStream bos = new BufferedOutputStream(os);byte[] bytes = new byte[1024];int len = -1;//len = in.read(bytes);//os.write(bytes, 0, len);while ((len = bis.read(bytes)) > 0) {bos.write(bytes);}bos.close();bis.close();in.close();os.close();long l1 = System.currentTimeMillis();System.out.println(l1 - l);//验证时间}
}

 BufferedReaderBufferedWriter

public class Buffer {public static void main(String[] args) throws IOException {long l = System.currentTimeMillis();FileReader reader = new FileReader("E:/aaa/aaa.txt");BufferedReader br = new BufferedReader(reader);FileWriter writer = new FileWriter("D:/aaa/aaa.txt");BufferedWriter bw = new BufferedWriter(writer);char[] chars = new char[1];int len = 0;while ((len = br.read(chars)) > 0) {bw.write(new String(chars, 0, len));}bw.close();br.close();reader.close();writer.close();System.out.println(System.currentTimeMillis() - l);}
}

2.8  对象流

把java内存中的对象写入到文件【网盘】中,或把文件中写入得java对象,读取到程序中的过程。---设计到对象流。

存档 ---序列化:讲java对象转换成二进制的数据流操作。

实现序列化Serializable接口即可

java程序设定 如果程序员将某个类设定实现 Serializable 此时就表明当前类可以被序列化就会调用底层源码。 所以 Serializable 的作用就是 标记 当前类可以被序列化。  

public class Test {public static void main(String[] args) throws Exception{Game game=new Game("李白",16,100,60);OutputStream os=new FileOutputStream("wzry/info/p1.txt");ObjectOutputStream oos=new ObjectOutputStream(os);//存档方法 java.io.NotSerializableException: 没有序列化。---oos.writeObject(game);oos.close();os.close();}
}

 读档 ---反序列化:将二进制数据流转换成java的操作。

public class Test02 {public static void main(String[] args) throws Exception{InputStream is=new FileInputStream("wzry/info/p1.txt");ObjectInputStream ois=new ObjectInputStream(is);//读档Object o = ois.readObject();System.out.println(o);ois.close();is.close();}
}

关键字:短视频获客_项目管理系统平台_fifa世界排名最新_河南郑州做网站的公司

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: