java io流转bate_Java学习 之 IO流(转换流、缓冲流)

1 /*

2

3 OutputStreamWriter:将字符串按照指定的编码表转换成字节,在使用字节流将字节写出去4

5 */

6

7 public classOutputStreamWriterDemo{8

9 public static void main(String[] args) throwsIOException{10

11 //字符转字节的转换流

12

13 writeCN();14

15 }16

17 public static void writeCN() throwsIOException{18

19 //创建与文件关联的字节输出流对象

20

21 FileOutputStream fos = new FileOutputStream("c:\\a.txt");22

23 //创建可以把字符转换成字节的转换流对象,并指定编码

24

25 OutputStreamWriter osw = new OutputStreamWriter(fos , "utf-8");26

27 //调用转换流,把文字写出去,其实是写到转换流的缓冲区

28

29 osw.write("你好");//写入缓冲区

30

31 osw.close();32

33 }34

35 }36

37 /*

38

39 InputStreamReader:是字节流通向字符流的桥梁,使用指定的字符编码表读取字节并将其解码为字符40

41 */

42

43 public classInputStreamReaderDemo{44

45 public static void main(String[] args) throwsIOException{46

47 //字节转换字符流的转换流

48

49 readCN();50

51 }52

53 public static void readCN() throwsIOException{54

55 //创建读取文件的字节流对象

56

57 FileInputStream fis = new FileInputStream("c:\\a.txt");58

59 //创建转换流对象

60

61 InputStreamReader isr = new InputStreamReader(fis , "utf-8");62

63 //使用转换流去读字节流中的字节

64

65 int ch = 0;66

67 while((ch = isr.read()) != -1){68

69 System.out.println((char)ch);70

71 }72

73 isr.close();74

75 }76

77 }78

79 /*

80

81 缓冲流:82

83 字节缓冲输出流:BufferedOutputStream84

85 构造方法:public BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流,以将数据写入指定的底层输出流86

87 */

88

89 public classBufferedOutputStreamDemo{90

91 public static void main(String[] args) throwsIOException{92

93 //写数据到文件的方法

94

95 write();96

97 }98

99 /*

100

101 写数据到文件的方法102

103 1、创建流104

105 2、写数据106

107 3、关闭流108

109 */

110

111 public static void write() throwsIOException{112

113 //创建基本的字节输出流

114

115 FileOutputStream fos = new FileOutputStream("a.txt");116

117 //使用高效的流,把基本的流进行封装,实现速度的提升

118 BufferedOutputStream bos = newBufferedOutputStream(fos);119

120 //写数据

121

122 out.write("你好".getBytes());123

124 //关闭流

125

126 out.close();127

128 }129

130 }131

132 /*

133

134 字节缓冲输入流:BufferedInputStream135

136 构造方法:public BufferedInputStream(InputStream in)137

138 */

139

140 public classBufferedInputStream{141

142 public static void main(String[] args) throwsIOException{143

144 read();145

146 }147

148 /*

149

150 从文件中读取数据151

152 1、创建缓冲流对象153

154 2、读数据,打印155

156 3、关闭157

158 */

159

160 public static void read() throwsIOException{161

162 //创建缓冲流对象

163

164 FileInputStream fis = new FileInputStream("a.txt");165

166 //把基本的流包装成高效的流

167

168 BufferedInputStream bis = newBufferedInputStream(fis);169

170 //读数据

171

172 int ch = -1;173

174 while((ch = bis.read()) != -1){175

176 System.out.println((char)ch);177

178 }179

180 bis.close();181

182 }183

184 }185

186 /*

187

188 字符缓冲输入流:BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取189

190 public String readLine()读取一个文本行,包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回true191

192 字符缓冲输出流:BufferedWriter将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入193

194 void newLine()根据当前的系统,写入一个换行符195

196 */

197

198 public classBufferedWriterDemo{199

200 public static void main(String[] args) throwsIOException{201

202 //创建流203

204 //基本字符输出流

205

206 FileWriter fw = new FileWriter("file.txt");207

208 //把基本的流进行包装

209

210 BufferedWriter bw = newBufferedWriter(fw);211

212 //写数据

213

214 for(int i = 0 ; i < 5 ; i++){215

216 bw.write("hello");217

218 bw.newLine();219

220 }221

222 //关闭流

223

224 bw.close();225

226 }227

228 }229

230 public classBufferedWriterDemo{231

232 public static void main(String[] args) throwsException{233

234 //创建流

235

236 BufferedReader br = new BufferedReader("file.txt");237

238 //读数据239

240 //一次一个字符241

242 //一次一个字符数组243

244 //一次读取文本中一行的字符串内容

245

246 String line = null;247

248 while((line = br.readLine()) != -1){249

250 System.out.println(line);251

252 }253

254 //关闭流

255

256 line.close();257

258 }259

260 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值