java使用转换流复制_Java对IO中每个流的理解心得,字符流字节流,转换流复制,对象流,分割流【诗书画唱】...

写一下你对IO流的心得,对于每个流的理解

如果我们把水当做文件,把水工厂当做file类,将运输用的大小水管当做inputstream,outputstream,reader,writer,再把瓢子,盘,水桶等容器当做数组,char等。file类是可以放很多很多的水的大仓库,通过inputstream,outputstream,reader,writer这四个节点流(=水管),将水运输到社区,最后,用不同容器(=数组,char)将水装出来用。

字节流:一次读入或读出是8位二进制

字符流:一次读入或读出是16位二进制

JDK 中后缀是 Stream 是字节流;后缀是 Reader,Writer 是字符流

节点流:直接与数据源相连,读入或写出

处理流:与节点流一块使用,在节点流的基础上,再套接一层

常用的流对文件进行操作:FileInputStream(字节输入流)、FileOutputStream(字节输出流)、FileReader(字符输入流)、FileWriter(字符输出流)

对管道进行操作:PipedInputStream(字节输入流)、PipedOutStream(字节输出流)、PipedReader(字符输入流)、PipedWriter(字符输出流)

字节/字符数组:ByteArrayInputStream、ByteArrayOutputStream、CharArrayReader、CharArrayWriter

Buffered 缓冲流:BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter

字节转化成字符流:InputStreamReader、OutputStreamWriter

数据流:DataInputStream、DataOutputStream

打印流:PrintStream、PrintWriter

对象流:ObjectInputStream、ObjectOutputStream

序列化流:SequenceInputStream

使用字符流(就是用上BufferedReader等)复制一个文本文件

81c9f58a79ea858ce83362cf6c89a5ca.png

//字节流:

//FileInputStream:字节输入流

//FileOutputStream:字节输出流

//字节流是可以读取任何文件,每次读取的时候是1字节

//缓冲流(包装流):

//BufferedReader:字符输入缓冲流

//BufferedWriter:字符输出缓冲流

//BufferedInputStream:字节输入缓冲流

//BufferedOutputStream:字节输出缓冲流

//为了提高读写流的效率,引入了缓冲机制,

//进行批量的读写,提高了读写的效率。

//Buffered包装类用于加快了读写内容的速度转换流:

//两个功能:1.将输入的字节转换为字符

//2.进行编码转换

//InputStreamReader:字节输入转换流

//OutputStreamWriter:字节输出转换流

package all;

import java.io.*;

public class copy {

public static void main(String[] args) throws IOException {

File old = new File("xiangDui.txt");

File copy = new File("xiangDuiCopy.txt");

CopyFangFa(old,copy);

}

public static void CopyFangFa(File old,File copy)

throws IOException {

//用FileInputStream等来创建字节输入输出流对象:

FileInputStream byteInput = new FileInputStream(old);

FileOutputStream byteOut = new FileOutputStream(copy);

//复制过程用字节数组和write写入来实现:

//用每次读取1024的字节的话,执行所花的时间就会变得更少。

int len;

byte[] byteArray = new byte[1024];

while ((len = byteInput.read(byteArray)) != -1) {

byteOut.write(byteArray, 0, len);

}

//用close,关闭,之后可释放资源:

byteOut.close();

byteInput.close();

}

}

ef6a5653b99e4ffd768e6a2d60129089.png

b09f32e17e988d5a7e399fcf00c3c527.png

使用字节流(FileInputStream等)复制一个图片

28d54f166c454b9cf32d065deec95cfb.png

package IO;

import java.io.*;

public class copyImg {

public static void main(String[] args) throws Exception {

File oldFile=new File("7.jpg");

BufferedInputStream CharInput=

new BufferedInputStream(

new FileInputStream(oldFile));

BufferedOutputStream CharOut=

new BufferedOutputStream(new FileOutputStream("7Copy.jpg"));

byte[] byteArray=new byte[1024];

int length=0;

while((length=CharInput.read(byteArray))!=-1){

CharOut.write(byteArray,0,length);

}

CharOut.flush();

CharOut.close();

CharInput.close();

System.out.println("复制成功!");

}}

2d5973ac9ac4e0531f7398411c141d43.png

0d9068356e0be3187e22fd360231da84.png

6ae2278124270a0ccc8a6e33092d3ba0.png

使用缓冲流复制一个文件夹和里面的所有文件

6965d8c7c142aedfaf11f9689b6ec9df.png

package all;

import java.io.*;

public class copyWenJianjia {

public static void copyAllFanFa(File oldFile, File copyFile)

throws Exception{

//用isDirectory判断原来的老的文件是否是文件夹

if (oldFile.isDirectory()) {

// 被复制的文件没复制成功,不存在,就创建文件夹

if (!copyFile.exists()) {

copyFile.mkdir();

}

// 将文件夹下的文件存入文件数组StringArray(字符串数组)

String[] StringArray = oldFile.list();

for (String File : StringArray) {

//用new等创建文件夹下的子目录,src:路径或目录

File oldFileSrc = new File(oldFile, File);

File copyFileSrc = new File(copyFile, File);

// 个人的理解:在自身方法中调用自身的方法,

//                  这样的话调用一次方法,就是调用了

//                  很多次正在循环执行的方法

//                  直到文件等遍历完了,就是将文件进行下一层循环。

copyAllFanFa(oldFileSrc, copyFileSrc);

}

} else {

// 创建FileInputStream(文件输入字节流)

//        :  用于读取文件内容,源文件

FileInputStream byteInput = new FileInputStream(oldFile);

// 创建FileOutputStream(文件输出的字节流)

//              ,用于将读取到的文件内容

//              写到另一个磁盘文件中,复制目标文件等

FileOutputStream byteOut =

new FileOutputStream(copyFile);

int len = -1;

byte[] byteArray = new byte[1024];

while (true) {

// 从FileInputStream(文件输入流)

//     中读取数据。每执行一次,数据读到字节数组b中

len = byteInput.read(byteArray, 0, 256);

if (len == -1) {

break;

}

//                  System.out.println(byteArray.toString());

byteOut.write(byteArray);

}

//              byteOut.write("\r\n".getBytes()); // 换行

byteInput.close();

byteOut.close();

}

}

public static void main(String[] args)

throws Exception{

copyAllFanFa(new File("NewFile"),new File("NewCopyFile"));

}

}

f357a96364d6751b2419b8e8f94ddac7.png

04f9b1d452596a597359871a17061eb3.png

使用转换流复制一个文件夹下的所有txt文件,将文件的编码格式转换为utf-8

3c10f13b2801088de5b43015880064d6.png

package all;

import java.io.*;

public class copyAlltxt {

public static void copyAllFanFa(File oldFile, File copyFile)

throws Exception{

if (oldFile.isDirectory()) {

if (!copyFile.exists()) {

copyFile.mkdir();

}

String[] StringArray = oldFile.list();

for (String File : StringArray) {

File oldFileSrc = new File(oldFile, File);

File copyFileSrc = new File(copyFile, File);

copyAllFanFa(oldFileSrc, copyFileSrc);

}

} else {

FileInputStream byteInput =

new FileInputStream(oldFile);

InputStreamReader byteChangeInput=

new InputStreamReader(byteInput,"UTF-8");

//      InputStreamReader:字节输入转换流(个人的理解:

//      把一些内容转格式后保存。)

FileOutputStream byteOut =

new FileOutputStream(copyFile);

char[] charArray=new char[100];

int len;

byte[] byteArray= new byte[1024];

while((len=byteChangeInput.read(charArray))!=-1){

byteOut.write(byteArray);

}

byteChangeInput.close();

byteInput.close();

byteOut.close();

}

}

public static void main(String[] args)

throws Exception{

copyAllFanFa(new File("NewFile"),new File("NewCopyFile"));

}

}

af215b8b089103a72770044408c2ba43.png

11b31e286a91b2559b78065b5847db98.png

使用分割流复制一个文本文件

1d18558507aeea675307710b8f978075.png

//字节流:

//FileInputStream:字节输入流

//FileOutputStream:字节输出流

//字节流是可以读取任何文件,每次读取的时候是1字节

//缓冲流(包装流):

//BufferedReader:字符输入缓冲流

//BufferedWriter:字符输出缓冲流

//BufferedInputStream:字节输入缓冲流

//BufferedOutputStream:字节输出缓冲流

//为了提高读写流的效率,引入了缓冲机制,

//进行批量的读写,提高了读写的效率。

//Buffered包装类用于加快了读写内容的速度转换流:

//两个功能:1.将输入的字节转换为字符

//2.进行编码转换

//InputStreamReader:字节输入转换流

//OutputStreamWriter:字节输出转换流

package fenGeLiu;

import java.io.*;

public class copy {

public static void main(String[] args) throws IOException {

File old = new File("xiangDui.txt");

File copy = new File("xiangDuiCopy.txt");

CopyFangFa(old,copy);

}

public static void CopyFangFa(File old,File copy)

throws IOException {

//用FileInputStream等来创建字节输入输出流对象:

RandomAccessFile byteInput=new RandomAccessFile(old,"r");

//r:是固定不可变的“key”一般的,代表read的部分

RandomAccessFile byteOut=new RandomAccessFile(copy,"rw");

//rw:是固定不可变的“key”一般的,代表read后的write的部分

//——————————————————

//FileInputStream byteInput = new FileInputStream(old);

//

//FileOutputStream byteOut = new FileOutputStream(copy);

//——————————————————————

//复制过程用字节数组和write写入来实现:

//用每次读取1024的字节的话,执行所花的时间就会变得更少。

int len;

byte[] byteArray = new byte[1024];

while ((len = byteInput.read(byteArray)) != -1) {

byteOut.write(byteArray, 0, len);

}

//用close,关闭,之后可释放资源:

byteOut.close();

byteInput.close();

}

}

70b11ce954722048166cb044cf9ab089.png

c825df4b32475870ae09adc24d5dd32f.png

使用对象流保存一个对象的信息

38c536c01aa5431a8d4f171a98144a9e.png

package fenGeLiu;

import java.io.*;

import java.util.Date;

public class baoLiuOneObject {

public static void main(String[] args) throws Exception{

student duiXiang1=new student(666,"诗书画唱三连关注",100.0f);

File File=new File("new.txt");

FileOutputStream FileOutput=new FileOutputStream(File);

ObjectOutputStream ObjectOutput

=new ObjectOutputStream(FileOutput);

ObjectOutput.writeObject(duiXiang1);

ObjectOutput.writeObject(null);

ObjectOutput.close();

System.out.println("存储成功");

//下面是打印所有的存在new.txt的文件的对象的内容:

//File File=new File("new.txt");

//传入字节输入流:

FileInputStream byteInputAll=new FileInputStream(File);

//duiXiangInput【“拼音+英文”的自己的命名】(对象输入流)。

ObjectInputStream duiXiangInput

=new ObjectInputStream(byteInputAll);

Object duiXiang="";

while((duiXiang=duiXiangInput.readObject())!=null){

System.out.println(duiXiang.toString());

}

duiXiangInput.close();

}

}

class student implements Serializable{

int bianHao;

String name;

//transient float chengJi;

float chengJi;

//这里用transient来让成绩(chengJi)不参与序列化,

//就是后面用toString方法返回的时候,

//不会打印出传入的真实成绩放的内容。

public student(int bianHao, String name, float chengJi) {

this.bianHao = bianHao;

this.name = name;

this.chengJi = chengJi;

}

@Override

public String toString() {

return "编号:" +

bianHao + ", 姓名:" + name + ", 成绩("

+ "若该变量数据类型前"

+ "\n设置为transient,就不能打印传入的内容"

+ "\n出来的成绩默认为0.0):" +chengJi+ "\n";

}

}

e37cdc9e414164544f7acde1cdd161a1.png

d580bde942596f8321197669964e38d0.png

b905da4a8dc5ea1b51f80b410af00316.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值