IO流
file类
1.目录 - 文件夹
2.如何创建File类的对象
File f = new File("路径");
-
路径分隔符
-
Wnidows:
-
\,但也是转义字符,和其后的字符结合表示特定含义
-
使用“
\\
”表示\ 本身 -
/,和操作系统无关的路径分隔符
-
-
3.常用方法
File f1 = new File("C:\\Users\\11094\\Desktop\\1.txt"); System.out.println(f1); System.out.println("是否隐藏" + f1.isHidden()); System.out.println("是否可读" + f1.canRead()); System.out.println("是否可写" + f1.canWrite()); System.out.println("绝对路径" + f1.getAbsolutePath()); System.out.println("文件名" + f1.getName()); System.out.println("是否是文件夹" + f1.isDirectory()); System.out.println("是否是文件" + f1.isFile()); System.out.println("最后修改时间:" + str); System.out.println("文件大小" + f1.length()); System.out.println("是否存在:" + f1.exists()); File f2 = new File("C:\\Users\\11094\\Desktop\\abcd1234"); if(!f2.exists()){ f2.mkdir(); }else{ f2.delete(); }
时间戳 --> 特定格式的日期时间字符串
//lastModified():返回时间戳 long l = f1.lastModified();//时间戳 --> Date Date date = new Date(l); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = sdf.format(date);//Date --> String
IO
一、Io流分类
字节流 8字节,处理二进制文件,文本文件 InputStream,OutputStream
字符流 16字节,处理文本文件 Reader,Writer
-
read()
-
read(byte[] b)
-
read(byte[] b,int offset, int length)
共同点:1.都是从输入流读取数据
2.读取到文件的末尾,都返回-1
不同点:1. read()返回每次读取到的那一个字节
-
read(byte[] b)返回读取到的字节数量,将读取到的数据放在了b这个数组中
-
read(byte[] b,int offset, int length)返回读取到的字节数量,将读取到的数据放在了b这个数组中(从数组的第offset这个位置开始存放)
字节流
FileInputStream in = new FileInputStream("C:/Users/11094/Desktop/1.txt"); byte[] arr = new byte[1024];//保存读取到数据的数组/缓冲区 int len = 0;//表示每次读取到的数据的长度/字节数 //使用IO流进行读写操作 while(true){ len = in.read(arr,0,arr.length);//从0开始,最多读取arr.length个字节 if(len == -1){ break; }//返回值:每次运行读取到的字节数(有可能小于arr.length),如果读取到了文件末尾,则返回-1 // //byte[] --> String String s = new String(arr,0,len); System.out.println(s); } in.close();
FileOutputStream out = new FileOutputStream("C:/Users/11094/Desktop/2.txt"); //String --> byte String str = "hello world"; byte[] arr = str.getBytes(); //将数组中的内容写入文件 out.write(arr,0,arr.length); //关闭流 out.close();
//OutputStream //第二个参数如果是true,是以追加的方式写入数据log日志 FileOutputStream out = new FileOutputStream("C:/Users/11094/Desktop/2.txt"); //String --> byte String str = "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"; byte[] arr = str.getBytes(); //将数组中的内容写入文件 out.write(arr,0, arr.length); out.close();
//图片拷贝 FileInputStream in = new FileInputStream("C:/Users/11094/Desktop/1.png"); FileOutputStream out = new FileOutputStream("C:/Users/11094/Desktop/2.png"); int len = 0;//接收读取到的长度 byte[] arr = new byte[1024];//接收读取到的数据 //读取写入 while(true){ len = in.read(arr); if(len == -1){ break; } out.write(arr,0,len); } //关闭流,后打开的先关闭 out.close(); in.close();
//文件拷贝 FileInputStream in = new FileInputStream("C:/Users/11094/Desktop/1.txt"); FileOutputStream out = new FileOutputStream("C:/Users/11094/Desktop/3.txt"); int len = 0; char[] arr = new char[1024]; while(true){ len = in.read(arr); if(len == -1){ break; } out.write(arr,0,len); } out.close(); in.close();
缓冲流
//缓冲流拷贝文件 BufferedReader in = new BufferedReader(new FileReader("C:/Users/11094/Desktop/1.txt")); BufferedWriter out = new BufferedWriter(new FileWriter("C:/Users/11094/Desktop/4.txt")); int len = 0; char[] arr = new char[1024]; while(true){ len = in.read(arr); if(len == -1){ break; } out.write(arr,0,len); } out.close(); in.close();
//缓冲流按行读取 BufferedReader in = new BufferedReader(new FileReader("C:/Users/11094/Desktop/1.txt")); String line = null; while(true){//按行读取,读取到末尾返回null line = in.readLine(); if(line == null){ break; } System.out.println(line); }