File(String pathname); File(String parent, String child); File(File parent, String child);
File file1=new File("a.txt"); System.out.println(file1); File file2=new File("b.txt"); System.out.println(file2); File file3=new File("D:\\file\\c.txt"); System.out.println(file3); File file4=new File("D:\\file","d.txt"); System.out.println(file4); File parentDir=new File("D:\\file"); String child ="e.txt"; File file5=new File(parentDir,child); System.out.println(file5);
获取方法:
public String getName() //返回由此抽象路径名表示的文件或目录的名称public long length() //返回由此抽象路径名表示的文件的长度public String getPath() //将此抽象路径名转换为一个路径名字符串public String getAbsolutePath() //返回抽象路径名的绝对路径名字符串public String getParent() //返回此抽象路径名的父路径名的路径名字符串,如果此路径名没有指定父目录,则返回 nullpublic File getParentFile() //返回此抽象路径名的父路径名的抽象路径名,如果此路径名没有指定父目录,则返回 null
String name1 = file1.getName(); long length1 = file1.length(); String path1 = file1.getPath(); String absolutePath1 = file1.getAbsolutePath(); String parent1 = file1.getParent(); File parentFile1 = file1.getParentFile(); System.out.println("名称:"+name1+" 文件长度:"+length1+" 路径名字符串:"+path1+" 绝对路径:" +absolutePath1+" 父目录:"+parent1+" 父抽象路径"+parentFile1); String name2 = file2.getName(); long length2 = file2.length(); String path2 = file2.getPath(); String absolutePath2 = file2.getAbsolutePath(); String parent2 = file2.getParent(); File parentFile2 = file2.getParentFile(); System.out.println("名称:"+name2+" 文件长度:"+length2+" 路径名字符串:"+path2+" 绝对路径:" +absolutePath2+" 父目录:"+parent2+" 父抽象路径"+parentFile2); String name3 = file3.getName(); long length3 = file3.length(); String path3 = file3.getPath(); String absolutePath3 = file3.getAbsolutePath(); String parent3 = file3.getParent(); File parentFile3 = file3.getParentFile(); System.out.println("名称:"+name3+" 文件长度:"+length3+" 路径名字符串:"+path3+" 绝对路径:" +absolutePath3+" 父目录:"+parent3+" 父抽象路径"+parentFile3); String name4 = file4.getName(); long length4 = file4.length(); String path4 = file4.getPath(); String absolutePath4 = file4.getAbsolutePath(); String parent4 = file4.getParent(); File parentFile4 = file4.getParentFile(); System.out.println("名称:"+name4+" 文件长度:"+length4+" 路径名字符串:"+path4+" 绝对路径:" +absolutePath4+" 父目录:"+parent4+" 父抽象路径"+parentFile4); String name5 = file5.getName(); long length5 = file5.length(); String path5 = file5.getPath(); String absolutePath5 = file5.getAbsolutePath(); String parent5 = file5.getParent(); File parentFile5 = file5.getParentFile(); System.out.println("名称:"+name5+" 文件长度:"+length5+" 路径名字符串:"+path5+" 绝对路径:" +absolutePath5+" 父目录:"+parent5+" 父抽象路径"+parentFile5);
判断方法:
public boolean exists() //测试此抽象路径名表示的文件或目录是否存在public boolean isDirectory() //测试此抽象路径名表示的文件是否是一个目录 public boolean isFile() //测试此抽象路径名表示的文件是否是一个标准文件
boolean f1 = file1.exists(); System.out.println(f1); boolean f2= file2.exists(); System.out.println(f2); boolean f3 = file3.exists(); System.out.println(f3); boolean f4 = file3.exists(); System.out.println(f3); boolean f5 = file1.exists(); System.out.println(f5);
创建方法:
public boolean createNewFile() throws IOException //当且仅当不存在具有此抽象路径名指定的名称的文件时, //原子地创建由此抽象路径名指定的一个新的空文件public boolean mkdir() //创建此抽象路径名指定的目录public boolean mkdirs() //创建此抽象路径名指定的目录,包括创建必需但不存在的父目录
boolean newFile1 = file1.createNewFile(); System.out.println(newFile1); boolean newFile2 = file2.createNewFile(); System.out.println(newFile2); boolean newFile3 = file3.createNewFile(); System.out.println(newFile3);
file1和file2指定的文件a.txt和b.txt已经存在,所以不再创建,而file3指定的c.txt不存在,因此会在目录下创建并返回true。
File dir1=new File("D:\\file\\newDir"); boolean b1 = dir1.mkdir(); System.out.println(b1); File dirs=new File("D:\\file\\newDir1\\newDir2"); boolean b2 = dirs.mkdirs(); System.out.println(b2);
删除方法:
public boolean delete();
System.out.println(file1.delete()); System.out.println(dir1.delete()); System.out.println(dirs.delete());
在newDir2的同级目录下新建了一个aaa.txt,删除dirs只删除了newDir2文件夹;如果把aaa.txt放到newDir2文件夹下,则不能删除,即目录必须为空才能删除。
遍历:
public String[] list() //返回由此抽象路径名所表示的目录中的文件和目录的名称所组成字符串数组public File[] listFiles() //返回一个抽象路径名数组,这些路径名表示此抽象路径名所表示目录中的文件
String[] list = dir1.list(); for (String s : list) { System.out.println(s); } System.out.println("======================="); File[] files = dirs.listFiles(); for (File file : files) { System.out.println(file); }
打印多级目录:
public static void printDirs(File dirs){ File[] files = dirs.listFiles(); for (File file : files) { if(file.isFile()){ System.out.println("文件名:"+file.getAbsolutePath()); }else { System.out.println("目录名:"+file.getAbsolutePath()); printDirs(file); } } }
文件搜索:
找出java文件
public static void printDirs2(File dirs) { File[] files = dirs.listFiles(); for (File file : files) { if (file.isFile()) { if(file.getName().endsWith(".java")){ System.out.println("文件名:"+file.getAbsolutePath()); } } else { printDirs2(file); } } }
I/O流
开发中主要用到字符流和字节流,因此主要介绍这两种流,其他几种了解即可。字节流
字节输入流(InputStream)
abstract int read() //从输入流读取数据的下一个字节 int read(byte[] b) //从输入流读取一些字节数,并将它们存储到缓冲区 b void close() //关闭此输入流并释放与流相关联的任何系统资源
InputStream抽象类是字节输入流的超类,读取字节信息到内存中。
FileInputStream类
构造方法:
File file=new File("a.txt"); FileInputStream fis=new FileInputStream(file); FileInputStream fis1=new FileInputStream("b.txt");
读取字节数据
read():每次读取一个字节的数据,提升为int类型,读取到文件末尾返回-1。
FileInputStream fis=new FileInputStream("b.txt"); int b; while ((b=fis.read())!=-1){ System.out.println((char)b); } fis.close();
read(byte[] b):每次读取b长度个字节到数组中,返回读取到的有效字节个数,读到末尾时返回-1。
FileInputStream fis=new FileInputStream("b.txt"); int len; byte[] b=new byte[3]; while ((len=fis.read(b))!=-1){ System.out.println(new String(b,0,len)); } fis.close(); }
字节输出流(OutputStream)
void close() 关闭此输出流并释放与此流相关联的任何系统资源。void flush() 刷新此输出流并强制任何缓冲的输出字节被写出。void write(byte[] b) 将 b.length字节从指定的字节数组写入此输出流。void write(byte[] b, int off, int len) 从指定的字节数组写入 len个字节,从偏移 off开始输出到此输出流。abstract void write(int b) 将指定的字节写入此输出流。
字节输出流OutputStream抽象类是字节输出流的超类,将指定的字节信息写出到目的地。
FileOutputStream类
构造方法:
File file=new File("a.txt"); FileOutputStream fos=new FileOutputStream(file); FileOutputStream fos1=new FileOutputStream("b.txt");
write(
int
b):每次写出一个字节数据。
FileOutputStream fos=new FileOutputStream("a.txt"); fos.write(97); fos.write(98); fos.write(99); fos.close();
write(
byte
[] b):每次写出数组中的数据。
FileOutputStream fos=new FileOutputStream("a.txt"); byte[] b="南理工真牛皮".getBytes(); fos.write(b); fos.close();
write(byte[] b, int off, int len):每次写出指定长度数据。
FileOutputStream fos=new FileOutputStream("a.txt"); byte[] b="123456abcdefg".getBytes(); fos.write(b,2,3);//3,4,5 fos.close();
追加续写:
在构造方法中加入true参数。
FileOutputStream fos=new FileOutputStream("a.txt",true); byte[] b="123456abcdefg".getBytes(); fos.write(b,2,3);//3,4,5 fos.close();
写换行
\r\n
FileOutputStream fos=new FileOutputStream("a.txt",true); byte[] b="123456abcdefg".getBytes(); fos.write(b,2,3);//3,4,5 fos.write("\r\n".getBytes()); fos.write(b,3,3);//4,5,6 fos.close();
字节流练习:复制图片
//指定数据源,读到内存中 FileInputStream fis=new FileInputStream("D:\\file\\321.jpg"); //指定目的地,从内存写出到目的地 FileOutputStream fos=new FileOutputStream("D:\\file\\zhulin.jpg"); int len; byte[] b=new byte[1024]; while ((len=fis.read(b))!=-1){ fos.write(b,0,len); } //流的关闭原则,后开先关 fos.close(); fis.close();
字符流
一个中文字符在GBK编码下占2个字节,在UTF-8编码下占3个字节,Windows系统默认GBK,IDEA默认UTF-8,因此用字节流读取文件可能会乱码,所以可以用字符流来处理文本文件。但是,字符流不能操作图片、视频等非文本文件。
字符输入流(Reader)读取字符文件到内存。
abstract void close() 关闭流并释放与之相关联的任何系统资源。int read() 读一个字符 int read(char[] cbuf) 将字符读入数组。abstract int read(char[] cbuf, int off, int len) 将字符读入数组的一部分。
FileReader类
构造方法:
File file=new File("a.txt"); FileReader fr=new FileReader(file); FileReader fr=new FileReader("b.txt");
int
read
(
) :读取一个字符,自动提升为int类型。
FileReader fr=new FileReader("b.txt"); int b; while ((b=fr.read())!=-1){ System.out.println((char)b); } fr.close();
int
read
(
char[] cbuf
)
:每次读b长度的字符到数组中,返回读到的有效字符个数。
int len; char[] cbuf=new char[3]; while ((len=fr.read(cbuf))!=-1) { System.out.println(new String(cbuf,0,len)); } fr.close();
字符输出流(Writer)
将指定的字符信息写出到目的地。
abstract void close() 关闭流,先刷新。 abstract void flush() 刷新流。 void write(int c) 写一个字符 void write(char[] cbuf) 写入一个字符数组。 void write(String str) 写一个字符串 abstract void write(char[] cbuf, int off, int len) 写入字符数组的一部分。 void write(String str, int off, int len) 写一个字符串的一部分。
FileWriter类
构造方法:
File file =new File("a.txt"); FileWriter fw =new FileWriter(file); FileWriter fw=new FileWriter("b.txt");
write(
int
c) :写出一个字符。
FileWriter fw=new FileWriter("b.txt"); fw.write(97); fw.write(98); fw.write('c'); fw.write('d'); fw.write(20000); fw.close();
write(char[] cbuf):写出字符数组。
write(
char
[] cbuf,
int
off
,
int
len
):写出字符数组的一部分。
FileWriter fw=new FileWriter("b.txt"); char[] chars="我生气了,气死我了".toCharArray(); fw.write(chars); //写出字符数组 fw.write("\r\n"); //换行 fw.write(chars,2,3); //写出字符数组的一部分 fw.close();
write(
String
str
):写出字符串。
write(String str, int off, int len):写出字符串的一部分。
FileWriter fw=new FileWriter("b.txt"); String s="我生气了呀哎呀呀"; fw.write(s); //写出字符串 fw.write("\r\n"); fw.write(s,2,3); //写出字符串的一部分 fw.flush();
flush:刷新缓冲区,流对象可以继续使用。close:先刷新缓冲区,然后关闭流对象。
此外,还有缓冲流、转换流、序列化流、打印流。简单介绍一下。
缓冲流缓冲流也叫高效流,对四个基本的字符字节流的增强。
字节缓冲流:BufferedInputStream,BufferedOutputStream字符缓冲流:BufferedReader,BufferedWriter
原理
创建流对象时会创建内置的默认大小的缓冲区数组,通过缓冲区读写,减少了IO次数,提高了读写效率。
转换流可以指定编码读写。
序列化流对象“流化”,序列化后的Java对象可以保存到本地文件系统,或者通过Socket传输到其他的服务器。主要应用在一些需要把数据存储到磁盘或者数据缓存。
打印流平时用的print和println都来自PrintStream类。