|
@@ -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.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.out.println("System.getProperty(\"user.dir\") = "+System.getProperty("user.dir"));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println("file.getAbsolutePath() = "+file.getAbsolutePath());
|
|
|
+
|
|
|
+ System.out.println("file.getAbsoluteFile() = "+file.getAbsoluteFile());
|
|
|
+
|
|
|
+ System.out.println("file.length() = "+file.length());
|
|
|
+ System.out.println("file.lastModified() = "+file.lastModified());
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 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);
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+ while ((data = fileReader.read()) != -1){
|
|
|
+
|
|
|
+ System.out.print((char) data);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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){
|
|
|
+
|
|
|
+
|
|
|
+ String s = new String(chars,0,data);
|
|
|
+ System.out.println(s);
|
|
|
+
|
|
|
+ }
|
|
|
+ fileReader.close();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t10() throws IOException {
|
|
|
+ File file = new File("aa.txt");
|
|
|
+ FileWriter fw = new FileWriter(file,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",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 {
|
|
|
+
|
|
|
+ FileInputStream fis = new FileInputStream("jdbc.properties");
|
|
|
+ int data;
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ while ((data=fis.read(bytes)) != -1){
|
|
|
+
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+}
|