字节输入流FileInputStream
创建对象:FileInputStream fileInputStream = new FileInputStream(路径);
FileInputStream.read() 从该输入流读取一个字节的数据如果达到文件的末尾, -1
FileInputStream.read(byte [] b)从该输入流读取最多b.length
字节的数据到字节数组,返回值是读入缓冲区的总字节数,如果没有更多的数据,因为文件的结尾已经到达,返回 -1
FileInputStream.read(byte[] b, int off, int len),在b数组中从下标off开始读取长度为len的字符串
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStream {
public static void main(String[] args) throws IOException {
readFile01();
readFile02();
}
//从该输入流读取一个字节的数据,如果达到文件的末尾, -1
public static void readFile01() throws IOException {
int readData=0;
String path = "e:\\文件.txt";
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(path);
while ((readData=fileInputStream.read())!=-1){
System.out.print((char)readData);
}
fileInputStream.close();
System.out.println();
}
//从该输入流读取最多b.length字节的数据到字节数组
//返回值为读入缓冲区的总字节数,如果没有更多的数据,因为文件的结尾已经到达, -1 。
public static void readFile02() throws IOException {
String path = "e:\\文件.txt";
byte[] by = new byte[8];
int readData = 0;
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(path);
while ((readData=fileInputStream.read(by))!=-1){
for (int i = 0; i < readData; i++) {
System.out.print((char)by[i]);
}
}
System.out.println();
}
}
字节输出流FileOutPutStream
创建对象:
FileOutputStream fileOutputStream = new FileOutputStream(path);
fileOutputStream.write(int b):将指定的字节写入此文件输出流b
- 要写入的字节。
fileOutputStream.write(byte[] b):将 b.length
字节从指定的字节数组写入此文件输出流
fileOutputStream.write(byte[] b, int off, int len):从位于偏移量 off
的指定字节数组写入 len
字节到该文件输出流.
new FileOutputStream(path)为覆盖写,之前写的内容会被覆盖掉,文件中只有最后一次写入的内容
new FileOutputStream(path,true)为追加写,不会覆盖原先内容
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutPutStream {
public static void main(String[] args) throws IOException {
writeFile();
}
/*
* new FileOutputStream(path)为覆盖写之前写的内容会被覆盖掉,文件中只有最后一次写入的内容
* new FileOutputStream(path,true)为追加写,不会覆盖原先内容
* */
//写入一个字节
public static void writeFile() throws IOException {
String path = "e:\\a.txt";
FileOutputStream fileOutputStream = new FileOutputStream(path,true);
//只写入单个字符
//fileOutputStream.write('L');
//写入字符串
// String str = "hello";
// fileOutputStream.write(str.getBytes());
//将 len字节从位于偏移量 off的指定字节数组写入此文件输出流,len表示要从off开始读几个字符
String str1 = "world";
fileOutputStream.write(str1.getBytes(),0,2);
}
}
Filecopy
要边读入边输出,如果一次全读入再输出文件过大时会导致内存不够
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) throws IOException {
String srcpath = "D:\\图片\\背景花.jpeg";
String outpath = "E:背景花.jpeg";
byte[] by = new byte[1024];
int readData = 0;
//要边读入边输出
FileInputStream fileInputStream = new FileInputStream(srcpath);
FileOutputStream fileOutputStream = new FileOutputStream(outpath);
while ((readData=fileInputStream.read(by))!=-1){
fileOutputStream.write(by,0,readData);
}
System.out.println("拷贝成功");
fileInputStream.close();
fileOutputStream.close();
}
}