好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

简述:

观察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:   ~

总结

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

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

查看更多关于Java中byte输出write到文件的实现方法讲解的详细内容...

  阅读:26次