java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter实例

FileInputStream

 
 
  1. <span style="font-family:Verdana;">import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.InputStream;  
  4.   
  5. public class TestFileInputStream {  
  6.     public static void main(String[] args) throws Exception { // 异常抛出, 不处理  
  7.         // 第1步: 使用File类找到一个文件  
  8.         File f = new File("c:" + File.separator + "test.txt");// 声明File 对象  
  9.         // 第2步: 通过子类实例化父类对象  
  10.         InputStream input = null;  
  11.         // 准备好一个输入的对象, 通过对象多态性进行实例化  
  12.         input = new FileInputStream(f);  
  13.         // 第3步:进行读操作, 所有的内容读到此数组中  
  14.         byte b[] = new byte[1024];  
  15.         int len = input.read(b);  
  16.         // 第4步:关闭输入流  
  17.         input.close();  
  18.         // 把byte数组变为字符串输出  
  19.         System.out.println("读入数据的长度:" + len);  
  20.         System.out.println("内容为:" + new String(b, 0, len));  
  21.     }  
  22. }</span>  

FileOutputStream

 
 
  1. <span style="font-family:Verdana;">import java.io.File;  
  2. import java.io.FileOutputStream;  
  3. import java.io.OutputStream;  
  4.   
  5. public class TestFileOutputStream {  
  6.     public static void main(String[] args) throws Exception { // 异常抛出,不处理  
  7.         // 第1步: 使用File类找到一个文件  
  8.         File f = new File("c:" + File.separator + "test.txt"); // 声明File对象  
  9.         // 第2步: 通过子类实例化父类对象  
  10.         OutputStream out = null;  
  11.         // 准备好一个输出的对象, 通过对象多态性,进行实例化  
  12.         out = new FileOutputStream(f);  
  13.         // 第3步: 进行写操作, 准备一个字符串  
  14.         String str = "Hello World!!!";  
  15.         // 只能输出byte数组,所以将字符串变为byte数组  
  16.         byte b[] = str.getBytes();  
  17.         // 将内容输出,保存文件  
  18.         out.write(b);  
  19.         // 第4步:关闭输出流  
  20.         out.close();  
  21.     }  
  22. }</span>  

FileReader

 
 
  1. <span style="font-family:Verdana;">import java.io.File;  
  2. import java.io.FileReader;  
  3. import java.io.Reader;  
  4.   
  5. public class TestFileReader {  
  6.     public static void main(String[] args) throws Exception { // 异常抛出, 不处理  
  7.         // 第1步:使用File类找到一个文件, 声明File对象  
  8.         File f = new File("d:" + File.separator + "test.txt");  
  9.         // 第2步:通过子类实例化父类对象  
  10.         Reader reader = null;  
  11.         // 准备好一个输入的对象, 通过对象多态性进行实例化  
  12.         reader = new FileReader(f);  
  13.         // 第3步:进行读操作, 所有的内容读到此数组中  
  14.         char c[] = new char[1024];  
  15.         int len = reader.read(c);  
  16.         // 第4步:关闭输入流  
  17.         reader.close();  
  18.         // 把char数组变为字符串输出  
  19.         System.out.println("内容为:" + new String(c, 0, len));  
  20.     }  
  21. }</span>  

FileWriter

 
 
    1. <span style="font-family:Verdana;">import java.io.File;  
    2. import java.io.FileWriter;  
    3. import java.io.Writer;  
    4.   
    5. public class TestFileWriter {  
    6.     public static void main(String[] args) throws Exception { // 异常抛出, 不处理  
    7.         // 第1步:使用File类找到一个文件, 声明File对象  
    8.         File f = new File("c:" + File.separator + "test.txt");  
    9.         // 第2步:通过子类实例化父类对象  
    10.         Writer out = null;  
    11.         // 准备好一个输出的对象, 通过对象多态性, 进行实例化  
    12.         out = new FileWriter(f);  
    13.         // 第3步:进行写操作, 准备一个字符串  
    14.         String str = "Hello World!!!";  
    15.         out.write(str);  
    16.         out.flush();  
    17.         // 第4步:关闭输出流  
    18.         out.close();  
    19.     }  
    20. }</span>  
posted @ 2018-01-20 22:26  hongma  阅读(242)  评论(0编辑  收藏  举报