Java中byte輸出write到文件的實(shí)現(xiàn)方法講解
簡(jiǎn)述:
觀察Byte值轉(zhuǎn)為字符寫入文件
如果在java里用byte打印出來(lái)
只有33 到 126的輸出字符比較正常
此外發(fā)現(xiàn)Byte值為13是空格,10是換行符
知識(shí)點(diǎn):
1. String 轉(zhuǎn)為Byte輸出("UTF-8"格式)
2. FileOutputStream 使用輸出文件流
代碼:
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: ~
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Java四種常用線程池的詳細(xì)介紹
- Java中字符數(shù)組和字符串與StringBuilder和字符串轉(zhuǎn)換的講解
- Java中json與javaBean幾種互轉(zhuǎn)的講解
- Java多線程工具篇BlockingQueue的詳解
- Java中Timer的schedule()方法參數(shù)詳解
- JavaScript使用小插件實(shí)現(xiàn)倒計(jì)時(shí)的方法講解
- Java調(diào)用CXF WebService接口的兩種方式實(shí)例
- java模擬ajax訪問(wèn)另一個(gè)項(xiàng)目的controller代碼實(shí)例
- Java實(shí)現(xiàn)批量修改txt文件名稱的方法示例
- MapStruct處理Java中實(shí)體與模型間不匹配屬性轉(zhuǎn)換的方法
相關(guān)文章
java從控制臺(tái)接收一個(gè)數(shù)字的實(shí)例詳解
這篇文章主要介紹了java從控制臺(tái)接收一個(gè)數(shù)字的實(shí)例詳解的相關(guān)資料,這里提供實(shí)例代碼,注釋說(shuō)明清晰,需要的朋友可以參考下2017-07-07
mybatis中的擴(kuò)展實(shí)現(xiàn)源碼解析
這篇文章主要介給大家紹了關(guān)于mybatis中擴(kuò)展實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
Springboot中登錄后關(guān)于cookie和session攔截問(wèn)題的案例分析
這篇文章主要介紹了Springboot中登錄后關(guān)于cookie和session攔截案例,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08

