|
@@ -0,0 +1,274 @@
|
|
|
+package com.sf.javase.day17;
|
|
|
+
|
|
|
+import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * file操作和IO流
|
|
|
+ */
|
|
|
+public class T {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * file构造器操作 - 绝对路径
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t1(){
|
|
|
+ String pathName = "D:\\aaa.txt";
|
|
|
+ File file = new File(pathName);
|
|
|
+// System.out.println(file);
|
|
|
+// new File("D:\\aaa\\bbb.txt");
|
|
|
+ // 获取文件的名称(带后缀)
|
|
|
+ System.out.println("file.getName()="+file.getName());
|
|
|
+ // 获取文件的路径(相对或绝对)
|
|
|
+ System.out.println("file.getPath()="+file.getPath());
|
|
|
+ //获取文件的父路径
|
|
|
+ System.out.println("file.getParent()="+file.getParent());
|
|
|
+ // 获取文件的绝对路径
|
|
|
+ System.out.println("file.getAbsolutePath() = "+file.getAbsolutePath());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 相对路径
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t2(){
|
|
|
+ File file = new File("jdbc.properties");
|
|
|
+ // 获取文件的名称(带后缀)
|
|
|
+ System.out.println("file.getName()="+file.getName());
|
|
|
+ // 获取文件的路径(相对或绝对)
|
|
|
+ System.out.println("file.getPath()="+file.getPath());
|
|
|
+ //获取文件的父路径
|
|
|
+ System.out.println("file.getParent()="+file.getParent());
|
|
|
+ // System.getProperty("user.dir") = 当前项目的根目录
|
|
|
+ System.out.println("System.getProperty(\"user.dir\") = "+System.getProperty("user.dir"));
|
|
|
+ // 路径分隔符
|
|
|
+// File.separator
|
|
|
+ // 相对路径 获取文件的绝对路径 file.getAbsolutePath() = System.getProperty("user.dir") + file.getPath()
|
|
|
+ System.out.println("file.getAbsolutePath() = "+file.getAbsolutePath());
|
|
|
+ // 区别 返回值不同
|
|
|
+ System.out.println("file.getAbsoluteFile() = "+file.getAbsoluteFile());
|
|
|
+ // 获取文件的长度 ---字节数 18
|
|
|
+ System.out.println("file.length() = "+file.length());
|
|
|
+ System.out.println("file.lastModified() = "+file.lastModified());
|
|
|
+// long l = file.lastModified();
|
|
|
+// Date date = new Date(l);
|
|
|
+// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+// String format = simpleDateFormat.format(date);
|
|
|
+// System.out.println(format);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t3(){
|
|
|
+ File file = new File("d:\\");
|
|
|
+ String[] list = file.list(); // 获取文件下的所有子文件或文件夹(的名字)
|
|
|
+ File[] files = file.listFiles(); // 获取文件下的所有子文件或文件夹(的路径)
|
|
|
+ for (File file1 : files) {
|
|
|
+ System.out.println(file1);
|
|
|
+ }
|
|
|
+// for (String s : list) {
|
|
|
+// System.out.println(s);
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * file - 重命名
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t4() {
|
|
|
+ File file = new File("d:\\a.txt");
|
|
|
+ boolean b = file.renameTo(new File("d:\\b.txt"));
|
|
|
+ System.out.println(b);
|
|
|
+ // ps:重命名不会更改修改时间
|
|
|
+ System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(new File("d:\\b.txt").lastModified())));
|
|
|
+ System.out.println(file.exists());
|
|
|
+ System.out.println(file.isDirectory());
|
|
|
+ System.out.println(file.isHidden());
|
|
|
+ System.out.println(file.canWrite()); ///文件是否为只写文件
|
|
|
+ System.out.println(file.canRead()); ///文件是否为只读文件
|
|
|
+
|
|
|
+ File file1 = new File("d:\\b.txt");
|
|
|
+ System.out.println(file1.canWrite()); ///文件是否为可写文件
|
|
|
+ System.out.println(file1.canRead()); ///文件是否为可读文件
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * file 创建和删除
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t5() throws IOException {
|
|
|
+ File file = new File("d:\\java.txt");
|
|
|
+ boolean newFile = file.createNewFile();
|
|
|
+ System.out.println(newFile);
|
|
|
+ File fileDir = new File("d:\\aaa");
|
|
|
+ boolean mkdir = fileDir.mkdir();
|
|
|
+ System.out.println(mkdir);
|
|
|
+
|
|
|
+ File fileDirs = new File("d:\\aaa\\bbb\\ccc");
|
|
|
+ boolean mkdirs = fileDirs.mkdirs();
|
|
|
+ System.out.println(mkdirs);
|
|
|
+ File fileDel = new File("d:\\aaa\\bbb\\ccc");
|
|
|
+ boolean delete = fileDel.delete();
|
|
|
+ System.out.println(delete);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t6() throws IOException {
|
|
|
+ File file = new File("d:\\aaa\\bbb\\a.txt");
|
|
|
+ boolean newFile = file.createNewFile();
|
|
|
+ System.out.println(newFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * io操作
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t7() throws IOException {
|
|
|
+ File file = new File("jdbc.properties");
|
|
|
+ FileReader fileReader = new FileReader(file);
|
|
|
+ int data;
|
|
|
+// int count =0;
|
|
|
+ while ((data = fileReader.read()) != -1){
|
|
|
+// count++;
|
|
|
+ System.out.print((char) data);
|
|
|
+ }
|
|
|
+// System.out.println(count);
|
|
|
+ fileReader.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t8() {
|
|
|
+ File file = new File("jdbc.properties");
|
|
|
+ FileReader fileReader = null;
|
|
|
+ try {
|
|
|
+ fileReader = new FileReader(file);
|
|
|
+ int data;
|
|
|
+ while ((data = fileReader.read()) != -1){
|
|
|
+ System.out.print((char) data);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ try {
|
|
|
+ fileReader.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * char[]
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t9() throws IOException{
|
|
|
+ File file = new File("jdbc.properties");
|
|
|
+ FileReader fileReader = new FileReader(file);
|
|
|
+ char[] chars = new char[7];
|
|
|
+ int data; // 读入有效字符
|
|
|
+ while ((data = fileReader.read(chars)) != -1){
|
|
|
+// System.out.println(Arrays.toString(chars));
|
|
|
+// List<char[]> chars1 = Arrays.asList(chars);
|
|
|
+ String s = new String(chars,0,data);
|
|
|
+ System.out.println(s);
|
|
|
+// chars1.clear();
|
|
|
+ }
|
|
|
+ fileReader.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写出文件
|
|
|
+ @Test
|
|
|
+ public void t10() throws IOException {
|
|
|
+ File file = new File("aa.txt");
|
|
|
+ FileWriter fw = new FileWriter(file,true); // true 追加
|
|
|
+ // 写出数据
|
|
|
+ fw.write(97);
|
|
|
+ fw.write(134);
|
|
|
+ fw.write('c');
|
|
|
+ fw.write('f');
|
|
|
+ fw.write(30000);
|
|
|
+ fw.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t11() throws IOException {
|
|
|
+ File file = new File("f.txt");
|
|
|
+ FileWriter fileWriter = new FileWriter(file, true);
|
|
|
+// fileWriter.write("abcdef".toCharArray());
|
|
|
+// fileWriter.write("abcdef".toCharArray(),0,4);
|
|
|
+// fileWriter.write("abcdef");
|
|
|
+ fileWriter.write("abcdef",1,2);
|
|
|
+ fileWriter.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字节输入流 FileInputStream
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t12() throws IOException {
|
|
|
+ File file = new File("jdbc.properties");
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
+ // 开始读取数据
|
|
|
+ int data;
|
|
|
+ while ((data = fis.read()) != -1){
|
|
|
+ System.out.print((char) data);
|
|
|
+ }
|
|
|
+ fis.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t13() throws IOException {
|
|
|
+// File file = new File("jdbc.properties");
|
|
|
+ FileInputStream fis = new FileInputStream("jdbc.properties");
|
|
|
+ int data;
|
|
|
+ byte[] bytes = new byte[1024]; // 将数据承装在bytes中
|
|
|
+ while ((data=fis.read(bytes)) != -1){
|
|
|
+// System.out.println(Arrays.toString(bytes));
|
|
|
+ String str = new String(bytes, 0, data);
|
|
|
+ System.out.println(str);
|
|
|
+ }
|
|
|
+ fis.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t14() throws IOException {
|
|
|
+ FileOutputStream fos = new FileOutputStream("fos.txt");
|
|
|
+ fos.write(101);
|
|
|
+ fos.write(102);
|
|
|
+ fos.write(103);
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t15() throws IOException {
|
|
|
+ FileOutputStream fos = new FileOutputStream("fos.txt",true);
|
|
|
+ fos.write("abcsd".getBytes(),0,3);
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实现复制
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t16() throws IOException {
|
|
|
+ FileInputStream fis = new FileInputStream("fos.txt");
|
|
|
+ FileOutputStream fos = new FileOutputStream("fos_bak.txt");
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ int data;
|
|
|
+ while ((data = fis.read(bytes)) != -1){
|
|
|
+ fos.write(bytes,0,data);
|
|
|
+ }
|
|
|
+ System.out.println("success~");
|
|
|
+ fis.close();
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+}
|