IT评测·应用市场-qidao123.com

标题: p3 FileInputStream 和 FileOutputStream [打印本页]

作者: 八卦阵    时间: 2023-6-4 16:51
标题: p3 FileInputStream 和 FileOutputStream
FileInputStream 和 FileOutputStream


一、FileInputStream



代码演示:
  1. import org.junit.jupiter.api.Test;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. /**
  5. * @author
  6. * @version 1.0
  7. */
  8. public class FileInputStream_ {
  9.     public static void main(String[] args) {
  10.     }
  11.     /**
  12.      * 演示读取文件
  13.      * read(byte b)单个字节的读取,效率较低
  14.      * 使用 read(byte[] b) 来读取
  15.      */
  16.     @Test
  17.     public void readFile01(){
  18.         String filePath = "e:\\hello.txt";
  19.         int readData = 0;
  20.         FileInputStream fileInputStream = null;
  21.         try {
  22.             //创建 FileInputStream 对象,用于读取文件
  23.             fileInputStream = new FileInputStream(filePath);
  24.             //从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止
  25.             //如果返回-1.表示读取完毕
  26.             while((readData = fileInputStream.read()) != -1){
  27.                 System.out.print((char)readData);//转成char显示
  28.             }
  29.         } catch (IOException e) {
  30.             e.printStackTrace();
  31.         }finally {
  32.             try {
  33.                 fileInputStream.close();
  34.             } catch (IOException e) {
  35.                 e.printStackTrace();
  36.             }
  37.         }
  38.     }
  39.     @Test
  40.     public void readFile02(){
  41.         String filePath = "e:\\hello.txt";
  42.         byte[] buf = new byte[8];//一次读8个字节
  43.         int readLen = 0;
  44.         FileInputStream fileInputStream = null;
  45.         try {
  46.             //创建 FileInputStream 对象,用于读取文件
  47.             fileInputStream = new FileInputStream(filePath);
  48. //            读取的字节数最多等于b的长度。
  49. //            令k为实际读取的字节数; 这些字节将存储在元素b[0]至b[ k -1] ,使元素b[ k ]至b[b.length-1]不受影响。
  50. //            返回读取到缓冲区的总字节数,或者如果没有更多的数据,因为已经到达流的末尾,则是 -1 。
  51.             while((readLen = fileInputStream.read(buf)) != -1){
  52.                 System.out.print(new String(buf, 0, readLen));//显示
  53.             }
  54.         } catch (IOException e) {
  55.             e.printStackTrace();
  56.         }finally {
  57.             try {
  58.                 fileInputStream.close();
  59.             } catch (IOException e) {
  60.                 e.printStackTrace();
  61.             }
  62.         }
  63.     }
  64. }
  65. /*运行结果
  66.         hello world
  67.         hello world
  68. */
复制代码
二、FileOutputStream



代码演示:
  1. import org.junit.jupiter.api.Test;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. /**
  7. * @author
  8. * @version 1.0
  9. */
  10. public class FileOutputStream_ {
  11.     public static void main(String[] args) {
  12.     }
  13.     @Test
  14.     public void writeFile(){
  15.         String filePath = "e:\\a.txt";
  16.         FileOutputStream fileOutputStream = null;
  17.         try {
  18.             String str = "Hello World!";
  19.             //得到FileOutputStream对象
  20.             //1. new FileOutputStream(filePath) 创建方式,写入内容会默认覆盖原来的内容
  21.             //2. new FileOutputStream(filePath, append) 创建方式,append 为ture,写入内容会默认追加至原来的内容后,否则就覆盖
  22.             fileOutputStream = new FileOutputStream(filePath, true);
  23.             //写入一个字节
  24.             fileOutputStream.write('H');
  25.             //写入字符串,str.getBytes() 可以把 字符串 -》字符数组
  26.             fileOutputStream.write(str.getBytes());
  27.             /*
  28.             write(byte[] b, int off, int len) 将 len 字节从位于偏移量 off的指定字节数组写入文件输出流
  29.              */
  30. //            fileOutputStream.write(str.getBytes(), 0, str.length());
  31.         } catch (IOException e) {
  32.             e.printStackTrace();
  33.         }finally {
  34.             try {
  35.                 fileOutputStream.close();
  36.             } catch (IOException e) {
  37.                 e.printStackTrace();
  38.             }
  39.         }
  40.     }
  41. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4