常用文件操作
-
创建文件对象相关构造器和方法
-
new File(String pathname)//根据路径构建一个File对象
public void create01() { String filePath = "e:\\news1.txt"; File file = new File(filePath); try { file.createNewFile(); System.out.println("文件创建成功"); } catch (IOException e) { e.printStackTrace(); } }
-
new File(File parent,String child) //根据父目录文件+子路径构建
public void create02() { File parentFile = new File("e:\\"); String fileName = "news2.txt"; File file = new File(parentFile, fileName); try { file.createNewFile(); System.out.println("创建成功~"); } catch (IOException e) { e.printStackTrace(); } }
-
new File(String parent,String child) //根据父目录+子路径构建
public void create03() { String parentPath = "e:\\"; String fileName = "news4.txt"; File file = new File(parentPath, fileName); try { file.createNewFile(); System.out.println("创建成功~"); } catch (IOException e) { e.printStackTrace(); } }
-
createNewFile //创建新文件
2.获取文件的相关信息
public void info() { File file = new File("e:\\news1.txt"); System.out.println("文件名字=" + file.getName()); System.out.println("文件绝对路径="+file.getAbsolutePath()); System.out.println("文件父级目录=" + file.getParent()); System.out.println("文件大小(字节)=" + file.length()); System.out.println("文件是否存在=" + file.exists()); System.out.println("是不是一个文件=" + file.isFile()); } }
3.文件的操作和文件删除
- mkdir( ) //创建一级目录
- mkdirs( ) //创建多级目录
- delete( ) //删除空目录或文件
-
IO流原理及流的分类
-
按操作数据单位不同分为:字节流(8 bit)二进制文件,字符流(按字符)文本文件
-
按数据流的流向不同分为:输入流,输出流
-
按流的角色的不同分为:节点流,处理流/包装流
抽象基类 字节流 字符流 输入流 InputStream Reader 输出流 OutputStream Writer
IO流常用的类
-
FileInputStream
/** * 单个字节的读取,效率比较低 */ public void readFile01() { String filePath = "e:\\hello.txt"; int readData = 0; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(filePath); while ((readData = fileInputStream.read()) != -1) { System.out.print((char)readData); } } catch (IOException e) { e.printStackTrace(); } finally { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 使用 read(byte[] b) 读取文件,提高效率 */ public void readFile02() { String filePath = "e:\\hello.txt"; byte[] buf = new byte[8]; int readLen = 0; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(filePath); while ((readLen = fileInputStream.read(buf))!= -1) { System.out.print(new String(buf, 0, readLen)); } } catch (IOException e) { e.printStackTrace(); } finally {. try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
-
FileOutputStream
/** * 使用 FileOutputStream 将数据写到文件中, * 如果该文件不存在,则创建该文件 */ public void writeFile() { String filePath = "e:\\a.txt"; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(filePath, true); String str = "world!"; fileOutputStream.write(str.getBytes(), 0, 3); } catch (IOException e) { e.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
public class FileCopy { public static void main(String[] args) { String destFilePath = "e:\\Koala3.jpg"; FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(srcFilePath); fileOutputStream = new FileOutputStream(destFilePath); byte[] buf = new byte[1024]; int readLen = 0; while ((readLen = fileInputStream.read(buf)) != -1) { fileOutputStream.write(buf, 0, readLen); } System.out.println("拷贝 ok~"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileInputStream != null) { fileInputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
-
FileReader&FileWriter
-
FileReader相关方法:
- ) new FileReader(File/String)
- ) read:每次读取单个字符,返回该字符,如果到文件末尾返回-1
- ) read(char[]):批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾返回相关API:
- )new String(char[]):将char[]转换成String
- )new String(char[],off,len):将char[]的指定部分转换成String
-
FileWriter 常用方法
-
) new FileWriter(File/String):覆盖模式,相当于流的指针在首端
-
)new FileWriter(File/String,true):追加模式,相当于流的指针在尾端
-
)write(int):写入单个字符
-
) write(char[]):写入指定数组
-
) write(char[],off,len):写入指定数组的指定部分
-
)write (string):写入整个字符串
-
)write(string,off,len):写入字符串的指定部分
相关API:String类:toCharArray:将String转换成char[]
注意:
FileWriter使用后,必须要关闭(close)或刷新(flush),否则写入不到指定的文件!
-
-
应用实例
/** * 单个字符读取文件 */public void readFile01() { String filePath = "e:\\story.txt"; FileReader fileReader = null; int data = 0; try { fileReader = new FileReader(filePath); while ((data = fileReader.read()) != -1) { System.out.print((char) data); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileReader != null) { fileReader.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 字符数组读取文件 */ public void readFile02() { System.out.println("~~~readFile02 ~~~"); String filePath = "e:\\story.txt"; FileReader fileReader = null; int readLen = 0; char[] buf = new char[8]; try { fileReader = new FileReader(filePath); while ((readLen = fileReader.read(buf)) != -1) { System.out.print(new String(buf, 0, readLen)); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileReader != null) { fileReader.close(); } } catch (IOException e) { e.printStackTrace(); } } }
public class FileWriter_ { public static void main(String[] args) { String filePath = "e:\\note.txt"; FileWriter fileWriter = null; char[] chars = {'a', 'b', 'c'}; try { fileWriter = new FileWriter(filePath); fileWriter.write('H'); fileWriter.write(chars); fileWriter.write("你好".toCharArray(), 0, 3); fileWriter.write(" 你好北京~"); fileWriter.write("风雨之后,定见彩虹"); fileWriter.write("上海", 0, 2); } catch (IOException e) { e.printStackTrace(); } finally { try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("程序结束..."); }
-