Java常用字节流和字符流实例汇总

2025-05-29 0 81

IO流(输入流、输出流)

字节流字符流

  1.字节流

  • InputStream、OutputStream
  • InputStream抽象了应用程序读取数据的方式;
  • OutputStream抽象了应用程序写出数据的方式;

  2.EOF=End 读到-1就读到结尾

  3.输入流的基本方法:

  • int b=in.read();读取一个字节无符号填充到int低八位;-1是EOF;
  • in.read(byte[] buf)
  • in.read(byte[] buf,int start,int size)

  4.输出流基本方法: 

  • out.write(int b) 写出一个byte到流,b的低8位;
  • out.write(byte[] buf) 将buf字节数组都写入到流;
  • out.write(byte[] buf,int start,int size)       

  5.FileInputStream—>具体实现了在文件上读取数据,下面请看最简单的文件读取:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29
package com.wxd.test2;

import java.io.FileInputStream;

import java.io.IOException;

public class IOUtil {

/**

* 读取指定文件内容,按照16进制输出到控制台

* 并且每输出10个byte换行

* @param fileName

*/

public static void printHex(String fileName) throws IOException {

//把文件作为字节流进行读操作

FileInputStream in =new FileInputStream(fileName);

int b;

int i=1;

while ((b=in.read())!=-1){

if(b<=0xf){

//单位数前面补0

System.out.print(0);

}

System.out.print(Integer.toHexString(b)+" ");

if(i++%10==0){

System.out.println();

}

}

in.close();

}

}

  6.FileOutputStream实现了向文件中写出byte数据的方法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24
package com.wxd.test2;

import java.io.FileOutputStream;

import java.io.IOException;

public class FileOutDemo1 {

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

//如果该文件不存在则直接创建,如果存在删除后创建

FileOutputStream out=new FileOutputStream("demo/out.dat");

out.write('A');//写出了‘A'字符的低八位

out.write('B');//写出了‘B'字符的低八位

int a=10;//write只能写低八位写一个int需要写四次每次8位

out.write(a>>>24);

out.write(a>>>16);

out.write(a>>>8);

out.write(a);

byte[] zg="中国".getBytes("UTF-8");

out.write(zg);

out.close();

IOUtil.printHex("demo/out.dat");

}

}

  7.DataOutputStream/DataInputStream对“流”功能的扩展,可以更加方便的读取int,long,字符等类型数据:

writeInt()/writeDouble()/writeUTF()  

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
package com.wxd.test2;

import java.io.DataOutputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class DosDemo {

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

String file="demo/dos.dat";

DataOutputStream dos=new DataOutputStream(new FileOutputStream(file));

dos.writeInt(10);

dos.writeInt(-10);

dos.writeLong(10l);

dos.writeDouble(10.5);

//采用UTF-8编码写出

dos.writeUTF("中国");

//采用utf-16be编码写出

dos.writeChars("中国");

dos.close();

}

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
package com.wxd.test2;

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.IOException;

public class DisDemo {

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

String file="demo/dos.dat";

DataInputStream dis=new DataInputStream(new FileInputStream(file));

int i=dis.readInt();//其实做了4次read,因为int是32位

System.out.println(i);

i=dis.readInt();

System.out.println(i);

long l=dis.readLong();//其实做了8次read,应为long是64位

System.out.println(l);

double d=dis.readDouble();

System.out.println(d);

String s=dis.readUTF();

System.out.println(s);

dis.close();

}

}

  8.BufferedInputStream&BufferedOutputStrean这两个流为IO提供了带缓冲区的操作,一般打开文件进行写入或读取操作时,都会加上缓冲,这种流模式提高了IO性能

从应用程序中把数据放入文件,相当于将一缸水倒入到另一个缸中:

  • FileOutputStream—>write()方法相当于一滴一滴地把水转移过去
  • DataOutputStream—>writeXxx()方法会方便一些,相当于一瓢一瓢的把水转移过去
  • BufferedOutputStream—>write()方法相当于一瓢一瓢先放入桶中,再从桶中倒入缸中
?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
/***

*进行文件的拷贝,利用带缓冲的字节流

* @param srcFile

* @param destFile

* @throws IOException

*/

public static void copyFileByBuffer(File srcFile,File destFile) throws IOException{

if(!srcFile.exists()){

throw new IllegalArgumentException("文件:"+srcFile+"不存在");

}

if(!srcFile.isFile()){

throw new IllegalArgumentException(srcFile+"不是文件!");

}

BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));

BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));

int c;

while ((c=bis.read())!=-1){

bos.write(c);

bos.flush();//刷新缓冲区,否则写入不到;

}

bis.close();

bos.close();

}

字符流

1.编码问题

2.认识文本和文本文件

3.Java的文本(char)是16位无符号的整数,是字符的unicode编码(双字节编码)文件是byte byte byte…的数据序列

  文本文件是文本(char)序列按照某种编码方案(utf-8,utf-16be,gbk)序列化为byte的存储

4.字符流(Reader Writer):—>操作的是文本文件

  字符的处理,一次处理一个字符

  字符的底层任然是基本的字节序列

  字符流的基本实现

    InputStreamReader 完成byte流解析为char流,按照编码解析

    OutputStreamWriter 提供char流到byte流,按照编码处理

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29
package com.wxd.test2;

import java.io.*;

public class IsrAndOswDemo {

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

//InputStreamReader被称为桥梁流

FileInputStream in=new FileInputStream("demo\\\\test.txt");//默认项目的编码,操作的时候要写文件本身的编码

InputStreamReader isr=new InputStreamReader(in,"utf-8");

FileOutputStream out=new FileOutputStream("demo\\\\test2.txt");

OutputStreamWriter osw=new OutputStreamWriter(out,"utf-8");

// int c;

// while ((c=isr.read())!=-1){

// System.out.print((char)c);

// }

char[] buffer=new char[8*1024];

int c;

//批量读取,放入buffer这个字符数组,从第0个位置开始放置,最多放buffe.length个

//返回的是读到的字符的个数

while ((c=isr.read(buffer,0,buffer.length))!=-1){

String s=new String(buffer,0,c);

System.out.println(s);

osw.write(buffer,0,c);

osw.flush();

}

isr.close();

osw.close();

}

}

5.FileReader/FileWriter(这种方式bu)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
package com.wxd.test2;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class FrAndFwDemo {

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

FileReader fr=new FileReader("demo\\\\test.txt");

FileWriter fw=new FileWriter("demo\\\\text2.txt",true);//设置为true在后面追加

char[] buffer=new char[2056];

int c;

while ((c=fr.read(buffer,0,buffer.length))!=-1){

fw.write(buffer,0,c);

fw.flush();

}

fr.close();

fw.close();

}

}

6.字符流的过滤器

  BufferedReader —–>readLine一次读一行

  BufferedWriter/PrintWriter —->写一行

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
package com.wxd.test2;

import java.io.*;

public class BrAndBwOrPwDemo {

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

//对文件进行读写操作

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("demo\\\\test.txt")));

/*BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("demo\\\\test3.txt")));*/

PrintWriter pw = new PrintWriter("demo\\\\test3.txt");

String line;

while ((line = br.readLine()) != null) {

System.out.println(line);//一次读一行,并不能识别换行

/*bw.write(line);

//单独写出换行操作

bw.newLine();

bw.flush();*/

pw.println(line);

pw.flush();

}

br.close();

// bw.close();

pw.close();

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://www.cnblogs.com/MrXiaoAndDong/p/IO.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java常用字节流和字符流实例汇总 https://www.kuaiidc.com/119255.html

相关文章

发表评论
暂无评论