手机版
你好,游客 登录 注册
背景:
阅读新闻

Java中的IO处理

[日期:2012-06-28] 来源:Linux公社  作者:tianjf0514 [字体:大 中 小]

读入字节流:FileInputStream

FileInputStreamDemo.Java
  1. package com.tianjf;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7.   
  8. public class FileInputStreamDemo {  
  9.     public static void main(String[] args) throws IOException {  
  10.         /** 知道文件大小的读取 */  
  11.         /* 整个读取 */  
  12.         String fileName = "D:" + File.separator + "hello01.txt";  
  13.         File file = new File(fileName);  
  14.         InputStream in = new FileInputStream(file);  
  15.         byte[] bytes = new byte[(int) file.length()];  
  16.         in.read(bytes);  
  17.         in.close();  
  18.         System.out.println(new String(bytes));  
  19.   
  20.         /* 一个字节一个字节读取 */  
  21.         in = new FileInputStream(file);  
  22.         bytes = new byte[(int) file.length()];  
  23.         for (int i = 0; i < bytes.length; i++) {  
  24.             bytes[i] = (byte) in.read();  
  25.         }  
  26.         in.close();  
  27.         System.out.println(new String(bytes));  
  28.   
  29.         /** 不知道文件大小的读取,需要判断是否读到了结尾 */  
  30.         /* 每次读取1KB */  
  31.         in = new FileInputStream(file);  
  32.         bytes = new byte[1024];  
  33.         while (true) {  
  34.             // 读取1KB的内容到数组中   
  35.             int num = in.read(bytes);  
  36.             // 返回-1则表示读到了结尾   
  37.             if (num == -1) {  
  38.                 break;  
  39.             }  
  40.             // 将字节数组转换为字符串(index = 0, length = num)   
  41.             String str = new String(bytes, 0, num);  
  42.             System.out.println(str);  
  43.         }  
  44.         in.close();  
  45.   
  46.         /* 一个字节一个字节的读取 */  
  47.         in = new FileInputStream(file);  
  48.         bytes = new byte[1024];  
  49.         int count = 0;  
  50.         int tempByte = 0;  
  51.         while ((tempByte = in.read()) != -1) {  
  52.             bytes[count++] = (byte) tempByte;  
  53.         }  
  54.         in.close();  
  55.         System.out.println(new String(bytes));  
  56.     }  
  57. }  

写入字符流:FileWriter

FileWriterDemo.java
  1. package com.tianjf;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6. import java.io.Writer;  
  7.   
  8. public class FileWriterDemo {  
  9.     public static void main(String[] args) throws IOException {  
  10.         String fileName = "D:" + File.separator + "hello.txt";  
  11.         File file = new File(fileName);  
  12.         String str = "hello";  
  13.         Writer out = new FileWriter(file);  
  14.         out.write(str);  
  15.         out.flush();  
  16.                 out.close();  
  17.   
  18.         /* 追加新内容到文件中 */  
  19.         out = new FileWriter(file, true);  
  20.         out.write("\r\n" + str);  
  21.         out.flush();  
  22.                 out.close();  
  23.     }  
  24. }  

读入字符流:FileReader

FileReaderDemo.java
  1. package com.tianjf;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.io.Reader;  
  7.   
  8. public class FileReaderDemo {  
  9.     public static void main(String[] args) throws IOException {  
  10.         String fileName = "D:" + File.separator + "hello.txt";  
  11.         File file = new File(fileName);  
  12.         char[] chars = new char[100];  
  13.         Reader in = new FileReader(file);  
  14.         int count = in.read(chars);  
  15.         in.close();  
  16.         System.out.println("读入的长度为:" + count);  
  17.         System.out.println("内容为:" + new String(chars, 0, count));  
  18.   
  19.         /* 依次读取,需要判断是否读到了结尾 */  
  20.         chars = new char[100];  
  21.         in = new FileReader(file);  
  22.         int tempChar = 0;  
  23.         count = 0;  
  24.         while ((tempChar = in.read()) != -1) {  
  25.             chars[count++] = (char) tempChar;  
  26.         }  
  27.         in.close();  
  28.         System.out.println("内容为:" + new String(chars, 0, count));  
  29.     }  
  30. }  

使用字节流好还是字符流好?

答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。


缓冲区:BufferdReader

BufferdReaderDemo.java

  1. package com.tianjf;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.File;  
  6. import java.io.FileReader;  
  7. import java.io.FileWriter;  
  8. import java.io.IOException;  
  9.   
  10. public class BufferdReaderDemo {  
  11.     public static void main(String[] args) throws IOException {  
  12.         BufferedReader br = null;  
  13.         BufferedWriter bw = null;  
  14.         bw = new BufferedWriter(new FileWriter("D:" + File.separator + "hello.txt"));  
  15.         br = new BufferedReader(new FileReader("D:" + File.separator + "hello.txt"));  
  16.         String s = null;  
  17.         for (int i = 1; i <= 100; i++) {  
  18.             s = String.valueOf(Math.random());  
  19.             bw.write(s);  
  20.             // 写入一个行分隔符   
  21.             bw.newLine();  
  22.         }  
  23.         bw.flush();  
  24.         while ((s = br.readLine()) != null) {// BufferdReader方便的方法readLine()读一行   
  25.             System.out.println(s);  
  26.         }  
  27.         bw.close();  
  28.         br.close();  
  29.     }  
  30. }  

使用Scanner类从文件中读出内容

ScannerDemo.java

  1. package com.tianjf;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.util.Scanner;  
  6.   
  7. public class ScannerDemo {  
  8.     public static void main(String[] args) throws FileNotFoundException {  
  9.         File file = new File("d:" + File.separator + "hello.txt");  
  10.         Scanner scanner = new Scanner(file);  
  11.         while (scanner.hasNext())  
  12.         {  
  13.             System.out.println(scanner.next());  
  14.         }  
  15.     }  
  16. }  
linux
相关资讯       Java教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款