黑马程序员——java基础——IO的学习总结
2015-07-18 17:19
761 查看
android培训、java培训期待与您交流!
一、IO流体系图:
二、字节流的抽象基类:
InputStream ,OutputStream
三、字符流的抽象基类:
Reader , Writer
由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。
如:InputStream的子类FileInputStream。
如:Reader的子类FileReader。
InputStreamReader是Reader的子类
四、注意事项
1、文件路径
定义文件路径时Windows中的目录符号为“\”,但这个符号在Java中是特殊字符,需要转义。
可以用“\\”或“/”表示。
2、读取文件
读取文件时必须保证文件存在,否则将抛出FileNotFoundException。
3、写出文件
写出时文件如不存在时程序会创建新文件,如文件已存在则会清空原文件内容重新写入。
如需追加内容可调用FileWriter构造函数FileWriter(String fileName, boolean append)
五、练习
1、复制文本文件的九种方法
3、把指定文件夹的java文件复制另一个文件夹并改后缀名
一、IO流体系图:
二、字节流的抽象基类:
InputStream ,OutputStream
三、字符流的抽象基类:
Reader , Writer
由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。
如:InputStream的子类FileInputStream。
如:Reader的子类FileReader。
InputStreamReader是Reader的子类
四、注意事项
1、文件路径
定义文件路径时Windows中的目录符号为“\”,但这个符号在Java中是特殊字符,需要转义。
可以用“\\”或“/”表示。
2、读取文件
读取文件时必须保证文件存在,否则将抛出FileNotFoundException。
3、写出文件
写出时文件如不存在时程序会创建新文件,如文件已存在则会清空原文件内容重新写入。
如需追加内容可调用FileWriter构造函数FileWriter(String fileName, boolean append)
五、练习
1、复制文本文件的九种方法
package IO; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class copyFileDemo { public static void main(String[] args) throws IOException { String srcString ="C:\\Demo6.java"; String destString="D:\\copyDemo.java"; //method1(srcString,destString); //method2(srcString,destString); //method3(srcString,destString); //method4(srcString,destString); //method5(srcString,destString); //method6(srcString,destString); //method7(srcString,destString); method8(srcString,destString); /*method9(srcString,destString);*/ } //字节流每次读取和输出一个字节 public static void method1(String srcString,String destString) throws IOException{ //建立字节流输入对象 FileInputStream inFile=new FileInputStream(srcString); //建立字节流输出对象 FileOutputStream outFile=new FileOutputStream(destString); int length=0; //读取和写出数据 while((length=inFile.read())!=-1) { outFile.write(length); } //释放资源 outFile.close(); inFile.close(); } //字节流每次读取一个字节数组,字节流每次输出一个字节数组的一部分 private static void method2(String srcString, String destString) throws IOException{ //建立字节流输入对象 FileInputStream inFile=new FileInputStream(srcString); //建立字节流输出对象 FileOutputStream outFile=new FileOutputStream(destString); byte [] buf=new byte[1024]; int length=0; while((length=inFile.read(buf))!=-1){ outFile.write(buf, 0, length); outFile.flush(); } outFile.close(); inFile.close(); } //字节缓冲区每次读写一个字节 public static void method3(String srcString, String destString) throws IOException { //建立字节输入缓冲流对象 BufferedInputStream buffereIn=new BufferedInputStream(new FileInputStream(srcString)); //建立字节输出缓冲流对象 BufferedOutputStream buffereOut=new BufferedOutputStream(new FileOutputStream(destString)); int length=0; while((length=buffereIn.read())!=-1){ buffereOut.write(length); } //释放资源 buffereOut.close(); buffereIn.close(); } //建立字节输入缓冲流对象每次读取一个字节数组,建立字节输出流每次写出一个字节数组的一部分 public static void method4(String srcString, String destString) throws IOException{ //建立字节输入缓冲流对象 BufferedInputStream bufferedIn=new BufferedInputStream(new FileInputStream(srcString)); //建立字节输出缓冲流对象 BufferedOutputStream bufferedOut=new BufferedOutputStream(new FileOutputStream(destString)); byte[] buf=new byte[1024]; int length=0; while((length=bufferedIn.read(buf))!=-1){ bufferedOut.write(buf,0,length); bufferedOut.flush(); } //释放资源 bufferedOut.close(); bufferedIn.close(); } //字符流每次读取一个字符 public static void method5(String srcString, String destString) throws IOException { //建立输入字符流对象 FileReader inFile=new FileReader(srcString); //建立输出字符流对象 FileWriter outFile=new FileWriter(destString); int length=0; while((length=inFile.read())!=-1){ outFile.write(length); } //关闭资源 outFile.close(); inFile.close(); } //字符输入流每次读取一个字符数组,字符流每次写出一个字符数组的一部分 public static void method6(String srcString,String destString) throws IOException{ //建立字符输入流对象 FileReader inFile=new FileReader(srcString); //建立字符输入流对象 FileWriter outFile=new FileWriter(destString); char[] chr=new char[1024]; int length=0; while((length=inFile.read(chr))!=-1){ outFile.write(chr,0,length); outFile.flush(); } //释放资源 outFile.close(); inFile.close(); } //字符缓冲流每次读写一个字符 public static void method7(String srcString,String destString) throws IOException{ //建立字符输入缓冲流对象 BufferedReader brd=new BufferedReader(new FileReader(srcString)); //建立字符输出缓冲流对象 BufferedWriter bwt=new BufferedWriter(new FileWriter(destString)); int length=0; while((length=brd.read())!=-1){ bwt.write(length); } //释放资源 bwt.close(); brd.close(); } //字符缓冲输入流每次读一个字符数组,字符缓冲输出流每次读一个字符数组的一部分 public static void method8(String srcString,String destString) throws IOException{ //建立字符输入缓冲流对象 BufferedReader br=new BufferedReader(new FileReader(srcString)); //建立字符输出缓冲流对象 BufferedWriter bw=new BufferedWriter(new FileWriter(destString)); char[] chs=new char[1024]; int length=0; while((length=br.read(chs))!=-1){ bw.write(chs,0,length); } //释放资源 bw.close(); br.close(); } //字符缓冲输入流每次读取一行,字符缓冲输出流每次写出一行 public static void method9(String srcString,String destString) throws IOException{ //建立字符输入缓冲流对象 BufferedReader br=new BufferedReader(new FileReader(srcString)); //建立字符输出缓冲流对象 BufferedWriter bw=new BufferedWriter(new FileWriter(destString)); String str=null; while((str=br.readLine())!=null){ bw.write(str); bw.newLine(); bw.flush(); } //释放资源 bw.close(); br.close(); } }2、复制多级目录到另一个盘
package IO; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class copyFolderDemo { public static void main(String[] args) { File srcFile=new File("F:\\JavaSe\\day23\\code\\Demo"); File destFile=new File("G:\\"); try { copyFolder(srcFile,destFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void copyFolder(File srcFile, File destFile) throws IOException { if(srcFile.isDirectory()){ //如果是文件夹就获取该文件夹的名字和该文件夹下所有的File对象 File newFolder=new File(destFile,srcFile.getName());//<span style="font-family: Arial, Helvetica, sans-serif;">并在目的地目录创建相同名称的文件夹</span> newFolder.mkdir(); File[] fileArray=srcFile.listFiles();//获取该文件夹下所有的File对象存储到File[]数组中 for(File file:fileArray){<span style="white-space:pre"> </span>//遍历File[]数组 copyFolder(file, newFolder); //递归调用 } }else{ File newFile=new File(destFile,srcFile.getName());//如果是文件就获取文件名 copyFile(srcFile,newFile);//<span style="font-family: Arial, Helvetica, sans-serif;">复制文件到目的地</span> } } public static void copyFile(File srcFile, File newFile) throws IOException { //建立字节输入缓冲流对象 BufferedInputStream bufferedIn=new BufferedInputStream(new FileInputStream(srcFile)); //建立字节输出缓冲流对象 BufferedOutputStream bufferedOut=new BufferedOutputStream(new FileOutputStream(newFile)); byte[] buf=new byte[1024]; int length=0; while((length=bufferedIn.read(buf))!=-1){ bufferedOut.write(buf,0,length); bufferedOut.flush(); } //释放资源 bufferedOut.close(); bufferedIn.close(); } }
3、把指定文件夹的java文件复制另一个文件夹并改后缀名
package IO; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; public class FileRenameDemo2 { public static void main(String[] args) { File srcFolder=new File("F:\\Demo"); File destFolder=new File("F:\\Test"); if(!destFolder.exists()){ //判断目的地是否有该文件夹,没有就创建 destFolder.mkdir(); } //获取数据源文件的所有File对象,并用文件过滤器进行过滤,把以“.java”结尾的文件存储到File数组中 File[] fileArray=srcFolder.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { // TODO Auto-generated method stub return new File(dir,name).isFile()&&name.endsWith(".java"); } }); // 遍历用过滤器过滤过的File数组并调用复制文件的方法复制文件到目的地文件夹中 for(File file:fileArray){ String name=file.getName(); File newFile=new File(destFolder,name); try { copyFile(file,newFile);//调用复制文件夹的方法 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //获取目标文件夹下所有的File对象 File[] destFileArray= destFolder.listFiles(); for(File destFile:destFileArray){ String name=destFile.getName();//遍历获取每一个File对象的名字 String newName=name.replace(".java", ".jad");//替换 File newFile=new File(destFolder,newName);//相同目录是重命名,不同目录是剪切 destFile.renameTo(newFile);//重命名 } } private static void copyFile(File file, File newFile) throws IOException { // TODO Auto-generated method stub //建立字节缓冲输入流对象 BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file)); //建立字节缓冲输出流对象 BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(newFile)); byte[] buf=new byte[1024]; int length=0; while((length=bis.read(buf))!=-1){ bos.write(buf,0,length); } bos.close(); bis.close(); } }
相关文章推荐
- MySQL DBA的基础面试题目
- 黑马程序员——IO流(下)
- Java程序员经典面试题
- 黑马程序员——Java基础---面向对象
- 黑马程序员——Java基础---泛型
- 字符串面试题:将字符串转换为整数
- 黑马程序员——Java基础---异常
- 字符串面试题:将句子的单词序倒置
- 黑马程序员——Java基础---网络编程
- 黑马程序员--泛型和Collections使用
- 黑马程序员--集合框架
- 史上最全的程序员求职渠道总结
- Merge Two Sorted Lists
- 黑马程序员——高新技术---反射
- 黑马程序员——Java基础---IO流
- 如何进入游戏行业成为一个程序员
- 黑马程序员——Java基础---集合
- “菜鸟”程序员和“大神”程序员差距在哪里
- 码农的思维训练:超越专家
- 黑马程序员----JAVA概述