|
@@ -2,8 +2,7 @@ package com.sf.ioioioioi;
|
|
|
|
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
|
@@ -124,9 +123,224 @@ public class test01 {
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
- public void t9(){
|
|
|
|
|
|
+ public void t9() throws IOException {
|
|
|
|
+ //利用File构造器,new一个文件目录file
|
|
|
|
+ // 在其中创建 当前日期目录 之后创建helloworld.java文件
|
|
|
|
+ //编写方法,实现删除file中指定文件的操作
|
|
|
|
+ File file = new File("D:\\file");
|
|
|
|
+ boolean mkdirs = file.mkdirs();
|
|
|
|
+ if (mkdirs) {
|
|
|
|
+ File file1 = new File("D:\\file\\a.java");
|
|
|
|
+ file1.createNewFile();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 判断指定目录下是否有后缀名为.jpg的文件。如果有,就输出该文件名称
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void t10(){
|
|
|
|
+ File srcFile = new File("d:\\demo");
|
|
|
|
+ String[] fileNames = srcFile.list();
|
|
|
|
+ for(String fileName : fileNames){
|
|
|
|
+ if(fileName.endsWith(".jpg")){
|
|
|
|
+ System.out.println(fileName);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 方法二
|
|
|
|
+ * 判断指定目录下是否有后缀名为.jpg的文件。如果有,就输出该文件名称
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void t11(){
|
|
|
|
+ File srcFile = new File("d:\\demo");
|
|
|
|
+ File[] listFiles = srcFile.listFiles();
|
|
|
|
+ for(File file : listFiles){
|
|
|
|
+ if(file.getName().endsWith(".jpg")){
|
|
|
|
+ System.out.println(file.getAbsolutePath());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ *判断指定目录下是否有后缀名为.jpg的文件。如果有,就输出该文件名称
|
|
|
|
+ * * File类提供了两个文件过滤器方法
|
|
|
|
+ * * public String[] list(FilenameFilter filter)
|
|
|
|
+ * * public File[] listFiles(FileFilter filter)
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void t12(){
|
|
|
|
+ File file = new File("d:\\demo");
|
|
|
|
+ File[] files = file.listFiles(new FilenameFilter() {
|
|
|
|
+ @Override
|
|
|
|
+ public boolean accept(File dir, String name) {
|
|
|
|
+ return name.endsWith(".jsp");
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ for (File file1 : files) {
|
|
|
|
+ System.out.println(file1.getAbsolutePath());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 练习3:遍历指定目录所有文件名称,包括子文件目录中的文件。
|
|
|
|
+ * 拓展1:并计算指定目录占用空间的大小
|
|
|
|
+ * 拓展2:删除指定文件目录及其下的所有文件
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void t13(){
|
|
|
|
+ /**
|
|
|
|
+ * 代码在另外一个类(ListFilesTest)中的主函数中
|
|
|
|
+ */
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 读取hello.txt文件中的字符数据,并显示在控制台上
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void t14() throws IOException {
|
|
|
|
+ // 1、创建file类的对象 对应着物理磁盘上的某个文件
|
|
|
|
+ File file = new File("d:\\a.txt");
|
|
|
|
+ //2 、创建fileReader对象 将file类的对象作为参数传递到fileRead的构造器中
|
|
|
|
+ FileReader fr = new FileReader(file);
|
|
|
|
+ //3、通过相关流的方法 读取文件中的数据
|
|
|
|
+ int data;
|
|
|
|
+ while ((data = fr.read()) !=-1){
|
|
|
|
+ System.out.println((char) data);
|
|
|
|
+ }
|
|
|
|
+ //4、关闭相关的流资源 避免出现内存泄露
|
|
|
|
+ fr.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 实现方法3 调用read(char[] cbuf),每次从文件中读取多个字符 FileReader --read()
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void t15() throws IOException {
|
|
|
|
+ FileReader fr = null;
|
|
|
|
+ try {
|
|
|
|
+ //1. 创建File类的对象,对应着物理磁盘上的某个文件
|
|
|
|
+ File file = new File("d:\\a.txt");
|
|
|
|
+ //2. 创建FileReader流对象,将File类的对象作为参数传递到FileReader的构造器中
|
|
|
|
+ fr = new FileReader(file);
|
|
|
|
+ //3. 通过相关流的方法,读取文件中的数据
|
|
|
|
+ char[] cbuf = new char[5];
|
|
|
|
+ /*
|
|
|
|
+ * read(char[] cbuf) : 每次将文件中的数据读入到cbuf数组中,并返回读入到数组中的
|
|
|
|
+ * 字符的个数。
|
|
|
|
+ * */
|
|
|
|
+ int len; //记录每次读入的字符的个数
|
|
|
|
+ while ((len = fr.read(cbuf)) != -1) {
|
|
|
|
+ //处理char[]数组即可
|
|
|
|
+ //正确:
|
|
|
|
+ String str = new String(cbuf, 0, len);
|
|
|
|
+ System.out.print(str);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ //4. 关闭相关的流资源,避免出现内存泄漏
|
|
|
|
+ if (fr != null)
|
|
|
|
+ fr.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void t16() throws IOException {
|
|
|
|
+ // 使用文件名称创建流对象
|
|
|
|
+ FileWriter fw = new FileWriter(new File("d:\\fw.txt"));
|
|
|
|
+ // 写出数据
|
|
|
|
+ fw.write(97); // 写出第1个字符
|
|
|
|
+ fw.write('b'); // 写出第2个字符
|
|
|
|
+ fw.write('C'); // 写出第3个字符
|
|
|
|
+ fw.write(30000); // 写出第4个字符,中文编码表中30000对应一个汉字。
|
|
|
|
+ //关闭资源
|
|
|
|
+ fw.close();
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void t17() throws IOException {
|
|
|
|
+ // 使用文件名称创建流对象
|
|
|
|
+ FileWriter fw = new FileWriter(new File("fw.txt"));
|
|
|
|
+ // 字符串转换为字节数组
|
|
|
|
+ char[] chars = "爱扣钉".toCharArray();
|
|
|
|
+ // 写出字符数组
|
|
|
|
+ fw.write(chars); // 爱扣钉
|
|
|
|
+ // 写出从索引1开始,2个字符。
|
|
|
|
+ fw.write(chars,1,2); // 钉钉
|
|
|
|
+ // 关闭资源
|
|
|
|
+ fw.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void t18() throws IOException {
|
|
|
|
+ FileWriter fw = null;
|
|
|
|
+ try {
|
|
|
|
+ //1. 创建File的对象
|
|
|
|
+ File file = new File("personinfo.txt");
|
|
|
|
+ //2. 创建FileWriter的对象,将File对象作为参数传递到FileWriter的构造器中
|
|
|
|
+ //如果输出的文件已存在,则会对现有的文件进行覆盖
|
|
|
|
+ fw = new FileWriter(file);
|
|
|
|
+// fw = new FileWriter(file,false);
|
|
|
|
+ //如果输出的文件已存在,则会在现有的文件末尾写入数据
|
|
|
|
+// fw = new FileWriter(file,true);
|
|
|
|
+
|
|
|
|
+ //3. 调用相关的方法,实现数据的写出操作
|
|
|
|
+ //write(String str) / write(char[] cbuf)
|
|
|
|
+ fw.write("hello world");
|
|
|
|
+ fw.write("world".toCharArray());
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ //4. 关闭资源,避免内存泄漏
|
|
|
|
+ try {
|
|
|
|
+ if (fw != null)
|
|
|
|
+ fw.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void t19() throws IOException {
|
|
|
|
+//复制文件 b.txt 到 upload下
|
|
|
|
+ FileReader fr = new FileReader("d:\\a.txt");
|
|
|
|
+ FileWriter fw = new FileWriter("d:\\b.txt",true);
|
|
|
|
+ //数组
|
|
|
|
+ char[] chars = new char[1024*8];
|
|
|
|
+
|
|
|
|
+ int len;
|
|
|
|
+ while ( (len=fr.read(chars)) != -1 ){
|
|
|
|
+ fw.write(chars,0,len);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //关闭
|
|
|
|
+ fw.close();
|
|
|
|
+ fr.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void t20() throws IOException {
|
|
|
|
+ // 使用文件名称创建流对象
|
|
|
|
+ FileWriter fw = new FileWriter("fw.txt");
|
|
|
|
+ // 写出数据,通过flush
|
|
|
|
+ fw.write('刷'); // 写出第1个字符
|
|
|
|
+ fw.flush();
|
|
|
|
+ fw.write('新'); // 继续写出第2个字符,写出成功
|
|
|
|
+ fw.flush();
|
|
|
|
+
|
|
|
|
+ // 写出数据,通过close
|
|
|
|
+ fw.write('关'); // 写出第1个字符
|
|
|
|
+ fw.close();
|
|
|
|
+ fw.write('闭'); // 继续写出第2个字符,【报错】java.io.IOException: Stream closed
|
|
|
|
+ fw.close();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|