java IO(File类、字节流与字符流、字节字符转换流) ゝ一世哀愁。 2021-10-06 13:52 364阅读 0赞 # File类 # 在整个io包中,唯一表示与文件本身有关的类就是File类。使用File类可以进行创建或删除文件等常用操作,要想使用File类,则首先要观察File类的构造方法,此类的常用构造方法如下 **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **public** File(String pathname)实例化File类的时候,必须设置好路径根据路径找到文件 ### File类中的主要方法和常量 ### <table style="color:rgb(0,0,0); font-family:Arial; font-size:14px; line-height:26px"> <tbody> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 方法或常量</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 类型</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 描述</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public static final String pathSeparator</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 常量</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 表示路径的分隔符(windows:‘;’)</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public static final String separator</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 常量</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 表示路径分隔符(windows:‘\’)</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public File(String pathname)</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 构造</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 创建File类对象,传入完整的路径</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public boolean createNewFile() throws IOException</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 普通</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 创建新文件</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public boolean exists()</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 普通</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 判断文件是否存在</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public boolean delete()</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 普通</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 删除文件</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public boolean isDirectory()</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 普通</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 判断给定的路径是否是一个目录</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public long length()</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 普通</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 返回文件的大小</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public String[] list()</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 普通</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 列出指定目录的全部内容,只是名称</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public File[] listFiles()</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 普通</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 列出指定目录的全部内容,会列出路径。</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public boolean mkdir()</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 普通</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 创建一个目录</p> </td> </tr> <tr> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> public boolean renameTo(File dest)</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 普通</p> </td> <td> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px"> 为已有的文件重命名</p> </td> </tr> </tbody> </table> 代码示例: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.\*; 2. **public** **class** FileDemo01 3. \{ 4. **public** **static** **void** main(String args\[\])\{ 5. File file=**new** File("d:"\+File.separator+"test.txt"); 6. System.out.println("file.pathSeparator:"\+file.pathSeparator); //调用静态变量 7. System.out.println("file.separator:"\+file.separator); //调用静态变量 8. **if**(file.exists())\{ //判断当前文件是否存在 9. file.delete(); //存在就删除 10. \} 11. **try**\{ 12. file.createNewFile(); //删除后重新创建 13. \}**catch**(IOException e)\{ 14. e.printStackTrace(); 15. \} 16. System.out.println("文件的大小:"\+file.length()); //输出新创建文件的大小 17. \} 18. \} 代码示例2: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.\*; 2. **public** **class** FileDemo02 3. \{ 4. **public** **static** **void** main(String args\[\])\{ 5. File file=**new** File("d:"\+File.separator+"test"); 6. file.mkdir(); //创建新的文件夹 7. File f=**new** File("d:"\+File.separator+"test.txt"); 8. f.renameTo(**new** File("d:"\+File.separator+"test1.txt")); //为已知的文件重命名 9. \} 10. \} 案例:列出指定目录的全部文件 **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.File ; 2. **import** java.io.IOException ; 3. **public** **class** FileDemo03\{ 4. **public** **static** **void** main(String args\[\])\{ 5. File my = **new** File("d:" + File.separator) ; // 操作路径 6. print(my) ; 7. \} 8. **public** **static** **void** print(File file)\{ // 递归调用 9. **if**(file!=**null**)\{ // 判断对象是否为空 10. **if**(file.isDirectory())\{ // 如果是目录 11. File f\[\] = file.listFiles() ; // 列出全部的文件 12. **if**(f!=**null**)\{ // 判断此目录能否列出 13. **for**(**int** i=0;i<f.length;i++)\{ 14. print(f\[i\]) ; // 因为给的路径有可能是目录,所以,继续判断 15. \} 16. \} 17. \}**else**\{ 18. System.out.println(file) ; // 输出路径 19. \} 20. \} 21. \} 22. \}; # 字节流与字符流 # 在[Java][].io包中操作文件内容的主要有两大类:字节流、字符流。两类都分为输入和输出操作。在字节流中输出数据主要是使用OutputStream完成,输入使用的是InputStream,在字符流中输出主要是使用Writer类完成,输入主要是使用Reader类完成。 ## 流 ## 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存 起来的时候,就要使用输出流完成。 ### 操作流程 ### 在java中IO操作也是有相关步骤的,以文件操作为例,主要的操作流程如下: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. •A、使用File类打开一个文件 2. •B、通过字节流或字符流的子类,指定输出的位置 3. •C、进行读\\写操作 4. •D、关闭输入\\输出 ## 字节流 ## 字节流主要是操作byte类型数据,也byte数组为准,主要操作类就是 ·字节输出流:OutputStream ·字节输入流:InputStream ### 字节输出流:OutputStream ### OutputStream类是整个io包中字节输出流的最大父类,此类的定义如下: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **public** **abstract** **class** OutputStream **extends** Object **implements** Closeable, Flushable 2. •Closeable:表示可以关闭的操作,因为程序到最后肯定要关闭。 3. •Flushable:表示刷新,清空内存中的数据。 从以上类的定义中可以发现,此类是一个抽象类,如果要想使用此类的话,则首先必须通过子类实例化对象,那么如果现在要操作一个文件,则可以使用FileOutputStream类。通过向上转型之后,可以为OutputStream实例化。 OutputStream类中的常用方法: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. 方法 描述 2. **public** **void** close() **throws** IOException 关闭输出流 3. **public** **void** flush() **throws** IOException 刷新缓冲区 4. **public** **void** write(**byte**\[\] b) **throws** IOException 将一个**byte**数组写入数据流 5. **public** **void** write(**byte**\[\] b,**int** off,**int** len)**throws** IOException 将一个指定范围的**byte**数组写入数据流 6. **public** **abstract** **void** write(**int** b) **throws** IOException 将一个字节数据写入数据流 要想使用以上的方法,必须使用子类进行实例化,此时使用FileOutputStream子类,此类的构造方法如下: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **public** FileOutputStream(File file) **throws** FileNotFoundException 代码示例:创建文件并写入字符 **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.\*; 2. **public** **class** OutputStreamDemo01 3. \{ 4. **public** **static** **void** main(String args\[\]) **throws** Exception\{ 5. //1、使用File类指定一个文件名 6. File file=**new** File("d:"\+File.separator+"test.txt"); 7. //2、创建OutputStream类,并为此实例化对象 8. OutputStream out=**new** FileOutputStream(file); 9. //3、执行写入操作 10. String str="Hello world"; 11. **byte** b\[\]=str.getBytes(); 12. out.write(b); 13. //4、关闭输入流 14. out.close(); 15. \} 16. \} 注: 1、 在操作的时候如果文件本身不存在,则会为用户自动创建新文件。 2、 如果要追加的内容需要换行,则在内容中加入“\\r\\n”就可以了。 以上的操作在写入数据之后,文件之前的内容已经不存在了,因为在IO操作中默认的情况是将其进行覆盖的,如果现在想执行追加的功能,则必须设置追加的操作,此时可以通过FileoutputStream向文件中追加内容:其另外的一个构造方法: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. FileOutputStream(File file, **boolean** append) 在构造方法中,如果将append的值设置为true,则表示在文件的末尾追加内容。 **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. OutputStream out=**new** FileOutputStream(file,**true**); ### 字节输入流:InputStream ### 既然程序可以向文件中写入内容,则就可以通过InputStream从文件中把内容读取进来,首先来看InputStream类的定义: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **public** **abstract** **class** InputStream **extends** Object **implements** Closeable 和OutputStream一样,InputStream本身也是一个抽象类,必须依靠其子类,如果现在是从文件中读取,子类肯定是FileInputStream。构造方法: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **public** FileInputStream(File file) **throws** FileNotFoundException **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. InputStream类的常用方法: 2. 方法 描述 3. **public** **int** available() **throws** IOException 可以取得输入文件的大小 4. **public** **void** close() **throws** IOException 关闭输入流 5. **public** **abstract** **int** read() **throws** IOException 读取内容,以数字的方式读取 6. **public** **int** read(**byte**\[\] b) **throws** IOException 将内容读到**byte**数组之中,同时返回个数 示例代码: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.File ; 2. **import** java.io.InputStream ; 3. **import** java.io.FileInputStream ; 4. **public** **class** InputStreamDemo01\{ 5. **public** **static** **void** main(String args\[\]) **throws** Exception\{ // 异常抛出,不处理 6. // 第1步、使用File类找到一个文件 7. File f= **new** File("d:" + File.separator + "test.txt") ; // 声明File对象 8. // 第2步、通过子类实例化父类对象 9. InputStream input = **null** ; // 准备好一个输入的对象 10. input = **new** FileInputStream(f) ; // 通过对象多态性,进行实例化 11. // 第3步、进行读操作 12. **byte** b\[\] = **new** **byte**\[1024\] ; // 所有的内容都读到此数组之中 13. input.read(b) ; // 读取内容 14. // 第4步、关闭输出流 15. input.close() ; // 关闭输出流 16. System.out.println("内容为:" + **new** String(b)) ; // 把byte数组变为字符串输出 17. \} 18. \}; 以上代码对文件中的内容读取了出来,但是数组开辟的空间远远要大于文件实际占用的空间,则此时可以根据读取文件的大小来开辟数组空间: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.File ; 2. **import** java.io.InputStream ; 3. **import** java.io.FileInputStream ; 4. **public** **class** InputStreamDemo03\{ 5. **public** **static** **void** main(String args\[\]) **throws** Exception\{ // 异常抛出,不处理 6. // 第1步、使用File类找到一个文件 7. File f= **new** File("d:" + File.separator + "test.txt") ; // 声明File对象 8. // 第2步、通过子类实例化父类对象 9. InputStream input = **null** ; // 准备好一个输入的对象 10. input = **new** FileInputStream(f) ; // 通过对象多态性,进行实例化 11. // 第3步、进行读操作 12. // byte b\[\] = new byte\[input..available()\] ; 跟使用下面的代码是一样的 13. **byte** b\[\] = **new** **byte**\[(**int**)f.length()\] ; // 数组大小由文件决定 14. **int** len = input.read(b) ; // 读取内容 15. // 第4步、关闭输出流 16. input.close() ; // 关闭输出流\\ 17. System.out.println("读入数据的长度:" + len) ; 18. System.out.println("内容为:" + **new** String(b)) ; // 把byte数组变为字符串输出 19. \} 20. \}; 另一种读取方法: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.File ; 2. **import** java.io.InputStream ; 3. **import** java.io.FileInputStream ; 4. **public** **class** InputStreamDemo05\{ 5. **public** **static** **void** main(String args\[\]) **throws** Exception\{ // 异常抛出,不处理 6. // 第1步、使用File类找到一个文件 7. File f= **new** File("d:" + File.separator + "test.txt") ; // 声明File对象 8. // 第2步、通过子类实例化父类对象 9. InputStream input = **null** ; // 准备好一个输入的对象 10. input = **new** FileInputStream(f) ; // 通过对象多态性,进行实例化 11. // 第3步、进行读操作 12. **byte** b\[\] = **new** **byte**\[1024\] ; // 数组大小由文件决定 13. **int** len = 0 ; 14. **int** temp = 0 ; // 接收每一个读取进来的数据 15. **while**((temp=input.read())!=-1)\{ 16. // 表示还有内容,文件没有读完 17. b\[len\] = (**byte**)temp ; 18. len++ ; 19. \} 20. // 第4步、关闭输出流 21. input.close() ; // 关闭输出流\\ 22. System.out.println("内容为:" + **new** String(b,0,len)) ; // 把byte数组变为字符串输出 23. \} 24. \}; 以上的读取方式在都是比较常见的。 ## 字符流 ## 在程序中一个字符等于2个字节,那么java提供了Reader、Writer两个专门操作字符流的类。 ·字符输出流:Writer ·字符输入流:Reader ### 字符输出流:Writer ### Writer本身是一个字符流的输出类,此类的定义如下: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **public** **abstract** **class** Writer **extends** Object **implements** Appendable, Closeable, Flushable 此类本身也是一个抽象类,如果要想使用此类,则肯定要使用其子类,此时如果是向文件中写入内容,所以应该使用FileWriter子类。构造方法如下: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **public** FileWriter(File file) **throws** IOException Writer类的常用方法: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. 方法或常量 描述 2. **public** **abstract** **void** close() **throws** IOException 关闭输出流 3. **public** **void** write(String str) **throws** IOException 将字符串输出 4. **public** **void** write(**char**\[\] cbuf) **throws** IOException 将字符数组输出 5. **public** **abstract** **void** flush() **throws** IOException 强制性清空缓存 示例代码:(字符流可以直接输出字符串,不需要转换为字节) **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.File ; 2. **import** java.io.Writer ; 3. **import** java.io.FileWriter ; 4. **public** **class** WriterDemo01\{ 5. **public** **static** **void** main(String args\[\]) **throws** Exception\{ // 异常抛出,不处理 6. // 第1步、使用File类找到一个文件 7. File f= **new** File("d:" + File.separator + "test.txt") ; // 声明File对象 8. // 第2步、通过子类实例化父类对象 9. Writer out = **null** ; // 准备好一个输出的对象 10. out = **new** FileWriter(f) ; // 通过对象多态性,进行实例化 11. // 第3步、进行写操作 12. String str = "Hello World!!!" ; // 准备一个字符串 13. out.write(str) ; // 将内容输出,保存文件 14. // 第4步、关闭输出流 15. out.close() ; // 关闭输出流 16. \} 17. \}; 此时如果是想追加内容,与FileInputStream的格式是一样的,添加appemd属性为true; ### 字符输入流:Reader ### Reader本身是一个字符流的输入类,此类的定义如下: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **public** **abstract** **class** Reader **extends** Object **implements** Closeable, Readable; 此类本身也是一个抽象类,如果要想使用此类,则肯定要使用其子类,此时如果是向文件中写入内容,所以应该使用FileReader子类。构造方法如下: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **public** FileReaderr(File file) **throws** IOException Writer类的常用方法: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. 方法或常量 描述 2. **public** **abstract** **void** close() **throws** IOException 关闭输出流 3. **public** **int** read() **throws** IOException 读取单个字符 4. **public** **int** read(**char**\[\] cbuf) **throws** IOException 将内容读到字符串数组中,返回读入的长度 示例代码:(以字符数组的形式读取出数据) **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.File ; 2. **import** java.io.Reader ; 3. **import** java.io.FileReader ; 4. **public** **class** ReaderDemo01\{ 5. **public** **static** **void** main(String args\[\]) **throws** Exception\{ // 异常抛出,不处理 6. // 第1步、使用File类找到一个文件 7. File f= **new** File("d:" + File.separator + "test.txt") ; // 声明File对象 8. // 第2步、通过子类实例化父类对象 9. Reader input = **null** ; // 准备好一个输入的对象 10. input = **new** FileReader(f) ; // 通过对象多态性,进行实例化 11. // 第3步、进行读操作 12. **char** c\[\] = **new** **char**\[1024\] ; // 所有的内容都读到此数组之中 13. **int** len = input.read(c) ; // 读取内容 14. // 第4步、关闭输出流 15. input.close() ; // 关闭输出流 16. System.out.println("内容为:" + **new** String(c,0,len)) ; // 把字符数组变为字符串输出 17. \} 18. \}; 字节流在操作的时候本身是不会用到缓冲区(内存)的,是与文件本身直接操作的,而字符流在操作的时候使用到缓冲区的。 通过代码来验证字符流使用到了缓存。 **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.File ; 2. **import** java.io.OutputStream ; 3. **import** java.io.FileOutputStream ; 4. **public** **class** OutputStreamDemo05\{ 5. **public** **static** **void** main(String args\[\]) **throws** Exception\{ // 异常抛出,不处理 6. // 第1步、使用File类找到一个文件 7. File f= **new** File("d:" + File.separator + "test.txt") ; // 声明File对象 8. // 第2步、通过子类实例化父类对象 9. OutputStream out = **null** ; // 准备好一个输出的对象 10. out = **new** FileOutputStream(f) ; // 实例化 11. // 第3步、进行写操作 12. String str = "Hello World!!!" ; // 准备一个字符串 13. **byte** b\[\] = str.getBytes() ; // 只能输出byte数组,所以将字符串变为byte数组 14. out.write(b) ; // 写入数据 15. // 第4步、关闭输出流 16. // out.close() ; // 关闭输出流 此处没有关闭输出流 17. \} 18. \}; 在使用字节流操作中,即使没有关闭,最终也是可以输出的。 **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.File ; 2. **import** java.io.Writer ; 3. **import** java.io.FileWriter ; 4. **public** **class** WriterDemo03\{ 5. **public** **static** **void** main(String args\[\]) **throws** Exception\{ // 异常抛出,不处理 6. // 第1步、使用File类找到一个文件 7. File f= **new** File("d:" + File.separator + "test.txt") ; // 声明File对象 8. // 第2步、通过子类实例化父类对象 9. Writer out = **null** ; // 准备好一个输出的对象 10. out = **new** FileWriter(f) ; // 通过对象多态性,进行实例化 11. // 第3步、进行写操作 12. String str = "Hello World!!!" ; // 准备一个字符串 13. out.write(str) ; // 将内容输出,保存文件 14. // 第4步、关闭输出流 15. // out.close() ; // 此时,没有关闭 16. \} 17. \}; 以上的内容,没有输出任何的内容,也就是说,所有的内容都是保存在了缓冲区之中,而如果执行关闭输出流的话会强制性的刷新缓冲区,所以可以把内容输出。 如果现在假设,没有关闭的话,也可以手工强制性调用刷新方法: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **public** **void** flush() **throws** IOException 代码示例: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.File ; 2. **import** java.io.Writer ; 3. **import** java.io.FileWriter ; 4. **public** **class** WriterDemo04\{ 5. **public** **static** **void** main(String args\[\]) **throws** Exception\{ // 异常抛出,不处理 6. // 第1步、使用File类找到一个文件 7. File f= **new** File("d:" + File.separator + "test.txt") ; // 声明File对象 8. // 第2步、通过子类实例化父类对象 9. Writer out = **null** ; // 准备好一个输出的对象 10. out = **new** FileWriter(f) ; // 通过对象多态性,进行实例化 11. // 第3步、进行写操作 12. String str = "Hello World!!!" ; // 准备一个字符串 13. out.write(str) ; // 将内容输出,保存文件 14. // 第4步、关闭输出流 15. out.flush() ; // 强制性清空缓冲区中的内容 16. // out.close() ; // 此时,没有关闭 17. \} 18. \}; **在所有的硬盘保存文件或是进行传输的时候都是以字节的方式进行的。包括图片也是按字节完成,而字符只有在内存中才会形成。所以在开发中使用字节的操作是较多的。** ## 范例:文件拷贝 ## 通过执行该程序拷贝源文件到目标文件: **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.\* ; 2. **public** **class** Copy\{ 3. **public** **static** **void** main(String args\[\])\{ 4. **if**(args.length!=2)\{ // 判断是否是两个参数 5. System.out.println("输入的参数不正确。") ; 6. System.out.println("例:java Copy 源文件路径 目标文件路径") ; 7. System.exit(1) ; // 系统退出 8. \} 9. File f1 = **new** File(args\[0\]) ; // 源文件的File对象 10. File f2 = **new** File(args\[1\]) ; // 目标文件的File对象 11. **if**(!f1.exists())\{ 12. System.out.println("源文件不存在!") ; 13. System.exit(1) ; 14. \} 15. InputStream input = **null** ; // 准备好输入流对象,读取源文件 16. OutputStream out = **null** ; // 准备好输出流对象,写入目标文件 17. **try**\{ 18. input = **new** FileInputStream(f1) ; 19. \}**catch**(FileNotFoundException e)\{ 20. e.printStackTrace() ; 21. \} 22. **try**\{ 23. out = **new** FileOutputStream(f2) ; 24. \}**catch**(FileNotFoundException e)\{ 25. e.printStackTrace() ; 26. \} 27. **if**(input!=**null** && out!=**null**)\{ // 判断输入或输出是否准备好 28. **int** temp = 0 ; 29. **try**\{ 30. **while**((temp=input.read())!=-1)\{ // 开始拷贝 31. out.write(temp) ; // 边读边写 32. \} 33. System.out.println("拷贝完成!") ; 34. \}**catch**(IOException e)\{ 35. e.printStackTrace() ; 36. System.out.println("拷贝失败!") ; 37. \} 38. **try**\{ 39. input.close() ; // 关闭 40. out.close() ; // 关闭 41. \}**catch**(IOException e)\{ 42. e.printStackTrace() ; 43. \} 44. \} 45. \} 46. \} # 字节-字符转换流 # ## OutputStreamWriter和InputStreamReader ## 在整个IO包中,实际上就是字节流和字符流,但是除了这两个流之外,还存在一组字节流-字符流的转换类。 **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. •OutputStreamWriter:是Writer的子类,将输出的字符流转换为字节流。即:将一个字节流的输出对象变为字节流的输出对象 2. •InputStreamReader:是Reader的子类,将输入的字节流变为字符流,即:将一个字节流的输入对象变为字符流的输入对象。 在OutputStreamWriter类中需要一个字节流的对象:public OutputStreamWriter(OutputStream out),例如:将字节的文件输出流,以字符的形式输出。 **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.\*; 2. **public** **class** OutputStreamWriterDemo01 3. \{ 4. **public** **static** **void** main(String args\[\]) **throws** Exception\{ //所有异常抛出 5. File file=**new** File("d:"\+File.separator+"test.txt"); 6. Writer writer=**null**; //字符输出流 7. writer=**new** OutputStreamWriter(**new** FileOutputStream(file)); //字节流变为字符流 8. String str="hello world!!!!"; 9. writer.write(str); //使用字符流输出 10. writer.close(); 11. \} 12. \} 读的时候,也可以使用字符流的形式读取字节流的文件。 **\[java\]** [view plain][] [copy][view plain] [print][view plain] [?][view plain] 1. **import** java.io.\* ; 2. **public** **class** InputStreamReaderDemo01\{ 3. **public** **static** **void** main(String args\[\]) **throws** Exception\{ 4. File f = **new** File("d:" + File.separator + "test.txt") ; 5. Reader reader = **null** ; 6. reader = **new** InputStreamReader(**new** FileInputStream(f)) ; // 将字节流变为字符流 7. **char** c\[\] = **new** **char**\[1024\] ; 8. **int** len = reader.read(c) ; // 读取 9. reader.close() ; // 关闭 10. System.out.println(**new** String(c,0,len)) ; 11. \} 12. \}; 对于FileWriter和FileReader的说明: 从JDK文档中可知FileOutputStream是OutputStream的直接子类,FileInputStream也是InputStream的直接子类,但是在字符流文件的两个操作类却有一些特殊,FileWriter并不是Writer的子类,而是OutputStream的子类,而FileReader也不是Reader的直接子类,是InputStreamReader的子类。 转载自:[点击打开链接][Link 1] [view plain]: http://blog.csdn.net/cz1029648683/article/details/6686050# [Java]: http://lib.csdn.net/base/17 [Link 1]: http://blog.csdn.net/cz1029648683/article/details/6686050
还没有评论,来说两句吧...