Java 文件操作 , 这也是基于Java API 操作来实现的。
文件是操作系统管理外存数据管理的基本单位, 几乎所有的操作系统都有文件管理机制。 所谓文件, 是具有符号名而且在逻辑上具有完整意义的信息。 在操作系统中, 文件是以目录的方式进行组织管理, 文件在目录中的位置称为文件的路径。
File类:
就是文件和目录路径名的抽象表示形式。 其构造方法有:
1 public File(String pathName); 2 public File(String parent,String child); 3 public File(File parent,String child);
处理文件名:
String getName( );//得到一个文件的名称( 不包括路径) |
String getPath( );//得到一个文件的路径名 |
String getAbsolutePath( );//得到一个文件的绝对路径名 |
String getParent( );//得到一个文件的上一级目录名 |
String renameTo(File newName);//将当前文件名更名为给定文件的完整路径 |
属性测试方法:
方法 | 含义 |
boolean exists() | 测试当前File对象所指示的文件是否存在 |
boolean canWrite() | 测试当前文件是否可写 |
boolean canRead() | 测试当前文件是否可读 |
boolean isFile() | 测试当前文件是否是文件(非目录) |
boolean isDirectory() | 测试当前文件是否目录 |
普通文件信息和工具:
方法 | 含义 |
long lastModified() | 得到文件最近一次修改时间 |
long length() | 得到文件长度(以字节为单位) |
boolean delete() | 删除当前文件 |
目录操作:
方法 | 含义 |
boolean mkdir() | 根据当前对象生成一个由该对象指定的路径 |
String list() | 列出当前目录下的文件 |
举几个栗子 :
1 通过程序创建新文件 2 import java.io.IOException; 3 public class FileDemo { 4 public static void main(String[] args) { 5 File file = new File("d:\\woniuxy\\1.txt"); 6 try { 7 //当文件已经存在时返回false, 创建失败 8 file.createNewFile(); 9 } catch (IOException e) { 10 e.printStackTrace(); 11 } 12 }
目录遍历与文件信息:
String[] list()
该方法返回一个字符串数组, 这些字符串指定此抽象路径名表示的目录中的子文件和子目录。 如果此抽象路径名不表示一个目录, 那么此方法将返回null;
File[] listFiles()
该方法返回一个抽象路径名数组, 这些路径名表示此抽象路径名所表示目录中文件。 如果此抽象路径名不表示一个目录,那么此方法将返回null;
举个栗子:
递归显示c盘下所有文件夹及其中文件: public static void main(String[] args) { File root = new File("c:/"); showAllFiles(root); } public static void showAllFiles(File dir) { File[] fs = dir.listFiles(); for (File file : fs) { if(file.isDirectory()){ showAllFiles(file); }else{ System.out.println(file.getPath()); } } }
I/O 与流:
“流” 可以看作是一个流动的数据缓冲区, 数据从数据源流向数据目的地。 在Java语言中, 将不同类型的I/O抽象为流。 所谓“流( stream) ” , 可以看作是数据在文件或程序之间的传递 。
流的分类:
1, 按照数据流向分类:
—---->>输入流(InputStream) : 从外部设备(键盘、 鼠标、 文件、 网络等)到计算机的数据流动;
—---->>输出流(OutputStream) : 从计算机到外部设备(屏幕、 打印机、 文件等)的数据流动。
2,按照数据类型可分为:
——>>>字节流: 用于读/写字节类型的数据(包括ASCII表中的字符)。 类可分为InputStream类及其子类及OutputStream类及其子类。
——>>>字符流 : 用于读/写字符( 16位 Unicode 码) 。 包括Reader类及其子类及Writer类及其子类。
我们来看下流的结构:
常用的字节流与区别:
- DateInputStream
数据输入流允许应用程序以与机器无关方式从基础输入流中读取基本java数据类型。 应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。
- ByteArrayInputStream
ByteArrayInputStream包含一个内部缓冲区, 被称为内存流, 该缓冲区存储从流中读取的字节。 内部计数器跟总read方法要提供的下一个字节。
关闭ByteArrayInputStream无效。 此类中的方法在关闭此流后仍可被调用, 而不会产生任何IOException。
FileInputStream类:
此类是输入流, 字节流, 是InputStream类的子类, 常用的构造方法有:
public FileInputStream(String name) throws FileNotFoundException
String name是一个完整的路径名
public FileInputStream(File file) throws FileNotFoundException
根据File对象创建文件
举个栗子:
1 //利用FileInputStream将D盘下OK文本文件读取到程序中: 2 public static void showAllFiles(File dir) throws IOException { 3 File infile = new File("D:/ok.txt"); 4 InputStream input = new FileInputStream(infile); 5 byte[] b = new byte[(int)infile.length()]; 6 //从输入流中获取的有效数据长度 7 input.read(b); 8 //转化成字符串类型 9 System.out.println(new String(b)); 10 }
FileOutputStream类:
此类是输出流、 字节流, 是OutputStream类的子类, 常用的构造方法有:
public FileOutputStream(String name)
String name是完整的路径。
public FileOutputStream(String name,boolean append)
String name是完整的类路径。
举个栗子:
1 //输出数据到D盘的fos.txt文件中: 2 public static void showAllFiles(File dir) throws IOException { 3 // 创建字节输出流对象 4 File file = new File("D:\\fos.txt"); 5 FileOutputStream fos = new FileOutputStream(file); 6 // 写数据 7 fos.write("hello,IO".getBytes()); 8 // 释放资源 9 fos.close(); 10 }
字符流类:
FileReader类:
FileReader是InputStreamReader的子类, 是字符流, 输入流。FileReader类创建了一个可以读取文件内容的Reader类, 它的构造函数有:
FileReader(String filePath) filePath是一个文件的完整路径; FileReader(File fileObj) fileObj是描述该文件的File对象;
举个栗子:
1 //使用FileReader读取文件: 2 public static void showAllFiles(File dir) throws IOException { 3 FileReader fr = new FileReader("c://in.txt"); 4 BufferedReader br = new BufferedReader(fr); 5 String context = ""; 6 while((context = br.readLine()) != null){ 7 System.out.println(context); 8 } 9 }
FileWriter类
FileWriter是OutputStreamWriter的子类, 是字符流, 输出流。FileWriter创建一个可以写文件的Writer类。 构造函数:
构造方法 | 含义 |
FileWriter(String filePath) | filePath 是一个文件的完整路径 |
FileWriter(String filePath,boolean append) | 如果 append 为true ,输出是追加到文件末尾 |
FileWriter(Filefile fileobj) | 是描述该文件的File对象 |
FileWriter(File fileojb,boolean append) | 是描述该文件的file对象,如果append 为true,输出是附加到文件末尾 |
举个栗子:
1 //使用FileWriter写出文件到磁盘: 2 public static void showAllFiles(File dir) throws IOException { 3 String source = "learn how to write to file"; 4 FileWriter f1 = new FileWriter("D:/file1.txt"); 5 f1.write(source); 6 f1.close(); 7 }
缓存流:
缓存是IO的一种性能优化, 缓存流为IO流增加了内存缓存区, 有了缓存区, 便可以减少系统直接与硬盘交互传输的次数, 增加每次传送的数据量, 借以提高传输效率, 保护硬件。有了缓冲功能, 则会将输出的内容先放置在内存中, 等到有一定数据量的时候, 或是流关闭、 或调用flush()方法时, 才会将相应的内容保存到硬盘或其他目的地中。
缓存流分BufferedInputStream和BufferedOutputStream两类。
BufferedInputStream类
BufferedInputStream类可以对所有的InputStream类进行带缓存区的包装, 以达到性能的优化。 此类提供了两个构造方法:
BufferedInputStream(InputStream in) 此形式的构造方法创建了一个带有32个字节的缓存流; BufferedInputStream(InputStream in,int size) 此形式的构造方法按指定的大小(字节)来构造缓存流;
举个栗子:
1 public class Demo{ 2 public static void main(String[] args)throws IOException{ 3 BufferedInputSteam buffers = null ; 4 File file = new File ("E:/test.txt"); 5 byte[] buffer = new byte[(int)file.length()]; 6 //创建bufferinpusteam对象 7 buffers = new BufferedInputSteam(new FileInputStream(file)) 8 int bytesRead = 0; 9 try{ 10 //从文件中按字节读取内容, 到文件尾部的时候read 方法将返回-1 11 while ((bytesRead = buffers .read(buffer)) !=-1) { 12 //将读取的字节转换为字符串对象 13 String chunk = new String(buffer,0,bytesRead ) ; 14 System.out.println(chunk); 15 } 16 }catch (Exceptoion e){ 17 e.printStackTrace(); 18 } 19 } finally{ 20 //关闭bufferinputstream 21 buffers.close(); 22 } 23 } 24 }
BufferedOutputStream类
BufferedOutputStream类可以对所有的OutputStream类进行带缓存区的包装, 以达到性能的优化。 此类提供了两个构造方法:
1 BufferedOutputStream(OutputStream out) 2 此方式创建了一个带有32个字节的缓存流; 3 BufferedOutputStream(OutputStream out, int size) 4 此方式按指定的大小(字节)来构造缓存流;
举个栗子:
1 //利用BufferedOutputStream输出文件: 2 public static void showAllFiles(File dir) throws IOException { 3 FileOutputStream fos = new FileOutputStream("E:/test/2.txt"); 4 BufferedOutputStream bos = new BufferedOutputStream(fos); 5 bos.write("hello Buffered".getBytes()); 6 bos.close(); 7 fos.close(); 8 }