流是以内存为对象,
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bDTvo6wS-1620884654001)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506081448354.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bmO7Q08a-1620884654003)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506081600249.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-X5WcvhLF-1620884654004)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506092005527.png)]
创建一级目录用mkdir,,,,多级目录用mkdirs
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Sx6cW4qg-1620884654007)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506095216592.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EScmVM3X-1620884654008)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506100127015.png)]
字节流与字符流
用字节流传输文件精细,无损操作
一般用字符流来传输文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ci4N27Pq-1620884654010)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506103048829.png)]
用流对象来关联硬盘中的文件,通过流来操作文件,使用流后一定要关闭流
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NFxKso1u-1620884654011)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506121758423.png)]
字节流读取数据,效率低,中文乱码,因为中文由三个字节组成。
read()一次读一个字节数据,如果没有可输入的字节,此方法将阻止。返回-1,表示读取完毕
public static void FileInputStram() {
String filePath = "F:\\code\\javaIO创建的文件.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(filePath);
while ((readData = fileInputStream.read()) != -1){
System.out.print((char) readData);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
//一定要关闭流
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
用字节数组来接收数据,一次接收多少自定义,快捷
FileInputStream fileInputStream = null;
byte[] but = new byte[8];
try {
fileInputStream = new FileInputStream(filePath);
while ((readData = fileInputStream.read(but)) != -1){
System.out.println(new String(but,0,readData));
}
字节输出流
String filePath = "F:\\code\\a.txt";
FileOutputStream fileOutputStream = null;
try {
//fileOutputStream = new FileOutputStream(filePath, true);追加输出
//fileOutputStream = new FileOutputStream(filePath);这样创建会覆盖原来文件的内容
fileOutputStream = new FileOutputStream(filePath);
String txt = "Hello.world";
//fileOutputStream.write('H');字节可以转成int
fileOutputStream.write(txt.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PTfiz5Jn-1620884654011)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506135200605.png)]
字符流,Reader
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mPGnlgNj-1620884654012)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506144627179.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fHozBO0j-1620884654013)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506144806019.png)]
字符流,fileReader
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UsmXxJei-1620884654013)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506145512218.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ztDWtwjJ-1620884654014)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506151055784.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cczpBLYm-1620884654014)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210506170345285.png)]
BufferedWriter缓冲流中有一个属性Writer,即可以接收Writer的所有子类,BufferedReader也是这样,排除了节点流对数据源格式的限制
用字节流处理二进制文件(视屏,图片)。用字符流处理文本文档
BufferedReader读取文件
String filePath = "F:\\code\\aa.txt";
//创建bufferedReader
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
//读取
String line;//按行读取,效率高 bufferedReader.readLine()按行读取文件
//当bufferedReader.readLine();返回值为null时,表示文件读取完毕
while ((line = bufferedReader.readLine()) != null){
System.out.println(line);
}
//一定要记得关闭流
bufferedReader.close();//fileReader会自动关闭
//1, BufferedReader与BufferedWriter,都是按照字符操作的
//2, 不要用他们去操作二进制文件,可能会照成损坏
String inputPath = "F:\\code\\a.txt";
String outputPath = "F:\\code\\a1.txt";
BufferedReader bf = null;
BufferedWriter bw = null;
String line;
try {
bf = new BufferedReader(new FileReader(inputPath));
bw = new BufferedWriter(new FileWriter(outputPath));
while ((line = bf.readLine()) != null){
//每读一行,就写一行
bw.write(line);
bw.newLine();
}
}catch(IOException e){
e.printStackTrace();
}finally {
if (bf != null) {
try {
bf.close();
} catch (IOException e) {
e.printStackTrace();
用字节缓冲流来(包装流),来拷贝二进制文件,
//拷贝图片,因为图片是二进制的,所以得用字节流来传输
String Apath = "F:\\code\\1.jpeg";
String Bpath = "F:\\code\\2.jpeg";
BufferedInputStream bi = null;
BufferedOutputStream bo = null;
//用于存放一次读出来的数据
byte[] buf = new byte[1024];
int line = 0;
try {
bi = new BufferedInputStream(new FileInputStream(Apath));
bo = new BufferedOutputStream(new FileOutputStream(Bpath,true));
//当结果为-1时,表示读取文件完毕
while ((line = bi.read(buf)) != -1){
bo.write(buf,0,line);
}
} catch (Exception e) {
e.printStackTrace();
}finally {//必须关闭打开的流接口
if (bi != null){
try {
bi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bo != null){
try {
bo.close();
字节包装流(BufferedInputStream)既可以操作二进制文件,也可以操作文本文档,并且不会乱码。而字符包装流不能操作二进制文件,
对象处理流(ObjectInputStream)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-F8UjFZHR-1620884654015)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210507093010081.png)]
ObjectOutputStream写入对象时,该对象需要被序列化(实现Serializable接口)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c1kQ2cOz-1620884654016)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210507094507611.png)]
反序列化
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PcF5cTgz-1620884654017)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210507095454157.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uki9L6w7-1620884654017)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210507095407894.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Wryml4Nh-1620884654018)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210507100619992.png)]
注意事项
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RSMgYyWi-1620884654019)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210507100815611.png)]
处理字符流因编码问题导致的乱码,,,因为字节流默认是以utf来处理文件,
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cLxFAcsx-1620884654019)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210507132848830.png)]
字节流乱码解决,将字节流转换为字符流
String Apath = "F:\\code\\a.txt";
byte[] buf = new byte[8];
int line;
FileInputStream fis = new FileInputStream(Apath);
while ((line = fis.read(buf)) != -1){
System.out.println(new String(buf,0,line));
}
输出乱码
Hello.wo
rld
手
动阀�
�分士�
��夫
而用包装流,将字节流转换为字符流,可指定编码格式
String Apath = "F:\\code\\a.txt";
String line;
//什么变么的文本文档就用什么变么打开
InputStreamReader psr = new InputStreamReader(new FileInputStream(Apath), "utf-8");
BufferedReader br = new BufferedReader(psr);
while ((line = br.readLine()) != null){
System.out.println(line);
}
一定要记得关闭流 br.close();
有意思的标准打印流(System.out.print)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0ic993Oc-1620884654020)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210507152100713.png)]
propertiece文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eaGU8rhz-1620884654020)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210507153926325.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ljiAokQh-1620884654021)(C:\Users\47669\AppData\Roaming\Typora\typora-user-images\image-20210507161901852.png)]