File类 构造方法 :
File(String pathname):根据一个路径得到File对象
File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
判断方法 :
public boolean isDirectory():判断是否是目录
public boolean isFile():判断是否是文件
public boolean exists():判断是否存在
public boolean canRead():判断是否可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是否隐藏
获取方法 :
public String getAbsolutePath():获取绝对路径
public String getPath():获取路径
public String getName():获取名称
public long length():获取长度。字节数
public long lastModified():获取最后一次的修改时间,毫秒值
public String[] list():获取指定目录下的所有文件或者文件夹的名称数组
public File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组
创建方法 :
public boolean createNewFile():创建文件 如果存在这样的文件,就不创建了
public boolean mkdir():创建文件夹 如果存在这样的文件夹,就不创建了
public boolean mkdirs():创建文件夹,如果父文件夹不存在,会帮你创建出来
文件名称过滤器方法:
public String[] list(FilenameFilter filter):返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中满足指定过滤器的文件和目录。
public File[] listFiles(FileFilter filter):返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import java.io.File;import java.io.FilenameFilter;public class Testing3 {public static void main (String[] args) { File dir = new File("F:/乱七八糟/360安全浏览器下载/chrome/下载/messing/小说" ); String[] arr = dir.list(new FilenameFilter(){ @Override public boolean accept (File dir, String name) { File f = new File(dir,name); return f.isFile() && f.getName().endsWith(".epub" ); } }); for (String s :arr){ System.out.println(s); } } }
重命名和删除方法 :
public boolean renameTo(File dest):把文件重命名为指定的文件路径
public boolean delete():删除文件或者文件夹
重命名注意事项
1 2 3 4 File file1 = new File("xxx.txt" ); File file2 = new File("ooo.txt" ); file1.renameTo(file2);
如果路径名相同,就是改名。
如果路径名不同,就是将文件剪切到指定位置并改名 。
删除注意事项:
Java中的删除不走回收站。
要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹。
IO流
FileInputStream.read()方法就只会读取一个字节
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class Demo1_FileInputStream { public static void main (String[] args) throws IOException { FileInputStream fis = new FileInputStream("xxx.txt" ); int x = fis.read(); System.out.println(x); int y = fis.read(); System.out.println(y); int z = fis.read(); System.out.println(z); fis.close(); } }
所以得出一般的读取文件的操作方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class Demo1_FileInputStream { public static void main (String[] args) throws IOException { FileInputStream fis = new FileInputStream("xxx.txt" ); int b; while ((b = fis.read()) != -1 ) { System.out.println(b); } fis.close(); } }
read()方法读取的是一个字节,为什么返回是int,而不是byte
因为字节输入流可以操作任意类型的文件,比如图片音频等,这些文件底层都是以二进制形式的存储的,
如果每次读取都返回byte,有可能在读到中间的时候遇到111111111,那么这11111111是byte类型的-1,我们的程序是遇到-1就会停止不读了,后面的数据就读不到了,
所以在读取的时候用int类型接收,如果11111111会在其前面补上24个0凑足4个字节,那么byte类型的-1就变成int类型的255了这样可以保证整个数据读完,而结束标记的-1就是int类型
FileOutputStream write()一次写出一个字节
FileOutputStream在创建对象的时候是如果没有这个文件会帮我创建出来,如果有这个文件就会先将文件清空,如果想追加 , 就添加一个true参数:
1 FileOutputStream fos = new FileOutputStream("yyy.txt" ,true );
1 2 3 4 5 6 7 8 9 public class Demo2_FileOutputStream { public static void main (String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("yyy.txt" ); fos.write(97 ); fos.write(98 ); fos.write(99 ); fos.close(); } }
方法 :
1 FileOutputStream fos = new FileOutputStream("yyy.txt" );
fos.available() : 文件的剩余字节数
fis.read(arr) / fos.write(arr)
1 2 3 byte [] arr = new byte [fis.available()]; fis.read(arr); fos.write(arr);
实际上不推荐上面的做法 , 因为有可能会导致内存溢出
最好的读取写入方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static void main (String[] args) throws IOException { FileInputStream fis = new FileInputStream("致青春.mp3" ); FileOutputStream fos = new FileOutputStream("copy.mp3" ); byte [] arr = new byte [1024 * 8 ]; int len; while ((len = fis.read(arr)) != -1 ) { fos.write(arr,0 ,len); } fis.close(); fos.close(); }
fos.write(b,offset,len)
:
b : 数据
offset : 数据中的起始偏移量
len : 要写入的字节数
字节缓冲区流
1 2 3 4 5 6 7 8 9 10 11 12 public static void main (String[] args) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("致青春.mp3" )); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.mp3" )); int b; while ((b = bis.read()) != -1 ) { bos.write(b); } bis.close(); bos.close(); }
BufferedInputStream
BufferedInputStream内置了一个缓冲区(数组)
从BufferedInputStream中读取一个字节时
BufferedInputStream会一次性从文件中读取8192个, 存在缓冲区中, 返回给程序一个
程序再次读取时, 就不用找文件了, 直接从缓冲区中获取
直到缓冲区中所有的都被使用过, 才重新从文件中读取8192个
BufferedOutputStream
BufferedOutputStream也内置了一个缓冲区(数组)
程序向流中写出字节时, 不会直接写到文件, 先写到缓冲区中
直到缓冲区写满, BufferedOutputStream才会把缓冲区中的数据一次性写到文件里
内存的运算效率比硬盘要高的多,所以只要降低硬盘的读写次数就会提高效率
小数组的读写和带Buffered的读取哪个更快?
定义小数组如果是8192个字节大小和Buffered比较的话定义小数组会略胜一筹,
因为读和写操作的是同一个数组 , 而Buffered操作的是两个数组
close()的作用
一定要手动调用close()方法
原因:
节省资源
close()会自动调用flush().防止因为缓冲区导致拷贝不完全 .
字节流读写中文
文件异常处理的标准方式 JDK1.6版本及之前
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public static void demo1 () throws FileNotFoundException, IOException { FileInputStream fis = null ; FileOutputStream fos = null ; try { fis = new FileInputStream("xxx.txt" ); fos = new FileOutputStream("yyy.txt" ); int b; while ((b = fis.read()) != -1 ) { fos.write(b); } }finally { try { if (fis != null ) fis.close(); }finally { if (fos != null ) fos.close(); } } }
JDK1.7版本及以后
1 2 3 4 5 6 7 8 9 10 11 public static void main (String[] args) throws IOException { try ( FileInputStream fis = new FileInputStream("xxx.txt" ); FileOutputStream fos = new FileOutputStream("yyy.txt" ); ){ int b; while ((b = fis.read()) != -1 ) { fos.write(b); } } }
类似于python的with语句
try()括号里面的定义变量,在执行完{}里面的语句后会自动调用关闭方法
python与java的对比:
1 2 3 4 5 try ( 变量B = 语句A; ){ 语句C; }
FileReader 和 FileWriter 1 2 3 4 5 6 7 8 9 10 11 12 public class Demo1_FileReader { public static void main (String[] args) throws IOException { FileReader fr = new FileReader("xxx.txt" ); int c; while ((c = fr.read()) != -1 ) { System.out.print((char )c); } fr.close(); } }
1 2 3 4 5 6 7 8 9 public class Demo2_FileWriter { public static void main (String[] args) throws IOException { FileWriter fw = new FileWriter("yyy.txt" ); fw.write("大家好!!!" ); fw.write(97 ); fw.close(); } }
注意
FileWriter自带一个大小为2k的缓冲区
所以一定要调用fw.close();
来flush
Write方法的重载
public void write(int c) throws IOException
: 往FileWriter写入单个字符c。
public void write(char [] c, int offset, int len)
: 写入字符数组中开始为offset长度为len的某一部分。
public void write(String s, int offset, int len)
: 写入字符串中开始为offset长度为len的某一部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Demo3_Copy { public static void main (String[] args) throws IOException { FileReader fr = new FileReader("xxx.txt" ); FileWriter fw = new FileWriter("yyy.txt" ); char [] arr = new char [1024 ]; int len; while ((len = fr.read(arr)) != -1 ) { fw.write(arr,0 ,len); } fr.close(); fw.close(); } }
什么情况下使用字符流
程序需要拷贝
一段文本, 不推荐使用字符流. 因为读取时会把字节转为字符, 写出时还要把字符转回字节.
程序需要读取
一段文本, 或者需要写出
一段文本的时候可以使用字符流
读取的时候是按照字符的大小读取的,不会出现半个中文
写出的时候可以直接将字符串写出,不用转换为字节数组
注意:
字符流不可以拷贝非纯文本的文件
因为在读的时候会将字节转换为字符,在转换过程中,可能找不到对应的字符,就会用?
代替,写出的时候会将字符转换成字节写出去
如果是?
,直接写出,这样写出之后的文件就乱了,看不了了
修改编码表 FileReader / FileWriter是使用默认码表读取/写入文件, 如果需要使用指定码表读取/写入, 那么可以使用InputStreamReader(字节流,编码表)
和OutputStreamWriter(字节流,编码表)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class Demo7_TransIO { public static void main (String[] args) throws FileNotFoundException, IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream("utf-8.txt" ), "uTf-8" ); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt" ), "gbk" ); int c; while ((c = isr.read()) != -1 ) { osw.write(c); } isr.close(); osw.close(); } }
或者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Demo7_TransIO { public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("utf-8.txt" ), "utf-8" )); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("gbk.txt" ), "gbk" )); int c; while ((c = br.read()) != -1 ) { bw.write(c); } br.close(); bw.close(); } }
BufferedReader 和 BufferedWriter 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class Demo3_Copy { public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("xxx.txt" )); BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt" )); int c; while ((c = br.read()) != -1 ) { bw.write(c); } br.close(); bw.close(); } }
BufferedReader.readLine() 和 BufferedWriter.newLine()
BufferedReader的readLine()方法可以读取一行字符(不包含换行符号)
BufferedWriter的newLine()可以输出一个跨平台 的换行符号”\r\n”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Demo4_Buffered { public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("zzz.txt" )); BufferedWriter bw = new BufferedWriter(new FileWriter("aaa.txt" )); String line; while ((line = br.readLine()) != null ) { bw.write(line); bw.newLine(); } br.close(); bw.close(); } }
将一个文本文档上的文本反转,第一行和倒数第一行交换,第二行和倒数第二行交换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;public class Testing { public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("zzz.txt" )); BufferedWriter bw = new BufferedWriter(new FileWriter("aaa.txt" )); ArrayList<String> list = new ArrayList<>(); String line; while ((line = br.readLine()) != null ) { list.add(line); bw.newLine(); } for (int i= list.size()-1 ; i>=0 ; i--) { bw.write(list.get(i)); bw.newLine(); } br.close(); bw.close(); } }
LineNumberReader LineNumberReader是BufferedReader的子类 , 具有相同的功能, 并且可以统计行号
调用getLineNumber()方法可以获取当前行号
调用setLineNumber()方法可以设置当前行号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.LineNumberReader;public class Demo5_LineNumberReader { public static void main (String[] args) throws IOException { LineNumberReader lnr = new LineNumberReader(new FileReader("zzz.txt" )); String line; lnr.setLineNumber(100 ); while ((line = lnr.readLine()) != null ) { System.out.println(lnr.getLineNumber() + ":" + line); } lnr.close(); } }
什么是序列流
序列流可以把多个字节输入流整合成一个 ,
从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推 .
不使用序列流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class Demo01_SequenceInputStream { public static void main (String[] args) throws FileNotFoundException, IOException { FileInputStream fis1 = new FileInputStream("a.txt" ); FileOutputStream fos = new FileOutputStream("c.txt" ); int b1; while ((b1 = fis1.read()) != -1 ) { fos.write(b1); } fis1.close(); FileInputStream fis2 = new FileInputStream("b.txt" ); int b2; while ((b2 = fis2.read()) != -1 ) { fos.write(b2); } fis2.close(); fos.close(); } }
使用方式:整合两个: SequenceInputStream(InputStream, InputStream)
1 2 3 4 FileInputStream fis1 = new FileInputStream("a.txt" ); FileInputStream fis2 = new FileInputStream("b.txt" ); SequenceInputStream sis = new SequenceInputStream(fis1, fis2); FileOutputStream fos = new FileOutputStream("c.txt" );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Demo01_SequenceInputStream { public static void main (String[] args) throws FileNotFoundException,IOException { FileInputStream fis1 = new FileInputStream("a.txt" ); FileInputStream fis2 = new FileInputStream("b.txt" ); SequenceInputStream sis = new SequenceInputStream(fis1, fis2); FileOutputStream fos = new FileOutputStream("c.txt" ); int b; while ((b = sis.read()) != -1 ) { fos.write(b); } sis.close(); fos.close(); } }
注意 : SequenceInputStream(InputStream, InputStream)
已经限死了两个参数 ,如果要合并多个流 , 就要使用SequenceInputStream(Enumeration<? extends InputStream> e)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class Demo01_SequenceInputStream { public static void main (String[] args) throws IOException { FileInputStream fis1 = new FileInputStream("a.txt" ); FileInputStream fis2 = new FileInputStream("b.txt" ); FileInputStream fis3 = new FileInputStream("c.txt" ); Vector<FileInputStream> v = new Vector<>(); v.add(fis1); v.add(fis2); v.add(fis3); Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("d.txt" ); int b; while ((b = sis.read()) != -1 ) { fos.write(b); } sis.close(); fos.close(); } }
ByteArrayOutputStream 什么是内存输出流 : 该输出流可以向内存中写数据, 把内存当作一个缓冲区 , 写出之后可以一次性获取出所有数据
此类实现了一个输出流,其中的数据被写入一个 byte数组。缓冲区会随着数据的不断写入而自动增长。可使用toByteArray()和toString()获取数据。
使用方式
创建对象: new ByteArrayOutputStream()
写出数据: write(int), write(byte[])
获取数据: toByteArray()
因为ByteArrayOutputStream对接的是内存 , 内存用完直接就释放了 , 没有对接硬盘 .所以ByteArrayOutputStream不需要关闭 .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Demo02_ByteArrayOutputStream { public static void main (String[] args) throws IOException { FileInputStream fis = new FileInputStream("e.txt" ); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int b; while ((b = fis.read()) != -1 ) { baos.write(b); } System.out.println(baos.toString()); fis.close(); } }
ObjecOutputStream 什么是对象操作流 : 该流可以将一个对象写出, 或者读取一个对象到程序中. 也就是执行了序列化和反序列化 的操作.
使用方式
写出: new ObjectOutputStream(OutputStream), writeObject()
读取: new ObjectInputStream(InputStream), readObject()
1 2 3 4 5 6 7 8 9 10 11 12 13 public class Demo03_ObjectOutputStream { public static void main (String[] args) throws IOException, FileNotFoundException { Person p1 = new Person("张三" , 23 ); Person p2 = new Person("李四" , 24 ); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt" )); oos.writeObject(p1); oos.writeObject(p2); oos.close(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Demo04_ObjectInputStream { public static void main (String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt" )); Person p1 = (Person) ois.readObject(); Person p2 = (Person) ois.readObject(); System.out.println(p1); System.out.println(p2); ois.close(); } }
注意 : Person类必须实现Serializable接口
一次性写入/读取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class Demo03_ObjectOutputStream { public static void main (String[] args) throws IOException { Person p1 = new Person("张三" , 23 ); Person p2 = new Person("李四" , 24 ); Person p3 = new Person("王五" , 25 ); Person p4 = new Person("赵六" , 26 ); ArrayList<Person> list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt" )); oos.writeObject(list); oos.close(); } }
1 2 3 4 5 6 7 8 9 10 11 12 public class Demo04_ObjectInputStream { public static void main (String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt" )); ArrayList<Person> list = (ArrayList<Person>) ois.readObject(); for (Person person : list) { System.out.println(person); } ois.close(); } }
PrintStream
什么是打印流 : 该流可以很方便的将对象的toString()结果输出, 并且自动加上换行, 而且可以使用自动刷出的模式
System.out就是一个PrintStream, 其默认向控制台输出信息
System.out.println(a);
如果是Null,直接打印Null
如果不是Null,调用a.toString()方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Demo05_PrintStream { public static void main (String[] args) throws IOException { System.out.println("aaa" ); PrintStream ps = System.out; ps.println(97 ); ps.write(97 ); Person p1 = new Person("张三" , 23 ); ps.println(p1); Person p2 = null ; ps.println(p2); ps.close(); } }
修改标准输入 1 2 System.setIn(new FileInputStream("IO图片.png" )); System.setOut(new PrintStream("copy.png" ));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Demo06_SystemInOut { public static void main (String[] args) throws IOException { System.setIn(new FileInputStream("a.txt" )); System.setOut(new PrintStream("b.txt" )); InputStream is = System.in; PrintStream ps = System.out; int b; while ((b = is.read()) != -1 ) { ps.write(b); } is.close(); ps.close(); } }
两种键盘录入 1 2 3 4 5 6 7 8 9 10 11 12 13 import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner;public class Demo07_SystemIn { public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); System.out.println(line); br.close(); } }
1 2 3 4 5 6 7 8 public class Demo07_SystemIn { public static void main (String[] args) throws IOException { Scanner sc = new Scanner(System.in); String line = sc.nextLine(); System.out.println(line); sc.close(); } }
RandomAccessFile
随机访问流概述
RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
支持对随机访问文件的读取和写入 。
read(),write(),seek()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;public class Demo08_RandomAccessFile { public static void main (String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile("g.txt" , "rw" ); raf.write(97 ); int x = raf.read(); System.out.println(x); raf.seek(0 ); raf.write(98 ); raf.close(); } }
什么是数据输入输出流
DataInputStream, DataOutputStream可以按照基本数据类型大小读写数据
例如按Long大小写出一个数字, 写出时该数据占8字节. 读取的时候也可以按照Long类型读取, 一次读取8个字节.
使用方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 DataOutputStream(OutputStream), writeInt(), writeLong() public class Demo09_Data { public static void main (String[] args) throws FileNotFoundException, IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("h.txt" )); dos.writeInt(997 ); dos.writeInt(998 ); dos.writeInt(999 ); dos.close(); } } public class Demo09_Data { public static void main (String[] args) throws FileNotFoundException, IOException { DataInputStream dis = new DataInputStream(new FileInputStream("h.txt" )); int x = dis.readInt(); int y = dis.readInt(); int z = dis.readInt(); System.out.println(x); System.out.println(y); System.out.println(z); dis.close(); } }
这个流实际上用的非常的少
Properties Properties的概述
Properties类是Hashtable的子类。
属性列表中每个键及其对应值都是一个字符串 。 Properties没有泛型。
Properties 类表示了一个持久的属性集 。
Properties 可保存在流中或从流中加载。
经常使用Properties将配置文件读取到一个字典中 。
功能:
public Object setProperty(String key,String value)
public String getProperty(String key)
public Enumeration<String> stringPropertyNames()
prop.load() : 读取文件到Map中
prop.store : 写入到文件中
1 2 3 4 5 6 7 public class Demo10_Properties { public static void main (String[] args) { Properties prop = new Properties(); prop.put("abc" , 123 ); System.out.println(prop); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.util.Enumeration;import java.util.Properties;public class Testing4 { public static void main (String[] args) { Properties prop = new Properties(); prop.setProperty("name" , "张三" ); prop.setProperty("tel" , "18912345678" ); System.out.println(prop); Enumeration<String> en = (Enumeration<String>) prop.propertyNames(); while (en.hasMoreElements()) { String key = en.nextElement(); String value = prop.getProperty(key); System.out.println(key + "=" + value); } } }
1 2 3 4 5 6 7 8 9 public class Demo10_Properties { public static void main (String[] args) throws FileNotFoundException, IOException { Properties prop = new Properties(); prop.load(new FileInputStream("config.properties" )); prop.setProperty("tel" , "18912345678" ); prop.store(new FileOutputStream("config.properties" ), null ); System.out.println(prop); } }
练习 File类递归练习(统计该文件夹大小) 从键盘接收一个文件夹路径,统计该文件夹大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import java.io.File;import java.util.Scanner;public class Test1 { public static void main (String[] args) { File dir = getDir(); System.out.println(getFileLength(dir)); } public static File getDir () { Scanner sc = new Scanner(System.in); System.out.println("请输入一个文件夹路径:" ); while (true ) { String line = sc.nextLine(); File dir = new File(line); if (!dir.exists()) { System.out.println("您录入的文件夹路径不存在,请输入一个文件夹路径:" ); }else if (dir.isFile()) { System.out.println("您录入的是文件路径,请输入一个文件夹路径:" ); }else { return dir; } } } public static long getFileLength (File dir) { long len = 0 ; File[] subFiles = dir.listFiles(); for (File subFile : subFiles) { if (subFile.isFile()) { len = len + subFile.length(); }else { len = len + getFileLength(subFile); } } return len; } }
File类递归练习(删除该文件夹) 从键盘接收一个文件夹路径,删除该文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import java.io.File;public class Test2 { public static void main (String[] args) { File dir = Test1.getDir(); deleteFile(dir); } public static void deleteFile (File dir) { File[] subFiles = dir.listFiles(); for (File subFile : subFiles) { if (subFile.isFile()) { subFile.delete(); }else { deleteFile(subFile); } } dir.delete(); } }
File类递归练习(拷贝) 从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class Test3 { public static void main (String[] args) throws IOException { File src = Test1.getDir(); File dest = Test1.getDir(); if (src.equals(dest)) { System.out.println("目标文件夹是源文件夹的子文件夹" ); }else { copy(src,dest); } } public static void copy (File src, File dest) throws IOException { File newDir = new File(dest, src.getName()); newDir.mkdir(); File[] subFiles = src.listFiles(); for (File subFile : subFiles) { if (subFile.isFile()) { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(newDir,subFile.getName()))); int b; while ((b = bis.read()) != -1 ) { bos.write(b); } bis.close(); bos.close(); }else { copy(subFile,newDir); } } } }
File类递归练习(按层级打印) 从键盘接收一个文件夹路径,把文件夹中的所有文件以及文件夹的名字按层级打印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import java.io.File;public class Test4 { public static void main (String[] args) { File dir = Test1.getDir(); printLev(dir,0 ); } public static void printLev (File dir,int lev) { File[] subFiles = dir.listFiles(); for (File subFile : subFiles) { for (int i = 0 ; i <= lev; i++) { System.out.print("\t" ); } System.out.println(subFile); if (subFile.isDirectory()) { printLev(subFile,++lev); } } } }
斐波那契数列 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class Test5 { public static void main (String[] args) { System.out.println(fun(8 )); } public static void demo1 () { int [] arr = new int [8 ]; arr[0 ] = 1 ; arr[1 ] = 1 ; for (int i = 2 ; i < arr.length; i++) { arr[i] = arr[i - 2 ] + arr[i - 1 ]; } System.out.println(arr[arr.length - 1 ]); } public static int fun (int num) { if (num == 1 || num == 2 ) { return 1 ; }else { return fun(num - 2 ) + fun(num - 1 ); } } }