11.5
1、常用的文件操作
方式一:根据路径构建一个 File 对象:
//方式 1 @Test public void create1(){ String filePath = "D:\\file1.txt"; File file = new File(filePath); try { file.createNewFile(); System.out.println("创建文件 1 成功"); } catch (IOException e) { e.printStackTrace(); } }
方式二:根据父目录文件+子路径构建:
//方式 2 @Test public void create2(){ File parentFile = new File("D:\\"); String fileNane = "file2.txt"; File file = new File(parentFile, fileNane); try { file.createNewFile(); System.out.println("文件 2 创建成功"); } catch (IOException e) { throw new RuntimeException(e); } }
方式三:根据父目录+子路径构建:
//方式三 @Test public void create3(){ String parentPath = "d:\\"; String filePath = "file3.txt"; File file = new File(parentPath, filePath); try { file.createNewFile(); System.out.println("文件 3 创建成功"); } catch (IOException e) { throw new RuntimeException(e); } }
2、获取文件的相关信息
Serializable是java中实现对象序列化与反序列化的方式之一,使用方式非常简单,只要在在类声明时实现Serializable接口就可以。
Comparable是个排序接口,若一个类实现了该接口,那么该类的数组和列表就可以通过Collections.sort或Arrays.sort进行自动排序。
gbk编码: 中文占用两个字节,英文占用一个字节
utf-8编码:中文占用三个字节,英文占用一个字节
常见的 File 相关方法: getName()/getAbsolutePath/getParent/length/exists/isFile/isDirectory
//获取文件信息 @Test public void Info(){ //先创建文件对象 File file = new File("D:\\file1.txt"); //调用相应方法,得到对应信息 System.out.println("文件名称:"+file.getName()); System.out.println("文件绝对路径:"+file.getAbsolutePath()); System.out.println("文件父目录:"+file.getParent()); System.out.println("文件大小(字节):"+file.length()); System.out.println("文件是否存在:"+file.exists()); System.out.println("是否是文件:"+file.isFile()); System.out.println("是否是目录:"+file.isDirectory()); }
3、目录的操作
创建一级目录:mkdir,创建多级目录:mkdirs ,delete 删除空目录或者文件:
@Test //删除文件 public void fileDelete() { String filePath = "D:\\file1.txt"; File file = new File(filePath); if (file.exists()) { if (file.delete()) { System.out.println(filePath + "删除成功"); } else { System.out.println(filePath + "删除失败"); } ; } else { System.out.println("文件不存在"); } } //删除目录 @Test public void fileDeleteD() { String filePath = "D:\\file1.txt"; File file = new File(filePath); if (file.exists()) { if (file.delete()) { System.out.println(filePath + "删除成功"); } else { System.out.println(filePath + "删除失败"); } ; } else { System.out.println("目录不存在"); } } //判断目录是否存在,不存在就创建 @Test public void fileDeleteD1() { String dirPath = "D:\\test\\dir1.txt"; File file = new File(dirPath); if (file.exists()) { System.out.println(dirPath + "该目录已经存在"); } else { if (file.mkdirs()) { System.out.println("创建成功"); } else { System.out.println("创建失败"); } ; } }
4、IO 流原理及流的分类
1、I=input,O=output,用于处理数据传输,读写文件、网络通信
2、Java 程序中,输入输出以流的形式进行
3、java.io 包下
流的分类:
按操作数据单位不同可以分为:字节流(8bit)、字符流(字符,对应字节按照编码确定)
效率:字符流更好,字节流的存在是为了更好的处理二进制文件,音视频
按流向不同可以分为:输入流、输出流
按流的角色不同分为:节点流、处理流
在java中InputStream是字节输入流,用来将文件中的数据读取到java程序中。InputStream是所有字节输入流的顶层父类,是一个抽象类。如果要用,需要使用子类。最常用的子类:FileInputStream。
public abstract class InputStream implements Closeable { private static final int MAX_SKIP_BUFFER_SIZE = 2048; public InputStream() { } public abstract int read() throws IOException; public int read(byte[] var1) throws IOException { return this.read(var1, 0, var1.length); }
下面是idea生成的类图:
OutputStream抽象类,此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。
public abstract class OutputStream implements Closeable, Flushable { public OutputStream() { } }
void close()
关闭此输出流并释放与此流有关的所有系统资源。void flush()
刷新此输出流并强制写出所有缓冲的输出字节。void write(byte[] b)
将 b.length 个字节从指定的 byte 数组写入此输出流。void write(byte[] b, int off, int len)
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。abstract void write(int b)
将指定的字节写入此输出流。
Java.io.Writer类是用于写入字符流的抽象类。而且Writer类是Java IO中所有Writer的基类。它的子类如:BufferedWriter和PrintWriter等
public abstract class Writer implements Appendable, Closeable, Flushable{ }
Reader是Java的IO库提供的另一个输入流接口。和InputStream的区别是,InputStream是一个字节流,即以byte为单位读取,而Reader是一个字符流,即以char为单位读取。
public abstract class Reader implements Readable, Closeable { }
![image](https://img2022.cnblogs.com/blog/2979319/202211/2979319-20221105160735108-97498430.png)
前面四个都是抽象类,想要使用必须实现其具体方法
5、Scanner 与 Println
1、基本键盘输入:
import org.testng.annotations.Test; import java.util.Scanner; public class scanPrintTest { public static void main(String[] args) { //创建 Scanner 对象,接受从控制台输入 Scanner input = new Scanner(System.in); //接受 String 类型 String str = input.next(); //输出结果 System.out.println(str); System.out.println("hello world"); } }
2、常见键盘输入类型:
import java.util.Scanner; public class scanTest { public static void main(String[] args) { Scanner input =new Scanner(System.in); //double 类型的数据 System.out.print("请输入一个 double 类型的数:"); double d = input.nextDouble(); System.out.println(d); //int 类型的数据 System.out.print("请输入一个 int 类型的数:"); int i = input.nextInt(); System.out.println(i); //字符串类型的数据 System.out.print("请输入一个 string 类型的数:"); String s = input.next(); System.out.println(s); } }
6、InputStream
常见的子类:
FileInputStream:文件输入流
BufferedStream:缓冲字节输入流
ObjectedStream:对象字节输入流
public class fileInputStream { public static void main(String[] args) { } @Test public void fileIn1() { String filePath = "D:\\testfile.txt"; int readData; FileInputStream fileInputStream = null; try { //创建 FileInputStream 对象,用于读取文件 fileInputStream = new FileInputStream(filePath); while ((readData = fileInputStream.read()) != -1) { System.out.println((char) readData); } ; } catch (IOException e) { e.printStackTrace(); } finally { //关闭文件流 try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } @Test public void fileIn2() { String filePath = "D:\\testfile.txt"; int readData; int readlength = 0; //字节数组 byte[] buf = new byte[8]; FileInputStream fileInputStream = null; try { //创建 FileInputStream 对象,用于读取文件 fileInputStream = new FileInputStream(filePath); while ((readlength = fileInputStream.read(buf)) != -1) { System.out.println(new String(buf, 0, readlength)); } ; } catch (IOException e) { e.printStackTrace(); } finally { //关闭文件流 try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } @Test public void fileIn3() { String filePath = "D:\\testfile.txt"; int readData; int readlength = 0; //字节数组 byte[] buf = new byte[8]; FileInputStream fileInputStream = null; try { //创建 FileInputStream 对象,用于读取文件 fileInputStream = new FileInputStream(filePath); while ((readlength = fileInputStream.read(buf)) != -1) { System.out.println(new String(buf, 0, readlength)); } ; } catch (IOException e) { e.printStackTrace(); } finally { //关闭文件流 try { fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
7、OutputStream
1、FileOutputStream:
例子:使用 FileOutputStream 在文件夹中写入“hello world”,如果文件不存在,会创建文件
public class outputStream { public static void main(String[] args) { } //使用 FileOutputStream 将数据写到文件中 //如果文件不存在,则创建文件 @Test public void writeFile() { String filePath = "d:\\fileout.txt"; //创建对象 FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(filePath); //1、写入一个字节 //fileOutputStream.write('H'); //2、写入一个字符串 //String str = "hello socket"; //str.getBytes()将字符串转换成字节数组 //fileOutputStream.write(str.getBytes()); //3、写入字符串 String str = "hello lst"; fileOutputStream.write(str.getBytes(), 0, str.length()); } catch (IOException e) { e.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
8、文件复制
public class copyPic { //file 读和写实现复制文件 public static void main(String[] args) throws Exception { //创建 file 对象 File f = new File("d:\\test.jpg"); //判断文件是否存在 if (f.exists()) { System.out.println("test.jpg 存在,可以复制"); } else { f.createNewFile(); System.out.println("test.jpg 不存在,新建成功,可以复制"); } //创建 FileInputStream 对象 FileInputStream inp = new FileInputStream(f); //创建 FileOutputStream 对象 //判断 demo 目录是否存在 File f1 = new File("d:\\demo"); if (f1.isDirectory()) { FileOutputStream out = new FileOutputStream("e:\\demo\\" + f.getName()); byte bytes[] = new byte[1024]; int temp = 0; //边读边写 while ((temp = inp.read(bytes)) != -1) { //读 out.write(bytes, 0, temp); //写 } //结束 inp.close(); out.close(); System.out.println("文件拷贝成功!"); } else { //新建 demo 目录 f1.mkdir(); System.out.println("demo 目录不存在,已经新建成功,继续复制"); FileOutputStream out = new FileOutputStream("d:\\demo\\" + f.getName()); byte bytes[] = new byte[1024]; int temp = 0; //边读边写 while ((temp = inp.read(bytes)) != -1) { //读 out.write(bytes, 0, temp); //写 } //结束 inp.close(); out.close(); System.out.println("文件拷贝成功!"); } } }