??? import java.io.FileNotFoundException; ??? import java.io.FileOutputStream; ??? import java.io.PrintStream; ??? ??? /**
??? * 为System.out.println()重定向输出 ??? * */
??? public class systemDemo{
??? public static void main(String[] args){ ??? // 此刻直接输出到屏幕
??? System.out.println(\
??? File file = new File(\??? try{
??? System.setOut(new PrintStream(new FileOutputStream(file))); ??? }catch(FileNotFoundException e){ ??? e.printStackTrace(); ??? }
??? System.out.println(\这些内容在文件中才能看到哦!\??? } ??? }
【运行结果】:
eclipse的控制台输出的是hello。然后当我们查看d盘下面的hello.txt文件的时候,会在里面看到:这些内容在文件中才能看到哦!
??? import java.io.File;
??? import java.io.FileNotFoundException; ??? import java.io.FileOutputStream; ??? import java.io.PrintStream; ??? ??? /**
??? * System.err重定向 这个例子也提示我们可以使用这种方法保存错误信息 ??? * */
??? public class systemErr{
??? public static void main(String[] args){
??? File file = new File(\??? System.err.println(\这些在控制台输出\ ??? try{
??? System.setErr(new PrintStream(new FileOutputStream(file))); ??? }catch(FileNotFoundException e){ ??? e.printStackTrace(); ??? }
??? System.err.println(\这些在文件中才能看到哦!\
??? } ??? }
【运行结果】:
你会在eclipse的控制台看到红色的输出:“这些在控制台输出”,然后在d盘下面的hello.txt中会看到:这些在文件中才能看到哦!
??? import java.io.File;
??? import java.io.FileInputStream; ??? import java.io.FileNotFoundException; ??? import java.io.IOException; ??? ??? /**
??? * System.in重定向 ??? * */
??? public class systemIn{
??? public static void main(String[] args){
??? File file = new File(\??? if(!file.exists()){ ??? return; ??? }else{ ??? try{
??? System.setIn(new FileInputStream(file)); ??? }catch(FileNotFoundException e){ ??? e.printStackTrace(); ??? }
??? byte[] bytes = new byte[1024]; ??? int len = 0; ??? try{
??? len = System.in.read(bytes); ??? }catch(IOException e){ ??? e.printStackTrace(); ??? }
??? System.out.println(\读入的内容为:\+ new String(bytes, 0, len)); ??? } ??? } ??? }
【运行结果】:
前提是我的d盘下面的hello.txt中的内容是:“这些文件中的内容哦!”,然后运行程序,输出的结果为:读入的内容为:这些文件中的内容哦!
BufferedReader的小例子
注意: BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用:
??? BufferedReader buf = new BufferedReader( ??? new InputStreamReader(System.in));
下面给一个实例:
??? import java.io.BufferedReader; ??? import java.io.IOException; ??? import java.io.InputStreamReader; ??? ??? /**
??? * 使用缓冲区从键盘上读入内容 ??? * */
??? public class BufferedReaderDemo{
??? public static void main(String[] args){ ??? BufferedReader buf = new BufferedReader( ??? new InputStreamReader(System.in)); ??? String str = null;
??? System.out.println(\请输入内容\??? try{
??? str = buf.readLine(); ??? }catch(IOException e){ ??? e.printStackTrace(); ??? }
??? System.out.println(\你输入的内容是:\??? } ??? }
运行结果: 请输入内容 dasdas
你输入的内容是:dasdas
五:
您所在的位置:开发 > Java > 热点推荐 > Java中的IO整理完整版(5)
Java中的IO整理完整版(5)
2011-09-19 16:17 Rollen Holt Rollen Holt 我要评论(0) 字号:T | T
本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面。照旧,文章依旧以例子为主,因为讲解内容的java书很多了,我觉的学以致用才是真。代码是写出来的,不是看出来的。
AD:
Scanner类
其实我们比较常用的是采用Scanner类来进行数据输入,下面来给一个Scanner的例子吧
? ? ? ? ? ? ? ? ?
import java.util.Scanner; /**
* Scanner的小例子,从键盘读数据 * */
public class ScannerDemo{
public static void main(String[] args){ Scanner sca = new Scanner(System.in); // 读一个整数
?? int temp = sca.nextInt(); ?? System.out.println(temp); ?? //读取浮点数
?? float flo=sca.nextFloat(); ?? System.out.println(flo); ?? //读取字符
?? //...等等的,都是一些太基础的,就不师范了。 ?? }
?? }
其实Scanner可以接受任何的输入流
下面给一个使用Scanner类从文件中读出内容
?? import java.io.File;
?? import java.io.FileNotFoundException; ?? import java.util.Scanner; ?? ?? /**
?? * Scanner的小例子,从文件中读内容 ?? * */
?? public class ScannerDemo{
?? public static void main(String[] args){ ??
?? File file = new File(\?? Scanner sca = null; ?? try{
?? sca = new Scanner(file); ?? }catch(FileNotFoundException e){ ?? e.printStackTrace(); ?? }
?? String str = sca.next();
?? System.out.println(\从文件中读取的内容是:\?? } ?? }
【运行结果】:
从文件中读取的内容是:这些文件中的内容哦! 数据操作流DataOutputStream、DataInputStream类
?? import java.io.DataOutputStream; ?? import java.io.File;
?? import java.io.FileOutputStream; ?? import java.io.IOException; ??
?? public class DataOutputStreamDemo{
?? public static void main(String[] args) throws IOException{ ?? File file = new File(\ ?? char[] ch = { 'A', 'B', 'C' }; ?? DataOutputStream out = null;