Java IO流
File类
import java.io.File;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) {
//推荐这样构建路径,因为可以跨平台。 不要用 \ 或者 ;
String path = "D:/Program Files/Test/tester.txt";
File src1 = new File("D:/Program Files/Test/tester.txt"); //绝对路径
String parentPath = "D:/Program Files/Test";
String fileName = "tester.txt";
File src2 = new File(parentPath, fileName); //相对路径
//File类常用方法
//1、获取文件名,路径名
src2.getName();
src2.getPath();
src2.getAbsolutePath();
src2.getParent();
//2、判断信息
System.out.println("文件是否存在: " + src2.exists());
System.out.println("文件是否可写:" + src2.canWrite());
src2.isFile();
src2.isDirectory();
src2.isAbsolute();
//3、长度
src2.length(); //这里是字符节长度
}
//4、创建删除文件
public static void testCreateFile() throws IOException {
String parentPath = "D:/Program Files/Test";
File src = new File(parentPath);
if (!src.exists()){
boolean flag = src.createNewFile(); //创建。 src.delete() - 删除
System.out.println(flag?"succeed!":"failed!");
}
}
//5、操作目录
src.mkdir() //必须有父目录
src.mkdirs() //会创建父目录
//其他方法的,在应用中了解
}
一、概念
流:从一端移动到另一端 (源头与目的地)ex: 程序与文件 | 数组 | 网络连接 | 数据库
二、IO流分类
1、流向:输入流与输出流
2、数据:字节流:二进制,可以作用于一些文件,包括纯文本、音频、视频等等。
字符流:文本文件,只能处理纯文本
3、功能:节点流:包裹源头
处理:增强功能,提供性能
三、字符流与字节流(重点)
1、字节流
输入流:InputStream,方法:read(),close()
输出流:OutputStream,方法:write(),flush(),close()
2、字符流
输入流:Reader,方法:read(),close()
输出流:Writter,方法:write(),flush(),close()
操作
1、建立联系
2、选择流,ex: 文件输入流:Fileinputstream
3、操作
4、释放资源
字节流(可处理一切文件)
/** 文件读取 */ public class Demo1 { public static void main(String[] args) { //1.建立联系 File src = new File("D:/Program Files/Test/test.txt"); //2.选择流 InputStream is =null; //提升作用域 try { is = new FileInputStream(src); //3.操作不断读取 byte[] car = new byte[10]; int len=0; //接收实际读取大小 //循环读取 while (-1!= (len=is.read(car))){ //-1表示没有数据了 String info = new String(car, 0, len); System.out.println(info); } } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("File does not exist!"); } catch (IOException e) { e.printStackTrace(); System.out.println("Fail to read the file!"); } finally { //4.释放资源 if (null != is){ try { is.close(); } catch (IOException e) { System.out.println("File to close FileInputStream!"); } } } } }
import java.io.*; /** * 文件写入 */ public class Demo02 { public static void main(String[] args) { //1.建立联系 File dest = new File("D:/Program Files/Test/test.txt"); //2.选择流,文件输出流 OutputStream os = null; //3.操作,以追加形式写出文件 try{ os = new FileOutputStream(dest, true); //这里的ture表示在一个文件追加,如果改成false,则会覆盖文件。 String str = "This is a test string"; //字符串转字节数组 byte[] data = str.getBytes(); os.write(data,0,data.length); os.flush(); //强制刷新 } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("找不到文件!"); } catch (IOException e) { e.printStackTrace(); System.out.println("文件写出失败!"); }finally { //4.释放资源 try { os.close(); } catch (IOException e) { System.out.println("关闭输出流失败!"); } } } }
import java.io.*; /** * 文件拷贝 */ public class FileUtil { public static void copyFile(String srcPath, String destPath)throws IOException { //1. 建立联系 + 目的地(文件可以不存在) copyFile(new File(srcPath), new File(destPath)); } public static void copyFile(File src, File dest)throws IOException { if (!src.isFile()){ System.out.println("只能拷贝文件!"); throw new IOException("只能拷贝文件"); } //2. 选择流 InputStream is = new FileInputStream(src); OutputStream os = new FileOutputStream(dest); //3. 文件拷贝, 循环 + 读取 + 写出 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(); //先打开的后关闭 } }
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; /** * 文件夹的拷贝 * 1. 复制, copy file * 2. 创建, mkdirs() * 3. 递归查找子集 */ public class DirCopyDemo { /** * 拷贝文件夹 * @param srcPath 源路径 * @param destPath 目标路径 */ public static void copyDir (String srcPath, String destPath){ File src = new File(srcPath); File dest = new File(destPath); copyDir(src, dest); } public static void copyDir(File src, File dest){ if (src.isDirectory()){ dest = new File(dest, src.getName()); //先在目标路径创建一个名字相同的文件夹,后面再调用方法,把子集全部拷贝过去 } if (src.isFile()){ try{ FileUtil.copyFile(src, dest); }catch (FileNotFoundException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } } else if (src.isDirectory()){ //是文件夹 //确保文件夹存在 dest.mkdirs(); //获取下一级目录 | 文件 for (File sub:src.listFiles()){ copyDir(sub, new File(dest, sub.getName())); } } } public static void main(String[] args) { //原目录 String srcPath = "D:/Program Files/Test01"; //目标目录 String destPath = "D:/Program Files/Test02"; copyDir(srcPath, destPath); } }
字符流(只能处理纯文本,全部为可见字符)
import java.io.*; /** 文件读取 */ public class Demo1 { public static void main(String[] args) { //1.建立联系 File src = new File("D:/Program Files/Test/test.txt"); //2.选择流 Reader reader = null; try { reader = new FileReader(src); //3.操作不断读取 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("源文件不存在!"); } catch (IOException e) { e.printStackTrace(); System.out.println("文件读取失败!"); } finally { //4.释放资源 if (null != reader){ try { reader.close(); } catch (IOException e) { System.out.println("字符流关闭失败!"); } } } } }
其他写法也与字节流类似,只是所用的流不同。
四、处理流
处理流:对其他流的处理,增强性能,效率。
1、字节缓冲流
- BufferedInputStream
- BufferedOutputStream
2、字符缓冲流
- BufferedReader: 新方法:readLine()
- BufferedWriter: 新方法:newLine()
//日常使用建议加上缓冲流增加效率 BufferedReader br = null; br = new BufferedReader(new FileReader(src)); String line = null; //循环读取 while (null!= (line=br.readLine())){ System.out.println(line); }
五、转换流
字节流转为字符流,主要用于处理乱码 (编码集,解码集)
- 编码:字符(人类只认字符)--- 编码字符集 ---> 二进制(计算机只认二进制)
- 解码:二进制 --- 编码字符集 ---> 字符
为什么会出现乱码?
- 编码与解码的字符集不统一 (ex: 一个GBK,一个UTF-8)
- 字节缺少,长度丢失
import java.io.*; public class ConvertDemo { public static void main(String[] args) throws IOException { //解码byte ---> char,默认gbk String str = "中国"; //编码char ---> byte byte[] data = str.getBytes(); //编码与解码字符集统一 System.out.println(new String(data)); //打印成功,无乱码 System.out.println(new String(data, 0, 3)); //字节数不完整,出现乱码 //设定编码集 data = str.getBytes("UTF-8"); System.out.println(new String(data)); //编码与解码集不统一,打印出现乱码 /** * 转换流:字节转为字符 * 1、输出流:OutputStreamWriter 编码 * 2、输入流:InputStreamReader 解码 */ BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream( new File("D:/Program Files/TEST/test.txt")),"UTF-8")); } }
浙公网安备 33010602011771号