Java中的IO整理完整版(一)

【案例1】创建一个新文件

1 import java.io.*;

2 class hello{

3     public static void main(String[] args) {

4         File f=new File("D:\\hello.txt");

5         try{

6             f.createNewFile();

7         }catch (Exception e) {

8             e.printStackTrace();

9         }

10     }

11 }

【运行结果】:

程序运行之后,在d盘下会有一个名字为hello.txt的文件。

【案例2】File类的两个常量

12 import java.io.*;

13 class hello{

14     public static void main(String[] args) {

15         System.out.println(File.separator);

16         System.out.println(File.pathSeparator);

17     }

18 }

【运行结果】:
\
;
此处多说几句:有些同学可能认为,我直接在windows下使用\进行分割不行吗?当然是可以的。但是在linux下就不是\了。所以,要想使得我们的代码跨平台,更加健壮,所以,大家都采用这两个常量吧,其实也多写不了几行。呵呵、

现在我们使用File类中的常量改写上面的代码:

19 import java.io.*;

20 class hello{

21     public static void main(String[] args) {

22         String fileName="D:"+File.separator+"hello.txt";

23         File f=new File(fileName);

24         try{

25             f.createNewFile();

26         }catch (Exception e) {

27             e.printStackTrace();

28         }

29     }

30 }

你看,没有多写多少吧,呵呵。所以建议使用File类中的常量。

删除一个文件

31 /**

32  * 删除一个文件

33  * */

34 import java.io.*;

35 class hello{

36     public static void main(String[] args) {

37         String fileName="D:"+File.separator+"hello.txt";

38         File f=new File(fileName);

39         if(f.exists()){

40             f.delete();

41         }else{

42             System.out.println("文件不存在");

43         }

44

45     }

46 }

创建一个文件夹

47 /**

48  * 创建一个文件夹

49  * */

50 import java.io.*;

51 class hello{

52     public static void main(String[] args) {

53         String fileName="D:"+File.separator+"hello";

54         File f=new File(fileName);

55         f.mkdir();

56     }

57 }

【运行结果】:

D盘下多了一个hello文件夹

列出指定目录的全部文件(包括隐藏文件):

58 /**

59  * 使用list列出指定目录的全部文件

60  * */

61 import java.io.*;

62 class hello{

63     public static void main(String[] args) {

64         String fileName="D:"+File.separator;

65         File f=new File(fileName);

66         String[] str=f.list();

67         for (int i = 0; i < str.length; i++) {

68             System.out.println(str[i]);

69         }

70     }

71 }

【运行结果】:

$RECYCLE.BIN

360

360Downloads

360Rec

360SoftMove

Config.Msi

da

Downloads

DriversBackup

eclipse

java web整合开发和项目实战

Lenovo

MSOCache

Program

Program Files

python

RECYGLER.{8F92DA15-A229-A4D5-B5CE-5280C8B89C19}

System Volume Information

Tomcat6

var

vod_cache_data

新建文件夹

(你的运行结果应该和这个不一样的,呵呵)

但是使用list返回的是String数组,。而且列出的不是完整路径,如果想列出完整路径的话,需要使用listFiles.他返回的是File的数组

列出指定目录的全部文件(包括隐藏文件):

72 /**

73  * 使用listFiles列出指定目录的全部文件

74  * listFiles输出的是完整路径

75  * */

76 import java.io.*;

77 class hello{

78     public static void main(String[] args) {

79         String fileName="D:"+File.separator;

80         File f=new File(fileName);

81         File[] str=f.listFiles();

82         for (int i = 0; i < str.length; i++) {

83             System.out.println(str[i]);

84         }

85     }

86 }

【运行结果】:

D:\$RECYCLE.BIN

D:\360

D:\360Downloads

D:\360Rec

D:\360SoftMove

D:\Config.Msi

D:\da

D:\Downloads

D:\DriversBackup

D:\eclipse

D:\java web整合开发和项目实战

D:\Lenovo

D:\MSOCache

D:\Program

D:\Program Files

D:\python

D:\RECYGLER.{8F92DA15-A229-A4D5-B5CE-5280C8B89C19}

D:\System Volume Information

D:\Tomcat6

D:\var

D:\vod_cache_data

D:\新建文件夹

通过比较可以指定,使用listFiles更加方便、

判断一个指定的路径是否为目录

87 /**

88  * 使用isDirectory判断一个指定的路径是否为目录

89  * */

90 import java.io.*;

91 class hello{

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

93         String fileName="D:"+File.separator;

94         File f=new File(fileName);

95         if(f.isDirectory()){

96             System.out.println("YES");

97         }else{

98             System.out.println("NO");

99         }

100     }

101 }

【运行结果】:YES

搜索指定目录的全部内容

102 /**

103  * 列出指定目录的全部内容

104  * */

105 import java.io.*;

106 class hello{

107     public static void main(String[] args) {

108         String fileName="D:"+File.separator;

109         File f=new File(fileName);

110         print(f);

111     }

112     public static void print(File f){

113         if(f!=null){

114             if(f.isDirectory()){

115                 File[] fileArray=f.listFiles();

116                 if(fileArray!=null){

117                     for (int i = 0; i < fileArray.length; i++) {

118                         //递归调用

119                         print(fileArray[i]);

120                     }

121                 }

122             }

123             else{

124                 System.out.println(f);

125             }

126         }

127     }

128 }

【运行结果】:

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\framepages\web4welcome_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\help_005fhome_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\help_005fhome_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\home_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\home_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\index_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\index_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\login_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\login_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\modify_005fuser_005finfo_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\modify_005fuser_005finfo_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\register_005fnotify_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\register_005fnotify_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\sign_005fup_jsp.class

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\sign_005fup_jsp.java

D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\transit_jsp.class

……

【使用RandomAccessFile写入文件】

129 /**

130  * 使用RandomAccessFile写入文件

131  * */

132 import java.io.*;

133 class hello{

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

135         String fileName="D:"+File.separator+"hello.txt";

136         File f=new File(fileName);

137         RandomAccessFile demo=new RandomAccessFile(f,"rw");

138         demo.writeBytes("asdsad");

139         demo.writeInt(12);

140         demo.writeBoolean(true);

141         demo.writeChar('A');

142         demo.writeFloat(1.21f);

143         demo.writeDouble(12.123);

144         demo.close();

145     }

146 }

如果你此时打开hello。txt查看的话,会发现那是乱码。

字节流

【向文件中写入字符串】

147 /**

148  * 字节流

149  * 向文件中写入字符串

150  * */

151 import java.io.*;

152 class hello{

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

154         String fileName="D:"+File.separator+"hello.txt";

155         File f=new File(fileName);

156         OutputStream out =new FileOutputStream(f);

157         String str="你好";

158         byte[] b=str.getBytes();

159         out.write(b);

160         out.close();

161     }

162 }

查看hello.txt会看到“你好”

当然也可以一个字节一个字节的写。

163 /**

164  * 字节流

165  * 向文件中一个字节一个字节的写入字符串

166  * */

167 import java.io.*;

168 class hello{

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

170         String fileName="D:"+File.separator+"hello.txt";

171         File f=new File(fileName);

172         OutputStream out =new FileOutputStream(f);

173         String str="你好";

174         byte[] b=str.getBytes();

175         for (int i = 0; i < b.length; i++) {

176             out.write(b[i]);

177         }

178         out.close();

179     }

180 }

结果还是:“你好”

向文件中追加新内容:

181 /**

182  * 字节流

183  * 向文件中追加新内容:

184  * */

185 import java.io.*;

186 class hello{

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

188         String fileName="D:"+File.separator+"hello.txt";

189         File f=new File(fileName);

190         OutputStream out =new FileOutputStream(f,true);

191         String str="Rollen";

192         //String str="\r\nRollen";  可以换行

193         byte[] b=str.getBytes();

194         for (int i = 0; i < b.length; i++) {

195             out.write(b[i]);

196         }

197         out.close();

198     }

199 }

【运行结果】:

你好Rollen

【读取文件内容】

200 /**

201  * 字节流

202  * 读文件内容

203  * */

204 import java.io.*;

205 class hello{

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

207         String fileName="D:"+File.separator+"hello.txt";

208         File f=new File(fileName);

209         InputStream in=new FileInputStream(f);

210         byte[] b=new byte[1024];

211         in.read(b);

212         in.close();

213         System.out.println(new String(b));

214     }

215 }

【运行结果】
你好Rollen

Rollen_

但是这个例子读取出来会有大量的空格,我们可以利用in.read(b);的返回值来设计程序。如下:

216 /**

217  * 字节流

218  * 读文件内容

219  * */

220 import java.io.*;

221 class hello{

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

223         String fileName="D:"+File.separator+"hello.txt";

224         File f=new File(fileName);

225         InputStream in=new FileInputStream(f);

226         byte[] b=new byte[1024];

227         int len=in.read(b);

228         in.close();

229         System.out.println("读入长度为:"+len);

230         System.out.println(new String(b,0,len));

231     }

232 }

【运行结果】:

读入长度为:18

你好Rollen

Rollen

读者观察上面的例子可以看出,我们预先申请了一个指定大小的空间,但是有时候这个空间可能太小,有时候可能太大,我们需要准确的大小,这样节省空间,那么我们可以这样干:

233 /**

234  * 字节流

235  * 读文件内容,节省空间

236  * */

237 import java.io.*;

238 class hello{

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

240         String fileName="D:"+File.separator+"hello.txt";

241         File f=new File(fileName);

242         InputStream in=new FileInputStream(f);

243         byte[] b=new byte[(int)f.length()];

244         in.read(b);

245         System.out.println("文件长度为:"+f.length());

246         in.close();

247         System.out.println(new String(b));

248     }

249 }

文件长度为:18

你好Rollen

Rollen

将上面的例子改为一个一个读:

250 /**

251  * 字节流

252  * 读文件内容,节省空间

253  * */

254 import java.io.*;

255 class hello{

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

257         String fileName="D:"+File.separator+"hello.txt";

258         File f=new File(fileName);

259         InputStream in=new FileInputStream(f);

260         byte[] b=new byte[(int)f.length()];

261         for (int i = 0; i < b.length; i++) {

262             b[i]=(byte)in.read();

263         }

264         in.close();

265         System.out.println(new String(b));

266     }

267 }

输出的结果和上面的一样。

细心的读者可能会发现,上面的几个例子都是在知道文件的内容多大,然后才展开的,有时候我们不知道文件有多大,这种情况下,我们需要判断是否独到文件的末尾。

268 /**

269  * 字节流

270  *读文件

271  * */

272 import java.io.*;

273 class hello{

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

275         String fileName="D:"+File.separator+"hello.txt";

276         File f=new File(fileName);

277         InputStream in=new FileInputStream(f);

278         byte[] b=new byte[1024];

279         int count =0;

280         int temp=0;

281         while((temp=in.read())!=(-1)){

282             b[count++]=(byte)temp;

283         }

284         in.close();

285         System.out.println(new String(b));

286     }

287 }

【运行结果】

你好Rollen

Rollen_

提醒一下,当独到文件末尾的时候会返回-1.正常情况下是不会返回-1的

现在我们使用字符流

288 /**

289  * 字符流

290  * 写入数据

291  * */

292 import java.io.*;

293 class hello{

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

295         String fileName="D:"+File.separator+"hello.txt";

296         File f=new File(fileName);

297         Writer out =new FileWriter(f);

298         String str="hello";

299         out.write(str);

300         out.close();

301     }

302 }

当你打开hello。txt的时候,会看到hello

其实这个例子上之前的例子没什么区别,只是你可以直接输入字符串,而不需要你将字符串转化为字节数组。

当你如果想问文件中追加内容的时候,可以使用将上面的声明out的哪一行换为:

Writer out =new FileWriter(f,true);

这样,当你运行程序的时候,会发现文件内容变为:

hellohello如果想在文件中换行的话,需要使用“\r\n”

比如将str变为String str="\r\nhello";

这样文件追加的str的内容就会换行了。

从文件中读内容:

303 /**

304  * 字符流

305  * 从文件中读出内容

306  * */

307 import java.io.*;

308 class hello{

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

310         String fileName="D:"+File.separator+"hello.txt";

311         File f=new File(fileName);

312         char[] ch=new char[100];

313         Reader read=new FileReader(f);

314         int count=read.read(ch);

315         read.close();

316         System.out.println("读入的长度为:"+count);

317         System.out.println("内容为"+new String(ch,0,count));

318     }

319 }

【运行结果】:

读入的长度为:17

内容为hellohello

hello

当然最好采用循环读取的方式,因为我们有时候不知道文件到底有多大。

320 /**

321  * 字符流

322  * 从文件中读出内容

323  * */

324 import java.io.*;

325 class hello{

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

327         String fileName="D:"+File.separator+"hello.txt";

328         File f=new File(fileName);

329         char[] ch=new char[100];

330         Reader read=new FileReader(f);

331         int temp=0;

332         int count=0;

333         while((temp=read.read())!=(-1)){

334             ch[count++]=(char)temp;

335         }

336         read.close();

337         System.out.println("内容为"+new String(ch,0,count));

338     }

339 }

运行结果:

内容为hellohello

hello

关于字节流和字符流的区别

实际上字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的 时候下后是会用到缓冲区的,是通过缓冲区来操作文件的。

读者可以试着将上面的字节流和字符流的程序的最后一行关闭文件的代码注释掉,然后运行程序看看。你就会发现使用字节流的话,文件中已经存在内容,但是使用字符流的时候,文件中还是没有内容的,这个时候就要刷新缓冲区。

使用字节流好还是字符流好呢?

答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

文件的复制

其实DOS下就有一个文件复制功能,比如我们想把d盘下面的hello.txt文件复制到d盘下面的rollen.txt文件中,那么我们就可以使用下面的命令:

copy d:\hello.txt d:\rollen.txt

运行之后你会在d盘中看见hello.txt.,并且两个文件的内容是一样的,(这是屁话)

下面我们使用程序来复制文件吧。

基本思路还是从一个文件中读入内容,边读边写入另一个文件,就是这么简单。、

首先编写下面的代码:

340 /**

341  * 文件的复制

342  * */

343 import java.io.*;

344 class hello{

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

346         if(args.length!=2){

347             System.out.println("命令行参数输入有误,请检查");

348             System.exit(1);

349         }

350         File file1=new File(args[0]);

351         File file2=new File(args[1]);

352

353         if(!file1.exists()){

354             System.out.println("被复制的文件不存在");

355             System.exit(1);

356         }

357         InputStream input=new FileInputStream(file1);

358         OutputStream output=new FileOutputStream(file2);

359         if((input!=null)&&(output!=null)){

360             int temp=0;

361             while((temp=input.read())!=(-1)){

362                 output.write(temp);

363             }

364         }

365         input.close();

366         output.close();

367     }

368 }

然后在命令行下面

javac hello.java

java hello d:\hello.txt d:\rollen.txt

现在你就会在d盘看到rollen。txt了,

OutputStreramWriter 和InputStreamReader类

整个IO类中除了字节流和字符流还包括字节和字符转换流。

OutputStreramWriter将输出的字符流转化为字节流

InputStreamReader将输入的字节流转换为字符流

但是不管如何操作,最后都是以字节的形式保存在文件中的。

将字节输出流转化为字符输出流

369 /**

370  * 将字节输出流转化为字符输出流

371  * */

372 import java.io.*;

373 class hello{

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

375         String fileName= "d:"+File.separator+"hello.txt";

File file=new File(file

376 Writer out=new OutputStreamWriter(new FileOutputStream(file));

377         out.write("hello");

378         out.close();

379     }

380 }

运行结果:文件中内容为:hello

将字节输入流变为字符输入流

381 /**

382  * 将字节输入流变为字符输入流

383  * */

384 import java.io.*;

385 class hello{

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

387         String fileName= "d:"+File.separator+"hello.txt";

388         File file=new File(fileName);

389         Reader read=new InputStreamReader(new FileInputStream(file));

390         char[] b=new char[100];

391         int len=read.read(b);

392         System.out.println(new String(b,0,len));

393         read.close();

394     }

395 }

【运行结果】:hello

前面列举的输出输入都是以文件进行的,现在我们以内容为输出输入目的地,使用内存操作流

ByteArrayInputStream 主要将内容写入内容

ByteArrayOutputStream 主要将内容从内存输出

使用内存操作流将一个大写字母转化为小写字母

396 /**

397  * 使用内存操作流将一个大写字母转化为小写字母

398  * */

399 import java.io.*;

400 class hello{

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

402         String str="ROLLENHOLT";

403         ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes());

404         ByteArrayOutputStream output=new ByteArrayOutputStream();

405         int temp=0;

406         while((temp=input.read())!=-1){

407             char ch=(char)temp;

408             output.write(Character.toLowerCase(ch));

409         }

410         String outStr=output.toString();

411         input.close();

412         output.close();

413         System.out.println(outStr);

414     }

415 }

【运行结果】:

rollenholt

内容操作流一般使用来生成一些临时信息采用的,这样可以避免删除的麻烦。

管道流

管道流主要可以进行两个线程之间的通信。

PipedOutputStream 管道输出流

PipedInputStream 管道输入流

验证管道流

416 /**

417  * 验证管道流

418  * */

419 import java.io.*;

420

421 /**

422  * 消息发送类

423  * */

424 class Send implements Runnable{

425     private PipedOutputStream out=null;

426     public Send() {

427         out=new PipedOutputStream();

428     }

429     public PipedOutputStream getOut(){

430         return this.out;

431     }

432     public void run(){

433         String message="hello , Rollen";

434         try{

435             out.write(message.getBytes());

436         }catch (Exception e) {

437             e.printStackTrace();

438         }try{

439             out.close();

440         }catch (Exception e) {

441             e.printStackTrace();

442         }

443     }

444 }

445

446 /**

447  * 接受消息类

448  * */

449 class Recive implements Runnable{

450     private PipedInputStream input=null;

451     public Recive(){

452         this.input=new PipedInputStream();

453     }

454     public PipedInputStream getInput(){

455         return this.input;

456     }

457     public void run(){

458         byte[] b=new byte[1000];

459         int len=0;

460         try{

461             len=this.input.read(b);

462         }catch (Exception e) {

463             e.printStackTrace();

464         }try{

465             input.close();

466         }catch (Exception e) {

467             e.printStackTrace();

468         }

469         System.out.println("接受的内容为 "+(new String(b,0,len)));

470     }

471 }

472 /**

473  * 测试类

474  * */

475 class hello{

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

477         Send send=new Send();

478         Recive recive=new Recive();

479         try{

480 //管道连接

481             send.getOut().connect(recive.getInput());

482         }catch (Exception e) {

483             e.printStackTrace();

484         }

485         new Thread(send).start();

486         new Thread(recive).start();

487     }

488 }

【运行结果】:

接受的内容为 hello , Rollen

打印流

489 /**

490  * 使用PrintStream进行输出

491  * */

492 import java.io.*;

493

494 class hello {

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

496         PrintStream print = new PrintStream(new FileOutputStream(new File("d:"

497                 + File.separator + "hello.txt")));

498         print.println(true);

499         print.println("Rollen");

500         print.close();

501     }

502 }

【运行结果】:

true

Rollen

当然也可以格式化输出

503 /**

504  * 使用PrintStream进行输出

505  * 并进行格式化

506  * */

507 import java.io.*;

508 class hello {

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

510         PrintStream print = new PrintStream(new FileOutputStream(new File("d:"

511                 + File.separator + "hello.txt")));

512         String name="Rollen";

513         int age=20;

514         print.printf("姓名:%s. 年龄:%d.",name,age);

515         print.close();

516     }

517 }

【运行结果】:

姓名:Rollen. 年龄:20.

使用OutputStream向屏幕上输出内容

518 /**

519  * 使用OutputStream向屏幕上输出内容

520  * */

521 import java.io.*;

522 class hello {

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

524         OutputStream out=System.out;

525         try{

526             out.write("hello".getBytes());

527         }catch (Exception e) {

528             e.printStackTrace();

529         }

530         try{

531             out.close();

532         }catch (Exception e) {

533             e.printStackTrace();

534         }

535     }

536 }

【运行结果】:

hello

输入输出重定向

537 import java.io.File;

538 import java.io.FileNotFoundException;

539 import java.io.FileOutputStream;

540 import java.io.PrintStream;

541

542 /**

543  * 为System.out.println()重定向输出

544  * */

545 public class systemDemo{

546     public static void main(String[] args){

547         // 此刻直接输出到屏幕

548         System.out.println("hello");

549         File file = new File("d:" + File.separator + "hello.txt");

550         try{

551             System.setOut(new PrintStream(new FileOutputStream(file)));

552         }catch(FileNotFoundException e){

553             e.printStackTrace();

554         }

555         System.out.println("这些内容在文件中才能看到哦!");

556     }

557 }

【运行结果】:

eclipse的控制台输出的是hello。然后当我们查看d盘下面的hello.txt文件的时候,会在里面看到:这些内容在文件中才能看到哦!

558 import java.io.File;

559 import java.io.FileNotFoundException;

560 import java.io.FileOutputStream;

561 import java.io.PrintStream;

562

563 /**

564  * System.err重定向 这个例子也提示我们可以使用这种方法保存错误信息

565  * */

566 public class systemErr{

567     public static void main(String[] args){

568         File file = new File("d:" + File.separator + "hello.txt");

569         System.err.println("这些在控制台输出");

570         try{

571             System.setErr(new PrintStream(new FileOutputStream(file)));

572         }catch(FileNotFoundException e){

573             e.printStackTrace();

574         }

575         System.err.println("这些在文件中才能看到哦!");

576     }

577 }

【运行结果】:

你会在eclipse的控制台看到红色的输出:“这些在控制台输出”,然后在d盘下面的hello.txt中会看到:这些在文件中才能看到哦!

578 import java.io.File;

579 import java.io.FileInputStream;

580 import java.io.FileNotFoundException;

581 import java.io.IOException;

582

583 /**

584  * System.in重定向

585  * */

586 public class systemIn{

587     public static void main(String[] args){

588         File file = new File("d:" + File.separator + "hello.txt");

589         if(!file.exists()){

590             return;

591         }else{

592             try{

593                 System.setIn(new FileInputStream(file));

594             }catch(FileNotFoundException e){

595                 e.printStackTrace();

596             }

597             byte[] bytes = new byte[1024];

598             int len = 0;

599             try{

600                 len = System.in.read(bytes);

601             }catch(IOException e){

602                 e.printStackTrace();

603             }

604             System.out.println("读入的内容为:" + new String(bytes, 0, len));

605         }

606     }

607 }

【运行结果】:

前提是我的d盘下面的hello.txt中的内容是:“这些文件中的内容哦!”,然后运行程序,输出的结果为:读入的内容为:这些文件中的内容哦

java中的IO详解(上)相关推荐

  1. java中的IO详解(下)

    BufferedReader的小例子 注意: BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用: 1  ...

  2. Java中JDBC连接数据库详解

    今天动力节点java学院小编分享的是JDBC连接数据库的相关知识,希望通过看过此文,各位小伙伴对DBC连接数据库有所了解,下面就跟随小编一起来看看JDBC连接数据库的知识吧. 一.JDBC连接数据库概 ...

  3. JAVA中的Random详解

    JAVA中的Random详解 首先,在JDK自带的常用的random中有两个,这俩都是产生随机数的,不过一个是util下的random,另外一个是Math下的.我们分别介绍一下 util中的rando ...

  4. Java中的byte详解

    Java中的byte详解 介绍 byte,即字节,由8位的二进制组成.在Java中,byte类型的数据是8位带符号的二进制数. 在计算机中,8位带符号二进制数的取值范围是[-128, 127],所以在 ...

  5. java中new关键字详解

    java中new关键字详解 在java中我们可以经常使用new来创建一个对象,但是这对于初学者来说可能只会使用却不能理解new关键字和它的语法 new关键字的语法 注意使用前先导包,一般我们使用ide ...

  6. Java中super关键字详解

    Java中super关键字详解 super有什么用? super什么时候不可以省略呢? super在内存图中是如何存在的呢? super使用时的注意事项 super有什么用? (1)当子类中构造方法第 ...

  7. Java中Iterator迭代器详解

    目录 一.Java中Iterator迭代器详解 1.为什么需要迭代器 2.迭代器长什么样子 3.如何使用迭代器 使用步骤: 代码演示: 迭代器可以简化为增强型for循环: 4.Iterator与Lis ...

  8. 【转载】java中泛型使用详解

    引入 Type接口 Class类 Method类 Field类 ParameterizedType接口 TypeVariable接口 类中定义泛型变量 方法中定义泛型变量 方法中泛型参数和泛型返回值 ...

  9. Java中的Base64详解

    详解Java中的Base64原理跟用法 简介 ​ Base64编码,是我们程序开发中经常使用到的编码方法.它是一种基于用64个可打印字符来表示二进制数据的表示方法.它通常用作存储.传输一些二进制数据编 ...

最新文章

  1. 2021年大数据Hadoop(二十五):YARN通俗介绍和基本架构
  2. Docker安装Tomcat、MySQL和Redis
  3. What Is Text Mining?
  4. java多维数组的指针_Java基础之二维数组
  5. 为进大厂刷爆算法题,最后却倒在了基础题上?太苦了!
  6. Android 判断应用 第一次启动
  7. SAP Commerce Cloud Product Action 导出的层级结构设计
  8. 第一台定制商用NAS存储服务器
  9. 仓库每天的账怎样做_新年第一站,济南:仓储匠人仓库问题解决与实战力培训...
  10. websocket传输数据大小限制_websocket 发送字符串数据上限是多少
  11. 计算机病毒正确探讨目录,毕业论文-计算机病毒的正确防御探讨.doc
  12. tensorflow出现问题Passing (type, 1) or 1type as a synonym of type is deprecated
  13. 企业架构-数据服务总线思路
  14. HDU 5143 NPY and arithmetic progression(思维)
  15. MyBatis框架学习笔记01:初入MyBatis(一)
  16. 在本地存储中存入和取出对象
  17. 【Practical】积分第一中值定理
  18. 人生的换档时刻?你是怎么度过的
  19. 游戏开发之Unity学习(五)——鼠标打飞碟(Hit UFO)
  20. MicroNet实战:使用MicroNet实现图像分类(二)

热门文章

  1. Hibernate 实体映射类的状态值自动转换
  2. 《memory leak: stackwalk》
  3. 心路历程(一)-自学java两个月心得
  4. ARP欺骗 ---网络执法官(转载)
  5. 【原】移动web动画设计的一点心得——css3实现跑步
  6. IDEA 部署项目的时候出错:Jar not loaded错误
  7. OpenLdap 相关命令
  8. Response 输出文件流过程中的等待效果
  9. Linux服务器程序编程的几个坎
  10. 这3句屁话,关键时刻能救命