java中的IO详解(上)
来源:互联网 发布:linux socket 库 编辑:程序博客网 时间:2024/06/06 03:11
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详解(上)
- java中的IO详解(上)
- java IO流详解(上)
- java中的IO详解
- java中的IO详解
- Java中的IO流(上)
- java中的io系统详解(转)
- java中的IO详解(下)
- java中的IO详解(下)
- java中的io系统详解(值得一看)
- java中的io系统详解
- java中的io系统详解
- java中的io系统详解
- java中的io系统详解
- JAVA中的IO系统详解
- java中的io系统详解
- java中的io系统详解
- java中的io系统详解
- java中的io系统详解
- SVN客户端--TortoiseSVN使用说明
- UVa:10229 Modular Fibonacci
- UTF-8、BOM、<feff>的问题
- eclipse Android 工程在Libs导入第三方jar / Android工程作为library为其他工程提供引用
- java中的IO详解(上)
- tty open failed
- java中的IO详解(下)
- cocos2d-x ios 输入框打开时点击其他部分依然有效
- 虚拟机安装黑苹果 VM9.0+MacOS10.8.5+XCode5
- How to Validate Network and Name Resolution Setup for the Clusterware and RAC (Doc ID 1054902.1)
- @RequestMapping拦截多个请求
- Android Emulator Failed To Allocate memory 8
- VM安装CentOS6.5 minimal 过程(均来源于网络,按照自己的安装过程整理下,方便以后查找)