本章目标
掌握System对IO的三种支持:
——System.out
——System.err
——System.in
掌握System.out及System.err的区别
掌握输入、输出重定向
System类的常量
System表示系统类,此类在之前讲解JAVA常用类库的时候就已经为读者介绍过了,实际上在java中System类也对IO给予了一定的支持。
System.out
System.out是PrintStream的对象,在PrintStream中定义了一系列的print()和println()方法,所以之前使用的“System.out.print()”或“System.out.println()”语句调用的实际上就是PrintStream类的方法。
使用OutputStream向屏幕上输出
import java.io.IOException; import java.io.OutputStream; public class SystemDemo01{ public static void main(String[] args) { OutputStream out = System.out;//此时的输出流是向屏幕上输出 try{ out.write("hello world!!!".getBytes());//向屏幕上输出 }catch(IOException e){ e.printStackTrace(); } try{ out.close();//关闭输出流 }catch(IOException e){ e.printStackTrace(); } } /* 结果: * hello world!!! * */ }
System.err
System.err表示的是错误信息输出,如果程序出现错误,则可以直接使用System.err进行打印。
打印错误信息
public class SystemDemo02{ public static void main(String[] args) { String str = "hello";//声明一个非数字的字符串 try{ System.out.println(Integer.parseInt(str)); }catch(Exception e){ System.err.println(e); } } /* 结果: * java.lang.NumberFormatException: For input string: "hello" * */ }
System.out和System.err的区别
System.out和System.err都是PrintStream的实例化对象,而且通过实例代码可以发现,两者都可以输出错误信息,但是一般来讲System.out是将信息显示给用户看,是正常的信息显示,而System.err的信息正好相反是不希望用户看到的,会直接在后台打印,是专门显示错误的
一般来讲,如果要想输出错误信息的时候最好不要使用System.out而是直接使用System.err,这一点只能从其概念上划分。
System.in
System.in实际上是一个键盘的输入流,其本身是InputStream类型的对象。那么,此时就可以利用此方式完成从键盘读取数据的功能。
从键盘上读取数据
import java.io.InputStream; public class SystemDemo04{ public static void main(String[] args) throws Exception {//所有异常抛出 InputStream input = System.in;//从键盘接收数据 byte b[] = new byte[1024];//开辟空间,接收数据 System.out.print("请输入内容:");//信息提示 int len = input.read(b);//接收数据 System.out.print("输入的内容为:"+new String(b,0,len)); input.close();//关闭输入流 } /* 结果: * 请输入内容:chaoyi * 输入的内容为:chaoyi * */ }
输入的问题
那么,如果此时不指定byte数组长度呢?是否可以完成输入的要求呢?
不指定大小
import java.io.InputStream; public class SystemDemo05{ public static void main(String[] args) throws Exception {//所有异常抛出 InputStream input = System.in;//从键盘接收数据 StringBuffer buf = new StringBuffer();//声明 StringBuffer 用于接收数据 System.out.print("请输入内容:"); int temp = 0; while((temp = input.read()) != -1){//循环接收 char c = (char)temp;//将数据变为字符 if(c =='\n'){ break;//退出循环,按 Enter 键表示输入完成 } buf.append(c);//追加数据 } System.out.println("输入的内容为:"+buf); input.close();//关闭输入流 } /* 结果: * 请输入内容:www.baidu * 输入的内容为:www.baidu * 请输入内容:百度一下 * 输入的内容为:°??????? * */ }
最好的输入方式
最好的输入方式是将全部输入的数据暂时放到一块内存之中,之后一次性的从内存中读取出数据,这样所有数据只读了一次,则不会造成乱码,而且也不会受长度的限制。
输入、输出重定向
从之前的操作中读者已经了解了System.out、System.err、System.in三个常量的作用,但是通过System类也可以改变System.in的输入流来源,以及System.out和System.err两个输出流的输出位置。
为System.out输出重定向
import java.io.File; import java.io.FileOutputStream; import java.io.PrintStream; public class SystemDemo06{ public static void main(String[] args) throws Exception {//所有异常抛出 //System.out 输出重定向 System.setOut(new PrintStream(new FileOutputStream("d:"+File.separator+"test.txt"))); //输出时,不再向控制台上输出,而是向指定的重定向位置输出 System.out.println("www.baidu.com"); System.out.println("百度一下"); } /* 结果: * www.baidu.com * 百度一下 * */ }
为用户保存错误信息
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; public class SystemDemo07{ public static void main(String[] args) { String str = "hello";//声明一个非数字的字符串 try{ System.out.println(Integer.parseInt(str)); }catch(Exception e){ try{ //输出重定位 System.setOut(new PrintStream(new FileOutputStream("d:"+File.separator+"err.log"))); }catch(FileNotFoundException e1){ e1.printStackTrace(); } //输出错误,保存到文件中 System.out.println(e); } } /* 结果: * java.lang.NumberFormatException: For input string: "hello" * */ }
为System.err输出重定向
import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class SystemDemo08{ public static void main(String[] args) throws Exception {//所有异常抛出 ByteArrayOutputStream bos = null;//定义内存输出流 bos = new ByteArrayOutputStream();//实例化内存输出流 System.setErr(new PrintStream(bos));//System.err 输出重定向 System.err.println("www.baidu.com");//错误输出时,不再向屏幕上输出 System.err.println("百度一下");//而是向内存的位置输出 System.out.println(bos);//打印错误信息 } /* 结果: * www.baidu.com * 百度一下 * */ }
设置System.in的输入重定向
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class SystemDemo09{ public static void main(String[] args) throws Exception {//所有异常抛出 System.setIn(new FileInputStream("d:"+File.separator+"demo.txt"));//设置输入重定向 InputStream input = System.in;//从文件中接收数据 byte b[] = new byte[1024];//开辟空间,接收数据 int len = input.read(b);//接收数据 System.out.println("输入的内容为:"+new String(b,0,len)); input.close();//关闭输入流 } /* 结果: * 输入的内容为:www.baidu.com 百度一下 * */
相关推荐
完美的支持单文件/多文件/单文件夹/多文件夹压缩,并可以设置每次Zip文件/文件夹在压缩包中的路径。 但是由于Package的天生缺陷:不支持中文文件名,所以解压缩功能没提供,但是在资源内部的文档中给出了解压缩的...
欢迎使用.NET nanoFramework System.IO.FileSystem类库存储库!建置状态成分建置状态NuGet软件包System.IO.FileSystem System.IO.FileSystem(预览)反馈和文档有关文档,提供反馈,问题以及找出如何做出贡献的信息...
SerialPortStream是System.IO.Ports.SerialPort和SerialStream的独立实现,以实现更好的可靠性和可维护性,现在可移植到Linux系统上的Mono。 SerialPortStream是Stream的基础实现,该Stream缓冲往返于串行端口的...
内存操作流,System对IO流的支持,缓冲区读取,Scanner类,字符编码问题,装饰着模式Decorator
System.Drawing.Image.FromStream 出现参数无效的解决方法
2.支持用户的注册,用户的登录,用户验证过程采用凯撒加密的方式。 3.代码中有详细的注释,数据库的建表语句也存放在代码文件当中。 4.代码逻辑很简单,适合初学者的入门项目。 5.没有UI界面,是纯控制台的文本交互...
System.IO.MemoryStream MStream = new System.IO.MemoryStream(); image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response....
1.2.0 增加了对事件消息确认的支持 例子 有关更多示例,请参见。 可以在我的其他存储库中找到真实示例-加密比较流媒体API客户端: : 打开/关闭连接 // socket is disposed using new "using" syntax, don
其他类提供支持下列操作的服务:数据类型转换,方法参数操作,数学计算,远程和本地程序调用,应用程序环境管理以及对托管和非托管应用程序的监管。 System.Collections 包含定义各种对象集合(如列表、队列、位数...
(请认真阅读此描述)都知道unity 发布出来的webgl是不能使用system.IO文件流的,所以无法通过unity创建文本,通过以下文章可实现网页交互生成文档。 https://blog.csdn.net/qq_33155437/article/details/124124771 ...
System.IO.MemoryStream MStream = new System.IO.MemoryStream(); image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Res ponse.ContentType = "image/Gif";//png , jpg都...
静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和打开一个文件。File类方法的参量很多时候都是路径path。File的一些方法可以返回FileStream和StreamWriter的对象。可以和他们...
在这里告诉大家在.NET Framework4.5中支持异步IO的操作。大大简化之前些的异步方法代码。 使用backgroundworker代码 代码如下:View Code private void Button_Click_3(object sender, RoutedEventArgs e) { ...
Interop.Shell32 1.2版本支持xp 1.0版本支持win7 string dirName = System.IO.Path.GetDirectoryName(FileName); string SongName = System.IO.Path.GetFileName(FileName);//获得歌曲名称 ShellClass sh = new ...
EQXMedia.TxFileSystem是使用System.IO.Abstractions的.NET Standard文件系统抽象的事务性文件系统包装。 该文件系统包装程序支持以下事务: 文件, 目录, 文件流。 NuGet软件包 EQXMedia.TxFileSystem每个...
我为缺少此功能的早期 atom.io 版本开发了此插件。 最新版本已经包含“在 Finder 中显示”菜单,但不包含“使用默认应用程序打开”菜单项。 此插件提供这两种功能 - 但将来不会支持。显示和打开系统内的文件您可以...
this.fileSystemWatcher1.Deleted += new System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Deleted); this.fileSystemWatcher1.Renamed += new System.IO.RenamedEventHandler(this.fileSystemWatcher...
Core.System.Configuration.Install 支持dotnet核心的System.Configuration.Install。 完整框架.NET 4.0中使用的版本。 帮助支持该项目: 用法样本TestInstaller.cs using System . Collections ;using System . ...