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

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

FileOutputStream

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

FileReader

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

FileWriter

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