FileInputStream详解
FileOutputStream详解
FileInputStream和FileOutputStream结合使用实现文件拷贝
能实现的效果:通过该流可以读取硬盘中的文件数据到内存中来。
创建并且关闭一个文件字节输入流的完整过程
public static void main(String[] args) {
FileInputStream fis = null;
try {
// 绝对路径可以写为:"C:/Users\yukea/Desktop/JavaProject/Test/FileInputStreamTest.txt",也可以用相对路径
fis = new FileInputStream("C:\\Users\yukea\\Desktop\\JavaProject\\Test\FileInputStreamTest.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
使用int read()
读取文件中的数据——返回读取到的字节本身
try {
//.txt文件中存储了abc三个字符,在windows中占用3个字节空间
fis = new FileInputStream("C:\\ProgramData\\Test\\FileInputStreamTest.txt");
fis.read();// 返回97
fis.read();// 返回98
fis.read();// 返回99
fis.read();// 返回-1
}
原理:有一个指针会随着Read()方法的调用而前移,首次调用Read()方法时指针前移指向.txt文件中的第一个字节,调用完后返回该字节数,后面每调用一次Read()方法则指针向后移动一位并且返回对应的字节数,直到指针指向文件末尾时返回-1。
使用int read(byte[] b)
读取文件中的数据——返回读取到的字节数量
try {
//.txt文件中存储了abcdef五个个字符
fis = new FileInputStream("C:\\ProgramData\\Test\\FileInputStreamTest.txt");
byte[] bytes = new byte[4];
fis.read(bytes);// 返回4,读取到了4个字节到bytes数组中
System.out.println(new String(bytes));// 打印 "abcd" 字符串
fis.read(bytes);// 返回2,读取到了2个字节到bytes数组中
System.out.println(new String(bytes));// 打印 "efcd" 字符串
fis.read(bytes);// 返回-1,没有读取到其他字节
}
原理:每次最多读取bytes数组长度的字节。
第一次读取:{97, 98 ,99 ,100} 4个字节保存到了字节数组中,返回读取到的字节数量4
第二次读取:{101, 102, 99, 100} 文件中的后两个字节覆盖到了字节数组中,返回读取到的字节数量2
第三次读取:返回-1,因为已读取到文件末尾
int read(byte[] b)
结合String构造方法使用——读取多少字节转换为多长字符串
try {
//.txt文件中存储了abcdef五个个字符
fis = new FileInputStream("C:\\ProgramData\\Test\\FileInputStreamTest.txt");
byte[] bytes = new byte[4];
int readCount = 0;
readCount = fis.read(bytes);// 返回4,读取到了4个字节到bytes数组中
System.out.println(new String(bytes, 0, readCount));// 打印 "abcd" 字符串
readCount = fis.read(bytes);// 返回2,读取到了2个字节到bytes数组中
System.out.println(new String(bytes, 0, readCount));// 打印 "ef" 字符串
}
int available()
——返回文件中剩余的没有读取到的字节数量
long skip(long n)
——跳过几个字节不读取
int available()
配合int read(byte[] b)
——不使用循环,一次性读取完文本中的字节。
try {
fis = new FileInputStream("Test/FileInputStreamTest.txt");
// 创建一个长度恰好为文本字节数量的bytes数组
byte[] bytes = new byte[fis.available()];
// 把文件中的字节读取到bytes数组中
fis.read(bytes);
// bytes数组转为字符串
String s = new String(bytes);
System.out.println(s);// 输出"莫莫你好hello"
}
先创建一个.txt文件
代码实现——将文件内容保存到一个String字符串上
FileInputStream fis = null;
try {
fis = new FileInputStream("Test/FileInputStreamTest.txt");
byte[] bytes = new byte[12];
int readCount = 0;
StringBuffer sb = new StringBuffer(10);
while ((readCount = fis.read(bytes)) != -1) {
String s = new String(bytes, 0, readCount);
sb.append(s);
}
System.out.println(sb);// 打印 "莫莫你好hello"
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
能实现的效果:通过该流可以将内存中的数据写入到硬盘数据文件中去。
创建并且关闭一个文件字节输出流的完整过程
try {
// 如果文件不存在则会自动新建一个文件
fos = new FileOutputStream("Test/FileOutputStreamTest.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
将指定字节数组中的字节写到流中的方法
(1) void write(byte[] b)
(2) void write(byte[] b, int off, int len)
try {
fos = new FileOutputStream("Test/FileOutputStreamTest.txt");
byte[] bytes = {97, 98, 99, 100};
fos.write(bytes);// 将数组中的全部字符写入
fos.write(bytes, 0, 2);// 将数组中从下标0开始写入2个长度的字节(接着上面写入的字节后面)
// 写完后一定要刷新!
fos.flush();
// 刷新后该文本文件中出现abcdab
}
注意:使用上述方法将字节写入文件,写入之前会先清空文件的内容再进行写入。
以追加的方式写入:fos = new FileOutputStream("Test/FileOutputStreamTest.txt", ture);
配合String类的byte[] getBytes()
方法使用——将字符串转换为字节数组,向文件中写入字符串
try {
FileOutputStream fos = new FileOutputStream("Test/FileOutputStreamTest.txt");
String s = "莫莫大包子";
fos.write(s.getBytes());
fos.flush();
}
String originFilePath = "/Users/apple/Downloads/FinalVideo_1591626463.139785.MOV";
String targetFilePath = "Test/copyFile/copiedFile2.MOV";
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(originFilePath);
fos = new FileOutputStream(targetFilePath);
// 一次读取1MB
byte[] bytes = new byte[1024 * 1024];
int readCount = 0;
// 读取多少写入多少
while ((readCount = fis.read(bytes)) != -1) {
fos.write(bytes, 0, readCount);
}
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文地址:https://blog.csdn.net/k909397116/article/details/107363246
如对本文有疑问, 点击进行留言回复!!
springmvc之ResponseBody响应json数据遇到的错误及解决
uni-app 后台升级 静默升级 uniapp 后台更新 静默更新 在线升级
SpringBoot多Module启动报错Could not transfer metadata
Hibernate项目报错:Cannot call sendError() after the response has been committed
网友评论