IO流 p3 FileInputStream 和 FileOutputStream
FileInputStream 和 FileOutputStream
-
InputStream:字节输入流
InputStream抽象类是所有类输入流的超类
InputStream 常用的子类
- FileInputStream: 文件输入流
- BufferedInputStream:缓冲字节输入流
- ObjectInputStream:对象字节输入流
一、FileInputStream
-
构造方法摘要
Constructor and Description FileInputStream(File file)
通过打开与实际文件的连接创建一个FileInputStream
,该文件由文件系统中的File
对象file
命名。FileInputStream(FileDescriptor fdObj)
创建FileInputStream
通过使用文件描述符fdObj
,其表示在文件系统中的现有连接到一个实际的文件。FileInputStream(String name)
通过打开与实际文件的连接来创建一个FileInputStream
,该文件由文件系统中的路径名name
命名。 -
方法摘要
Modifier and Type Method and Description int
available()
返回从此输入流中可以读取(或跳过)的剩余字节数的估计值,而不会被下一次调用此输入流的方法阻塞。void
close()
关闭此文件输入流并释放与流相关联的任何系统资源。protected void
finalize()
确保当这个文件输入流的close
方法没有更多的引用时被调用。FileChannel
getChannel()
返回与此文件输入流相关联的唯一的FileChannel
对象。FileDescriptor
getFD()
返回表示与此FileInputStream
正在使用的文件系统中实际文件的连接的FileDescriptor
对象。int
read()
从该输入流读取一个字节的数据。int
read(byte[] b)
从该输入流读取最多b.length
个字节的数据为字节数组。int
read(byte[] b, int off, int len)
从该输入流读取最多len
字节的数据为字节数组。long
skip(long n)
跳过并从输入流中丢弃n
字节的数据。
代码演示:
import org.junit.jupiter.api.Test; import java.io.FileInputStream; import java.io.IOException; /** * @author * @version 1.0 */ public class FileInputStream_ { public static void main(String[] args) { } /** * 演示读取文件 * read(byte b)单个字节的读取,效率较低 * 使用 read(byte[] b) 来读取 */ @Test public void readFile01(){ String filePath = "e:\\hello.txt"; int readData = 0; FileInputStream fileInputStream = null; try { //创建 FileInputStream 对象,用于读取文件 fileInputStream = new FileInputStream(filePath); //从该输入流读取一个字节的数据。如果没有输入可用,此方法将阻止 //如果返回-1.表示读取完毕 while((readData = fileInputStream.read()) != -1){ System.out.print((char)readData);//转成char显示 } } catch (IOException e) { e.printStackTrace(); }finally { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void readFile02(){ String filePath = "e:\\hello.txt"; byte[] buf = new byte[8];//一次读8个字节 int readLen = 0; FileInputStream fileInputStream = null; try { //创建 FileInputStream 对象,用于读取文件 fileInputStream = new FileInputStream(filePath); // 读取的字节数最多等于b的长度。 // 令k为实际读取的字节数; 这些字节将存储在元素b[0]至b[ k -1] ,使元素b[ k ]至b[b.length-1]不受影响。 // 返回读取到缓冲区的总字节数,或者如果没有更多的数据,因为已经到达流的末尾,则是 -1 。 while((readLen = fileInputStream.read(buf)) != -1){ System.out.print(new String(buf, 0, readLen));//显示 } } catch (IOException e) { e.printStackTrace(); }finally { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } /*运行结果 hello world hello world */
二、FileOutputStream
-
构造方法摘要
Constructor and Description FileOutputStream(File file)
创建文件输出流以写入由指定的File
对象表示的文件。FileOutputStream(File file, boolean append)
创建文件输出流以写入由指定的File
对象表示的文件。FileOutputStream(FileDescriptor fdObj)
创建文件输出流以写入指定的文件描述符,表示与文件系统中实际文件的现有连接。FileOutputStream(String name)
创建文件输出流以指定的名称写入文件。FileOutputStream(String name, boolean append)
创建文件输出流以指定的名称写入文件。 -
方法摘要
Modifier and Type Method and Description void
close()
关闭此文件输出流并释放与此流相关联的任何系统资源。protected void
finalize()
清理与文件的连接,并确保当没有更多的引用此流时,将调用此文件输出流的close
方法。FileChannel
getChannel()
返回与此文件输出流相关联的唯一的FileChannel
对象。FileDescriptor
getFD()
返回与此流相关联的文件描述符。void
write(byte[] b)
将b.length
个字节从指定的字节数组写入此文件输出流。void
write(byte[] b, int off, int len)
将len
字节从位于偏移量off
的指定字节数组写入此文件输出流。void
write(int b)
将指定的字节写入此文件输出流。
代码演示:
import org.junit.jupiter.api.Test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** * @author * @version 1.0 */ public class FileOutputStream_ { public static void main(String[] args) { } @Test public void writeFile(){ String filePath = "e:\\a.txt"; FileOutputStream fileOutputStream = null; try { String str = "Hello World!"; //得到FileOutputStream对象 //1. new FileOutputStream(filePath) 创建方式,写入内容会默认覆盖原来的内容 //2. new FileOutputStream(filePath, append) 创建方式,append 为ture,写入内容会默认追加至原来的内容后,否则就覆盖 fileOutputStream = new FileOutputStream(filePath, true); //写入一个字节 fileOutputStream.write('H'); //写入字符串,str.getBytes() 可以把 字符串 -》字符数组 fileOutputStream.write(str.getBytes()); /* write(byte[] b, int off, int len) 将 len 字节从位于偏移量 off的指定字节数组写入文件输出流 */ // fileOutputStream.write(str.getBytes(), 0, str.length()); } catch (IOException e) { e.printStackTrace(); }finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
本文作者:凉白茶
本文链接:https://www.cnblogs.com/zh-Note/p/17455290.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步