文件,文件夹,路径java
file
就是文件路径,文件,文件夹的抽象类。数组
//file的三种够着函数public File(String pathName) File file = new File("d:\\xxxx"); //只负责抽象出对象,提供方法,无论路径是否存在 public File(String father, String child) public File(File parent, String child) public boolean createNewFile(String name) //只能从建立文件不能建立文件夹 public boolean mdirs() public boolean delete() public File getAbsoluteFile() public boolean exists() public boolean isDirectory() //list listFile获取功能 public String[] list() //获得File中的文件和文件夹名的集合 public File[] listFile()
遍历一个文件对象的时候能够根据需求选择合适的文件app
接口FileFilter
并实现内部方法for example:函数
public boolean accept(File file){测试
return file.getName().endWith(".java")编码
}操作系统
**原理:**实现了FileFilter的类就会调用acceptcode
File file = new File("xxxx"); File[] fileArr = file.listFile(new xxxxFilter());
listFile先遍历File对象,而后将得到的文件名的全路径,用过滤器传递给accept方法进行判断,最后返回true的就装进File[]对象
public static getAllDir(File dir){ File[] fileArray = dir.list; for(File file : fileArray){ if(file.isDirectory){ getAllDir(file); }else{ System.out.println(file); } } }
在存储中,任何一个文件个的最小单位都是Byte,字节流就是操做最小的存储单元,对于任何的文件均可以操做,每次只操做一个字节,能够写一个文件,也能够读取文件,可是文件夹不是文件
。接口
|--java.io.OutputStream全部的字节输出流的超类 public void public Byte write(int b)写入一个字节 public Byte write(byte[] b)写入b.length长度的字节 public Byte write(byte[] b, int index, int howmunch)写谁,从哪开始写,写几个字节 public void close() public void flush()
流对象的构造方法都是用来绑定数据目的,File对象
String串
public FileOutputStream(){ }
流对象的操做
注意文件被覆盖
FileOutputStream fos = new FileOutputStream("流对象"); //构造方法会自动建立,若是有,就会将数据覆盖 //FileNotFoundException fos.write(100); //IOException,可能写不进去 fos.close(); //写字节数组 byte[] bytes = {65,66,67,68}; fos.write(bytes); //从一开始写两个 fos.write(bytes, 1, 2); //直接写入字符串的方式 fos.write("hello".getBytes())
打开文件看见的不是100而是d
字母,由于文本在打开的时候都会去运行ASCII码。
续写问题,流对象
public FileOutputStream = newFileOutputStream(File file, boolean append)
在不一样的操做系统中,换行符不同,可是在Windows中是\r\n
File file = new File("xxxx.txt") FileOutputStream fos = new FileOutputStream(file,true); fos.write("hello\r\n".getBytes())
//try的外面声明变量,内部创建对象 //对fos的做用域提高 FileOutputStream fos = null; try{ fos = new FileOutputStream("xxxxx"); fos.write() }catch(IOException ex){ System.out.println(ex.getMessage()); throw new RuntimeException("文件写入失败"); }finally{ //提高做用域自后,Unhandle exception type of ioException if(fos != null){} try{ fos.close(); }catch (IOException ex){ throw new RuntimeException("关闭文件失败"); } } }
public int available() public void close() public void mark(int readlimit)在此输入流中标记当前位置 public void reset()定位到最后一次调用mark的位置 public boolean MarkSupported()测试此输入流是否支持mark和reset public int read()将文件读入,下一次调用自动读取下一个字节,到达文件末尾返回-1 public int read(byte[] b)从输入流中读取必定数量的字节,而且将其写入缓冲区数组b中,返回值读取了多少个有效字节,-1表示结束 public long skip(long n)跳过和丢弃n个字节
流对象操做
FileInputStream fis = new FileInputStream("xxxx"); //接受read方法的返回值 int len = 0; while((len = fis.read() != -1){ System.out.prineln((char)len) } fis.close() //解决读多了的问题 byte[] b = new byte[1024]; int len = 0; while((len = fis.read(b)) != -1){ System.out.print(new String(b,0,len)); } fis.close();
将一个文件中的字节放到另外一个文件中去就是复制
//按字节的方式去拿,弊端 慢 public static void main(String[] args){ FileInputStream fis = null; FIleOutputStream fos = null; int len = 0; try{ fis = new FileInputStream("xxxx"); fos = new FileOutputStream("xxxx"); while((len = fis.read()) != -1){ fos.write(len); } }catch(IOException ex){ System.out.println(ex.getMessage()); throw new RuntimeException("文件复制失败") }finally{ if((fis != null)&&(fos != null))){ fis.close(); fos.close(); } } } //采用数组的缓冲来提升效率 public static void main(String[] args){ FileInputStream fis = null; FIleOutputStream fos = null; int len = 0; try{ fis = new FileInputStream("xxxx"); fos = new FileOutputStream("xxxx"); byte[] bytes = new byte[1024] while((len = fis.read(bytes)) != -1){ fos.write(bytes,0,len); } }catch(IOException ex){ System.out.println(ex.getMessage()); throw new RuntimeException("文件复制失败") }finally{ if((fis != null)&&(fos != null))){ fis.close(); fos.close(); } } }
ASCII:一个字节中有七位表示内容
用两个字节表示中文,可是第一个字节的开头必定是复数,可是第二个开头多是负数
unicode,不管什么类型都用连个字节表示,在java中char就是使用这个编码
UTF-8,一个字节就可存储,不用两个字节,在每一个字节头加入了编码信
这个超类只能写文本文件,每次操做的是一个字符,只能用来读和写字符文件
与FileOutputStream不一样的是,写入以后必须调用flush才能写入,而且close暗含刷新的方法。
FileWriter fw = new FileWriter("xxxxxx"); fw.write("xxxxx"); fw.flush();
全部字符输入流的超类
FileReader fr = new FileReader("xxxx"); int len = 0; char[] c = new char[1024]; while((len = fr.read()) != -1){ System.out.println(new String(char,0,len)); }