input和output
流的本质是数据传输
File类构造方法
File f1 = new File("E://java_study//javaMysql"); System.out.println(f1); File f2 = new File("E://java_study","javaMysql"); System.out.println(f2); File f3 = new File("E://java_study"); File f4 = new File(f3, "javaMysql"); System.out.println(f4);
创建功能
// 创建文件 File f1 = new File("D://java_study//html.txt"); System.out.println(f1.createNewFile()); // 创建一级目录 File f2 = new File("D://java_study//ddddd"); System.out.println(f2.mkdir()); // 创建多级目录 File f3 = new File("D://java_study//mysqlhh//zheshiyi"); System.out.println(f3.mkdirs());
路径
File f2 = new File("D://java_study//ddddd"); // 绝对路径 File f3 = new File("Student//html.txt"); // 相对路径 System.out.println(f3.createNewFile());
删除
// 删除路径 (目录和文件一样方法) System.out.println(f3.delete()); // 注意:删除要一层一层的删,先删父文件里的子文件(或内容),才能删父文件
常用方法
File f = new File("D:\java_study\Student\javaa.txt"); System.out.println(f.createNewFile()); System.out.println(f.isDirectory()); // 判断是否是目录 System.out.println(f.isFile()); // 判断是否是文件 System.out.println(f.exists()); System.out.println(f.getAbsoluteFile()); // D:java_studyStudentjavaa.txt 返回绝对路径 System.out.println(f.getPath()); // D:java_studyStudentjavaa.txt 返回抽象路径,就是上面定义的 System.out.println(f.getName()); // java.txt File f2 = new File("D:\java_Study\ddddd"); String[] list = f2.list(); for (String list1 : list) { System.out.println(list1); } File[] files = f2.listFiles(); for (File f1 : files) { if (f1.isFile()) { System.out.println(f1.getName()); } } // html.txt
遍历目录 得到文件,用到递归
public static void main(String[] args) { File f = new File("D:\java_study"); getFilePath(f); } public static void getFilePath(File f) { File[] f1 = f.listFiles(); if (f1 != null) { for (File f2 : f1) { if (f2.isDirectory()) { getFilePath(f2); } else { System.out.println(f2.getAbsoluteFile()); } } } }字节流 写入数据方法
FileOutputStream fos = new FileOutputStream("D:\java_study\Student\outPutStream.txt"); // 相当于FileOutputStream fos = new FileOutputStream(new File("D:\java_study\Student\outPutStream.txt")); // 写一个 fos.write(97); // 输出a // 写入字节数组 byte[] bys = {97,98,99,100,101}; fos.write(bys); // abcde // 用String转换 byte[] byss = "abcde".getBytes(); fos.write(byss); // abcde // 从off的索引开始,写len个数据,写局部数据 fos.write(bys, 1, 3); // bcd // 释放资源(相当于关闭) fos.close();追加数据和换行
// 要追加元素,就要在后面加True FileOutputStream fos = new FileOutputStream("D:\java_study\Student\outPutStream.txt", true); for (int i = 0; i < 10; i++) { fos.write("hello".getBytes()); // window换行用rn fos.write("rn".getBytes()); } fos.close();try…catch…finally处理异常
FileOutputStream fos = null; try { fos = new FileOutputStream("D:\java_study\Student\outPutStream.txt", true); fos.write("hello".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { // 写释放资源的 *** 作 try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } }字节流读数据
一次读一个
FileInputStream fis = new FileInputStream("D:\java_study\Student\outPutStream.txt"); int by; while ((by=fis.read()) != -1) { System.out.print((char)by); } fis.close();
一次读一个数组(推荐,配合缓冲流更快)
FileInputStream fis = new FileInputStream("D:\java_study\Student\outPutStream.txt"); byte[] bys = new byte[1024]; // 1024及其整数倍 int len; // 读取的个数 while ((len=fis.read(bys)) != -1) { System.out.println(new String(bys, 0, len)); } fis.close();案例
复制图片
FileInputStream fis = new FileInputStream("D:\美图\天堂旅行团1.png"); // 创建目的地对象 FileOutputStream fos = new FileOutputStream("D:\java_study\Student\天堂旅行团1.png"); // 复制图片 byte[] bys = new byte[1024]; int len; while ((len=fis.read(bys)) != -1) { fos.write(bys, 0, len); } // 释放资源 fis.close(); fos.close();字符缓冲输入和输出流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\美图\天堂旅行团1.png")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\java_study\Student\天堂旅行团1.png"));字符流
中文在GBK中占2个字节,在UTF-8中占3个字节
编码和解码要用同一个字符集
编码和解码String s = "中国"; // 编码 byte[] bys = s.getBytes(); // 默认是用UTF-8 byte[] bys1 = s.getBytes("GBK"); System.out.println(Arrays.toString(bys)); // [-28, -72, -83, -27, -101, -67] System.out.println(Arrays.toString(bys1)); // [-42, -48, -71, -6] // 解码 String ss = new String(bys); System.out.println(ss); // 中国 String sss = new String(bys1, "GBK"); System.out.println(sss); // 中国
方法
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\java_study\Student\outPutStream.txt"), "UTF-8"); osw.write("中国"); osw.close(); // 先关闭,不然下面输出不了 InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\java_study\Student\outPutStream.txt"), "UTF-8"); // 读取数据 int ch; while ((ch = isr.read()) != -1) { System.out.print((char) ch); } isr.close();
close方法是先刷新,在关闭
写数据的方法(5种)OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\java_study\Student\outPutStream.txt"), "UTF-8"); osw.write(97); osw.flush(); // 不同于osw.close();没有关闭 char[] ch = {'a', 'b', 'c', 'd', 'e'}; osw.write(ch); osw.write(ch, 1, 3); // bcd osw.write("abcde"); osw.write("abcde", 1, "avcde".length()); osw.close();
读数据参考字节流,只不过将byte改为了char而已,2中方法参考
简写方式(常用)简写的方式不能定义字符集,只能用默认的字符集,如要要修改字符集,还是要用全称
简写是全称的子类,可以调用父亲的方法
FileWriter fw = new FileWriter("D:\java_study\Student\outPutStream.txt"); FileReader fr = new FileReader("D:\java_study\Student\outPutStream.txt");字符缓冲流
也可以参照字节缓冲流的用法
特有功能
BufferedReader br = new BufferedReader(fr); fr.readLine // 只读一行的字符串 BufferedWriter bw = new BufferedWriter(fw); fw.newline // 换行,相当于win的tn
常用复制文件方法(用特有功能)
String line; while ((line = br.readLine()) != null) { bw.write(line); bw.newline(); bw.flush(); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)