Java中byte输出write到文件的实现方法讲解

2025-05-29 0 68

简述:

观察byte值转为字符写入文件

如果在java里用byte打印出来

只有33 到 126的输出字符比较正常

此外发现byte值为13是空格,10是换行符

知识点:

1. string 转为byte输出("utf-8"格式)

2. fileoutputstream 使用输出文件流

代码:

?

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
package testchar;

import java.io.file;

import java.io.filenotfoundexception;

import java.io.fileoutputstream;

import java.io.ioexception;

public class testchar {

public static void main(string[] args){

file outputfile = new file("output.txt");

fileoutputstream outputfilestream = null;

// try to open file output.txt

try {

outputfilestream = new fileoutputstream(outputfile);

} catch (filenotfoundexception e) {

e.printstacktrace();

}

//output to output.txt

for(int i = 33;i < 127;i++){

try {

string numstr = i + ": ";

byte[] numbytes = numstr.getbytes("utf-8");

outputfilestream.write(numbytes);

//i lies in [33, 127)

outputfilestream.write(i);

outputfilestream.write("\\n".getbytes());

} catch (ioexception e1) {

e1.printstacktrace();

}

}

//close file stream

try {

outputfilestream.close();

} catch (ioexception e) {

e.printstacktrace();

}

}

}

byte从33 到 126 的字符输出:

output.txt 用notepad打开:

33: !
34: "
35: #
36: $
37: %
38: &
39: '
40: (
41: )
42: *
43: +
44: ,
45: –
46: .
47: /
48: 0
49: 1
50: 2
51: 3
52: 4
53: 5
54: 6
55: 7
56: 8
57: 9
58: :
59: ;
60: <
61: =
62: >
63: ?
64: @
65: a
66: b
67: c
68: d
69: e
70: f
71: g
72: h
73: i
74: j
75: k
76: l
77: m
78: n
79: o
80: p
81: q
82: r
83: s
84: t
85: u
86: v
87: w
88: x
89: y
90: z
91: [
92: \\
93: ]
94: ^
95: _
96: `
97: a
98: b
99: c
100: d
101: e
102: f
103: g
104: h
105: i
106: j
107: k
108: l
109: m
110: n
111: o
112: p
113: q
114: r
115: s
116: t
117: u
118: v
119: w
120: x
121: y
122: z
123: {
124: |
125: }
126: ~

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/anialy/article/details/7961179

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 Java中byte输出write到文件的实现方法讲解 https://www.kuaiidc.com/109647.html

相关文章

发表评论
暂无评论