文件的复制
其实DOS下就有一个文件复制功能,比如我们想把d盘下面的hello.txt文件复制到d盘下面的rollen.txt文件中,那么我们就可以使用下面的命令:
copy d:\hello.txt d:\rollen.txt
运行之后你会在d盘中看见hello.txt.,并且两个文件的内容是一样的,(这是屁话)
下面我们使用程序来复制文件吧。
基本思路还是从一个文件中读入内容,边读边写入另一个文件,就是这么简单。、
首先编写下面的代码:
/** * 文件的复制 * */ importjava.io.*; classhello{ publicstaticvoidmain(String[] args) throwsIOException { if(args.length!=2){ System.out.println("命令行参数输入有误,请检查"); System.exit(1); } File file1=newFile(args[0]); File file2=newFile(args[1]); if(!file1.exists()){ System.out.println("被复制的文件不存在"); System.exit(1); } InputStream input=newFileInputStream(file1); OutputStream output=newFileOutputStream(file2); if((input!=null)&&(output!=null)){ inttemp=0; while((temp=input.read())!=(-1)){ output.write(temp); } } input.close(); output.close(); } }
然后在命令行下面
javac hello.java
java hello d:\hello.txt d:\rollen.txt
现在你就会在d盘看到rollen。txt了,
整个IO类中除了字节流和字符流还包括字节和字符转换流。
OutputStreramWriter将输出的字符流转化为字节流
InputStreamReader将输入的字节流转换为字符流
但是不管如何操作,最后都是以字节的形式保存在文件中的。
/** * 将字节输出流转化为字符输出流 * */ importjava.io.*; classhello{ publicstaticvoidmain(String[] args) throwsIOException { String fileName= "d:"+File.separator+"hello.txt"; File file=newFile(fileName); Writer out=newOutputStreamWriter(newFileOutputStream(file)); out.write("hello"); out.close(); } }
运行结果:文件中内容为:hello
/** * 将字节输入流变为字符输入流 * */ importjava.io.*; classhello{ publicstaticvoidmain(String[] args) throwsIOException { String fileName= "d:"+File.separator+"hello.txt"; File file=newFile(fileName); Reader read=newInputStreamReader(newFileInputStream(file)); char[] b=newchar[100]; intlen=read.read(b); System.out.println(newString(b,0,len)); read.close(); } }
【运行结果】:hello
前面列举的输出输入都是以文件进行的,现在我们以内容为输出输入目的地,使用内存操作流
ByteArrayInputStream 主要将内容写入内容
ByteArrayOutputStream 主要将内容从内存输出
/** * 使用内存操作流将一个大写字母转化为小写字母 * */ importjava.io.*; classhello{ publicstaticvoidmain(String[] args) throwsIOException { String str="ROLLENHOLT"; ByteArrayInputStream input=newByteArrayInputStream(str.getBytes()); ByteArrayOutputStream output=newByteArrayOutputStream(); inttemp=0; while((temp=input.read())!=-1){ charch=(char)temp; output.write(Character.toLowerCase(ch)); } String outStr=output.toString(); input.close(); output.close(); System.out.println(outStr); } }
【运行结果】:
rollenholt
内容操作流一般使用来生成一些临时信息采用的,这样可以避免删除的麻烦。
管道流主要可以进行两个线程之间的通信。
PipedOutputStream 管道输出流
PipedInputStream 管道输入流
/** * 验证管道流 * */ importjava.io.*; /** * 消息发送类 * */ classSend implementsRunnable{ privatePipedOutputStream out=null; publicSend() { out=newPipedOutputStream(); } publicPipedOutputStream getOut(){ returnthis.out; } publicvoidrun(){ String message="hello , Rollen"; try{ out.write(message.getBytes()); }catch(Exception e) { e.printStackTrace(); }try{ out.close(); }catch(Exception e) { e.printStackTrace(); } } } /** * 接受消息类 * */ classRecive implementsRunnable{ privatePipedInputStream input=null; publicRecive(){ this.input=newPipedInputStream(); } publicPipedInputStream getInput(){ returnthis.input; } publicvoidrun(){ byte[] b=newbyte[1000]; intlen=0; try{ len=this.input.read(b); }catch(Exception e) { e.printStackTrace(); }try{ input.close(); }catch(Exception e) { e.printStackTrace(); } System.out.println("接受的内容为 "+(newString(b,0,len))); } } /** * 测试类 * */ classhello{ publicstaticvoidmain(String[] args) throwsIOException { Send send=newSend(); Recive recive=newRecive(); try{ //管道连接 send.getOut().connect(recive.getInput()); }catch(Exception e) { e.printStackTrace(); } newThread(send).start(); newThread(recive).start(); } }
【运行结果】:
接受的内容为 hello , Rollen
/** * 使用PrintStream进行输出 * */ importjava.io.*; classhello { publicstaticvoidmain(String[] args) throwsIOException { PrintStream print = newPrintStream(newFileOutputStream(newFile("d:" + File.separator + "hello.txt"))); print.println(true); print.println("Rollen"); print.close(); } }
【运行结果】:
true
Rollen
当然也可以格式化输出
/** * 使用PrintStream进行输出 * 并进行格式化 * */ importjava.io.*; classhello { publicstaticvoidmain(String[] args) throwsIOException { PrintStream print = newPrintStream(newFileOutputStream(newFile("d:" + File.separator + "hello.txt"))); String name="Rollen"; intage=20; print.printf("姓名:%s. 年龄:%d.",name,age); print.close(); } }
【运行结果】:
姓名:Rollen. 年龄:20.
使用OutputStream向屏幕上输出内容
/** * 使用OutputStream向屏幕上输出内容 * */ importjava.io.*; classhello { publicstaticvoidmain(String[] args) throwsIOException { OutputStream out=System.out; try{ out.write("hello".getBytes()); }catch(Exception e) { e.printStackTrace(); } try{ out.close(); }catch(Exception e) { e.printStackTrace(); } } }
【运行结果】:
hello
importjava.io.File; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.PrintStream; /** * 为System.out.println()重定向输出 * */ publicclasssystemDemo{ publicstaticvoidmain(String[] args){ // 此刻直接输出到屏幕 System.out.println("hello"); File file = newFile("d:"+ File.separator + "hello.txt"); try{ System.setOut(newPrintStream(newFileOutputStream(file))); }catch(FileNotFoundException e){ e.printStackTrace(); } System.out.println("这些内容在文件中才能看到哦!"); } }
【运行结果】:
eclipse的控制台输出的是hello。然后当我们查看d盘下面的hello.txt文件的时候,会在里面看到:这些内容在文件中才能看到哦!
importjava.io.File; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.PrintStream; /** * System.err重定向 这个例子也提示我们可以使用这种方法保存错误信息 * */ publicclasssystemErr{ publicstaticvoidmain(String[] args){ File file = newFile("d:"+ File.separator + "hello.txt"); System.err.println("这些在控制台输出"); try{ System.setErr(newPrintStream(newFileOutputStream(file))); }catch(FileNotFoundException e){ e.printStackTrace(); } System.err.println("这些在文件中才能看到哦!"); } }
【运行结果】:
你会在eclipse的控制台看到红色的输出:“这些在控制台输出”,然后在d盘下面的hello.txt中会看到:这些在文件中才能看到哦!
importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.IOException; /** * System.in重定向 * */ publicclasssystemIn{ publicstaticvoidmain(String[] args){ File file = newFile("d:"+ File.separator + "hello.txt"); if(!file.exists()){ return; }else{ try{ System.setIn(newFileInputStream(file)); }catch(FileNotFoundException e){ e.printStackTrace(); } byte[] bytes = newbyte[1024]; intlen = 0; try{ len = System.in.read(bytes); }catch(IOException e){ e.printStackTrace(); } System.out.println("读入的内容为:"+ newString(bytes, 0, len)); } } }
【运行结果】:
前提是我的d盘下面的hello.txt中的内容是:“这些文件中的内容哦!”,然后运行程序,输出的结果为:读入的内容为:这些文件中的内容哦!
注意: BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用:
BufferedReader buf =
new
BufferedReader(
new
InputStreamReader(System.in));
importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStreamReader; /** * 使用缓冲区从键盘上读入内容 * */ publicclassBufferedReaderDemo{ publicstaticvoidmain(String[] args){ BufferedReader buf = newBufferedReader( newInputStreamReader(System.in)); String str = null; System.out.println("请输入内容"); try{ str = buf.readLine(); }catch(IOException e){ e.printStackTrace(); } System.out.println("你输入的内容是:"+ str); } }
运行结果:
请输入内容
dasdas
你输入的内容是:dasdas
其实我们比较常用的是采用Scanner类来进行数据输入,下面来给一个Scanner的例子吧
importjava.util.Scanner; /** * Scanner的小例子,从键盘读数据 * */ publicclassScannerDemo{ publicstaticvoidmain(String[] args){ Scanner sca = newScanner(System.in); // 读一个整数 inttemp = sca.nextInt(); System.out.println(temp); //读取浮点数 floatflo=sca.nextFloat(); System.out.println(flo); //读取字符 //...等等的,都是一些太基础的,就不师范了。 } }
其实Scanner可以接受任何的输入流
下面给一个使用Scanner类从文件中读出内容
importjava.io.File; importjava.io.FileNotFoundException; importjava.util.Scanner; /** * Scanner的小例子,从文件中读内容 * */ publicclassScannerDemo{ publicstaticvoidmain(String[] args){ File file = newFile("d:"+ File.separator + "hello.txt"); Scanner sca = null; try{ sca = newScanner(file); }catch(FileNotFoundException e){ e.printStackTrace(); } String str = sca.next(); System.out.println("从文件中读取的内容是:"+ str); } }
【运行结果】:
从文件中读取的内容是:这些文件中的内容哦!
importjava.io.DataOutputStream; importjava.io.File; importjava.io.FileOutputStream; importjava.io.IOException; publicclassDataOutputStreamDemo{ publicstaticvoidmain(String[] args) throwsIOException{ File file = newFile("d:"+ File.separator + "hello.txt"); char[] ch = { 'A', 'B', 'C'}; DataOutputStream out = null; out = newDataOutputStream(newFileOutputStream(file)); for(chartemp : ch){ out.writeChar(temp); } out.close(); } }
A B C
现在我们在上面例子的基础上,使用DataInputStream读出内容
importjava.io.DataInputStream; importjava.io.File; importjava.io.FileInputStream; importjava.io.IOException; publicclassDataOutputStreamDemo{ publicstaticvoidmain(String[] args) throwsIOException{ File file = newFile("d:"+ File.separator + "hello.txt"); DataInputStream input = newDataInputStream(newFileInputStream(file)); char[] ch = newchar[10]; intcount = 0; chartemp; while((temp = input.readChar()) != 'C'){ ch[count++] = temp; } System.out.println(ch); } }
【运行结果】:
AB