Java流操作之数据流实例代码

2025-05-29 0 4

实例1:

?

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

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64
package dataInputStreamAndPrintStreamDemo;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.PrintStream;

//示范如何自键盘读入字符串,并使用DataInputStream,PrintStream类将程序执行显示在屏幕(标准输出)上

public class DataInputStreamAndPrintStreamDemo {

public static void main(String[] args) {

int count;

byte input[] = new byte[256];

String InputString;

// 键盘读入

DataInputStream stdin = new DataInputStream(System.in);

//提高执行效率,几乎所有的InputStream类都可以被BufferedStream类包覆(wrap)来提高I/O效率

BufferedInputStream bufin = new BufferedInputStream(stdin);

// 屏幕输出

DataOutputStream stdout = new DataOutputStream(System.out);// 将结果输出至屏幕

BufferedOutputStream bufout = new BufferedOutputStream(stdout);// 提高输出效率

PrintStream p = new PrintStream(System.out);// 将结果输出至屏幕

try {

if (bufin.markSupported()) {

p.println("支持串流标记:是");// 使用PrintStream输出

p.println("输入字符串,结束请按【Enter】...\\n" + "=>");

//使得流在第一个位被作上标记(mark),并且会保留256位(mark(256))

bufin.mark(256);

//读取字节并存放在指定的数组中

count = bufin.read(input);

p.println("读入字符数:" + count);

p.print("你输入的字符串为:");

// 写入流,只是将数据写入流中而已,并不输出数据

// 所以在其后必须使用flush()函数将流中的数据强制输出

bufout.write(input, 0, count);

bufout.flush();// 强制输出至指定的输出装置

bufin.reset();// 将读取位置移至标记处,也就是流中的第一位

bufin.read(input, 0, count);

p.print("字符串的前半段:");

bufout.write(input, 0, count / 2);

//相当于System.out.println();

bufout.write((int)('\\n'));

bufout.flush();

bufin.reset();

bufin.skip(count / 2);

bufin.read(input, 0, count / 2);

p.print("字符串的后半段:");

bufout.write(input, 0, count / 2);

bufout.flush();

} else {

System.out.println("字符串流标记:否");

}

// 关闭流

p.close();

stdin.close();

bufin.close();

stdout.close();

bufout.close();

} catch (IOException E) {

System.out.println("发生I/O错误!!!");

}

}

}

//其实我们对PrintStream类应该很熟悉才对,System.out就是一个PrintStream类对象,其提供的print()和println()函数

//几乎可显示所有数据类型的变量

?

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

30

31

32

33

34

35

36

37

38

39
//例程2:package iotest;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.PrintStream;

import java.util.logging.Level;

import java.util.logging.Logger;

public class IOtest {

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

byte buf[] = new byte[255];

byte bufin[] = new byte[255]; //只能用byte格式将数据送入文件

String str = "输入的文字:";

buf = str.getBytes();

try {

FileOutputStream fout = new FileOutputStream("test.txt");

PrintStream p = new PrintStream(fout);

p.println("输入的文字~~~~~~~"+'\\n'); //方式一

fout.write(buf, 0, buf.length); //方式二

fout.write(buf); //方式三

//fout.flush();

//fout.close();

System.out.println("快输入文字:");

int bytes = System.in.read(bufin, 0, 255);

//追加文本!!!!!!!!!!!!!!!!

//fout = new FileOutputStream("test.txt",true);

fout.write(bufin, 0, bytes);

} catch (FileNotFoundException ex) {

Logger.getLogger(IOtest.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

结果:

?

1

2

3

4
//输入的文字~~~~~~~

//输入的文字:输入的文字:鍩庡競宸ヤ笟 fdsfdssssssssssssssssssssssssssss

总结

以上就是本文关于Java流操作数据流实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/sac761/article/details/47037063

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 Java流操作之数据流实例代码 https://www.kuaiidc.com/113204.html

相关文章

发表评论
暂无评论