12、java中的I/O流(2)

再介绍一下其他一些流的使用

数据操作流,数据输入流允许应用程序以独立于机器的方式从底层输入流读取原始Java数据类型,意思就是平台无关,相关的两个类DataInputStream、DataOutputStream,使用如下:

public class DataInputStreamTest {public static void main(String[] args) {DataInputStreamTest test = new DataInputStreamTest();test.testDataOutputStream();
//		test.testDataInputStream();}public void testDataInputStream() {DataInputStream dataInputStream = null;try {dataInputStream = new DataInputStream(new FileInputStream("E:\\test\\input.doc"));byte []b = new byte[1024];dataInputStream.read(b);System.out.println(new String(b));} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {if (dataInputStream != null) {try {dataInputStream.close();} catch (IOException e) {e.printStackTrace();}}}}public void testDataOutputStream() {DataOutputStream dataOutputStream = null;try {byte []by = new byte[2048];ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream(2048);dataOutputStream = new DataOutputStream(byteArrayInputStream);dataOutputStream.writeInt('5');dataOutputStream.writeChars("hahaha");dataOutputStream.writeInt(6);byte[] bs = byteArrayInputStream.toByteArray();for (byte b : bs) {System.out.print (b+" ");}//存入非字符型数据乱码,还不知为何//0 0 0 53 0 104 0 97 0 104 0 97 0 104 0 97 0 0 0 6 } catch (Exception e) {}finally {try {if (dataOutputStream != null) {dataOutputStream.close();}} catch (IOException e) {e.printStackTrace();}}}}

打印流,可用于将对象的格式表示打印到文本输出流,相关的类PrintWriter,使用如下:

public void testPrintWriter() {PrintWriter printWriter = null;BufferedReader bufferedReader = null;try {bufferedReader = new BufferedReader(new FileReader("E:\\test\\input.txt"));printWriter = new PrintWriter(new File("E:\\test\\printwriter.txt"));String s = null;while((s = bufferedReader.readLine()) != null) {printWriter.write(s);}} catch (Exception e) {}finally {if (printWriter != null) {printWriter.close();}if (bufferedReader!=null) {try {bufferedReader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

标准输入输出流,相关操作如下:

/*** 标准输入流使用*/private void testStandInputStream() {//标准输入流,可用于键盘录入Scanner scanner = new Scanner(System.in);System.out.println("=======课题选择========");System.out.println("\t计算机原理:001");System.out.println("\t信息系统论:002");System.out.println("\t计算机网络:003");System.out.println("\t数据库原理:004");System.out.println("====================");System.out.println("请输入要选择的课题代号,按回车键确定");String nextLine = scanner.nextLine();if ("001".equals(nextLine)) {System.out.println("选择的课程是:计算机原理");}else if ("002".equals(nextLine)) {System.out.println("选择的课程是:信息系统论");}else if ("003".equals(nextLine)) {System.out.println("选择的课程是:计算机网络");}else if ("004".equals(nextLine)) {System.out.println("选择的课程是:数据库原理");}System.out.println("系统关闭!");System.exit(-1);}//标准输出流   private void testStandOutputStream() {PrintStream out = System.out;out.println("打印信息到控制台");}

对象输入输出流,也叫做序列化流,可用于对象的序列化和反序列化,就是可以实现将对象写入文本、从文本中读取信息到对象中(这里是读取数据到对象),常用于网络信息传输和远程方法调用,实例如下:

/***	测试对象输入输出流也叫做序列化流*	通过对象输出流可以进行序列化,将对象写入到文本,通过对象输入流可以实现反序列化,从文本中读取对象*/
public class ObjectStreamTest {public static void main(String[] args) {ObjectStreamTest test = new ObjectStreamTest();test.testObjectOutputStream();test.testObjectInputStream();}/***	通过对象输出流可以进行序列化,将对象写入到文本*/public void testObjectOutputStream() {ObjectOutputStream objectOutputStream = null;try {objectOutputStream = new ObjectOutputStream(new FileOutputStream("E:\\test\\user.txt"));User user = new User("小王", 13);objectOutputStream.writeObject(user);objectOutputStream.flush();objectOutputStream.close();} catch (Exception e) {e.printStackTrace();}}/***	通过对象输入流可以实现反序列化,从文本中读取对象*/public void testObjectInputStream() {ObjectInputStream objectInputStream = null;try {objectInputStream = new ObjectInputStream(new FileInputStream("E:\\test\\user.txt"));Object readObject = objectInputStream.readObject();System.out.println(readObject);//User [name=小王, age=0]objectInputStream.close();} catch (Exception e) {e.printStackTrace();}}}/*** 对象一定要实现序列化接口*/
class User implements Serializable{private static final long serialVersionUID = -1796903937206347177L;private String name;private transient int age;//可以防止成员被序列化public User(String name,int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "User [name=" + name + ", age=" + age + "]";}}

介绍最后一个,合并流,可用于将多个输入流中数据合并到一个流中,使用如下:

public class AddStreamTest {/*** 将两个流中的数据同时写到一个文件中*/public static void main(String[] args) throws IOException {FileOutputStream fileOutputStream = new FileOutputStream("E:\\test\\input3.txt");FileInputStream fileInputStream1 = new FileInputStream("E:\\test\\input.txt");FileInputStream fileInputStream2 = new FileInputStream("E:\\test\\input2.txt");SequenceInputStream sequenceInputStream = new SequenceInputStream(fileInputStream1,fileInputStream2);byte []b = new byte[1024];int len = 0;while((len = sequenceInputStream.read(b, 0, b.length)) != -1) {fileOutputStream.write(b, 0, len);}fileOutputStream.close();sequenceInputStream.close();fileInputStream1.close();fileInputStream2.close();}
}

还有一些其他的流,在这里就不做介绍了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/321747.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

nssl1437-逮虾户【二分答案】

正题 题目大意 nnn段路&#xff0c;每一段路速度是vidv_idvi​d&#xff0c;长度是sis_isi​&#xff0c;在ttt秒跑完了&#xff0c;求ddd的值。 解题思路 指数域二分&#xff0c;然后判断是否跑完即可。 codecodecode #include<cstdio> #include<cstring> #incl…

动态规划训练25 [Food Delivery ZOJ - 3469 ]好题

Food Delivery ZOJ - 3469 区间DP的一道好题。 在这道题里&#xff0c;无非就是从出发点向左走到x1再向右走到有y1&#xff0c;再向左走到x2&#xff0c;再向右走到y2.。。。这样&#xff0c;一直将所有的顾客遍历完。 显然&#xff0c;起点这个点是非常特殊的一个点&#xf…

使用C#开发Android应用之WebApp

近段时间了解了一下VS2017开发安卓应用的一些技术&#xff0c;特地把C#开发WebApp的一些过程记录下来&#xff0c;欢迎大家一起指教、讨论&#xff0c;废话少说&#xff0c;是时候开始表演真正的技术了。。1、新建空白Android应用2、拖一个WebView控件进来3、打开模拟器Genymot…

13、字符集和字符编码

字符集&#xff1a;字符集是多个字符的集合&#xff0c;常见字符集有&#xff1a;ASCII字符集、GB2312字符集、GB18030字符集、Unicode字符集等。 ASCII字符集&#xff1a;是英文大小写字符、阿拉伯数字和西文符号的一个集合。&#xff08;可以看一下电脑键盘上的键&#xff0c…

nssl1438-战略威慑【枚举,树的直径】

正题 题目大意 nnn个点的无根树&#xff0c;求两条不相交的路径使它们长度之积最大。 解题思路 我们暴力枚举第一条&#xff0c;然后求树的直径即可。 codecodecode #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const i…

ASP.NET Core依赖注入深入讨论

这篇文章我们来深入探讨ASP.NET Core、MVC Core中的依赖注入&#xff0c;我们将示范几乎所有可能的操作把依赖项注入到组件中。依赖注入是ASP.NET Core的核心&#xff0c;它能让您应用程序中的组件增强可测试性&#xff0c;还使您的组件只依赖于能够提供所需服务的某些组件。举…

15、java中的集合(2)

说一下单列集合&#xff0c;java中的单列集合的顶级接口是Collection&#xff0c;它有两个子接口&#xff1a;List、Set&#xff0c;本篇介绍一下List接口及其实现类的功能方法和基本实现原理。 List集合是有序集合&#xff0c;这里的有序并不是指存入List集合的元素会被自动排…

P2467-[SDOI2010]地精部落【dp】

正题 题目链接:https://www.luogu.org/problem/P2467 题目大意 求长度为nnn的波动序列的个数。 解题思路 我们先考虑第一个是上升的&#xff0c;然后乘2即可。 设fi,jf_{i,j}fi,j​表示填1∼i1\sim i1∼i个&#xff0c;最前面的是jjj的个数。然后我们只要是1∼i−j11\sim i…

双向广搜 8数码问题

转载自&#xff1a;http://blog.sina.com.cn/s/blog_8627bf080100ticx.html Eight 题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1043讲到双向广搜&#xff0c;那就不能不讲经典的八数码问题&#xff0c;有人说不做此题人生不完整 。所谓双向广搜&am…

使用静态基类方案让 ASP.NET Core 实现遵循 HATEOAS Restful Web API

Hypermedia As The Engine Of Application State (HATEOAS)HATEOAS&#xff08;Hypermedia as the engine of application state&#xff09;是 REST 架构风格中最复杂的约束&#xff0c;也是构建成熟 REST 服务的核心。它的重要性在于打破了客户端和服务器之间严格的契约&…

U94222-循环往复【tarjan,DAGdp】

正题 题目链接:https://www.luogu.org/problem/U94222?contestId23574 题目大意 nnn个点若条有向边,求一条路径要求 经过一个酒店经过权值最大消费最小起点编号最小 按照顺序满足。 解题思路 将强连通分量缩成一个点&#xff0c;然后用gi,0/1g_{i,0/1}gi,0/1​到达第iii个…

16、java中的集合(3)

说一下双列集合&#xff0c;顶级接口是Map&#xff0c;实现类有HashMap、LinkedHashMap、TreeMap、HashTable等&#xff0c;使用键值对的格式存储数据&#xff0c;键不可以重复&#xff0c;值可以重复。接下来对实现类做一下详细介绍。 HashMap是最常用的Map集合&#xff0c;它…

搜索训练1 [8数码问题]

HDU1043、以及POJ1077上面都有这道题目&#xff0c;可以说是搜索里的非常经典的题目了。 poj上面的数据真的是弱&#xff0c;由于只有一组数据&#xff0c;简单bfs直接就可以过掉。 前前后后捣鼓了能有6个小时&#xff0c;才把这道题目在HDU上以4500ms的微弱优势通过。。。。…

【招聘(北京)】.NETCORE开发工程师(微服务方向)

组织&#xff1a;华汽集团北京研发中心位置&#xff1a;北京市朝阳区焦奥中心官网&#xff1a;www.sinoauto.com邮箱&#xff1a;taoxu.weisinoauto.com 项目&#xff1a;打造面向国内汽车后市场用户的一站式云服务平台&#xff08;华汽云&#xff09;&#xff0c;形态包括B2B、…

2017西安交大ACM小学期数据结构 [分块,区间修改,单点查询]

Problem A 发布时间: 2017年6月28日 09:29 最后更新: 2017年6月28日 13:03 时间限制: 1000ms 内存限制: 32M 描述 给定一个长度为n的序列a1, a2, ..., an给出q个操作, 操作分为两种 对于形如1xyz的操作, 将下标介于[x,y]的元素加上z, 满足1≤x≤y≤n, 1≤z≤105对于形如2…

17、java中的集合(4)

之前单列集合只说过了List系列的集合&#xff0c;接下来再说一下Set集合系列&#xff0c;Set集合是无序集合&#xff08;存取顺序不一致&#xff09;&#xff0c;不允许添加相同元素&#xff0c;Set的实现依赖于Map集合&#xff0c;可以将Set集合看作Map集合键的集合&#xff0…

U92904-画地为佬【二分,结论】

正题 题目链接:https://www.luogu.org/problem/U92904?contestId23574 题目大意 用mmm根长度为1的火柴求能够圈住的最多块的地。 解题思路 显然如果刚好能够围成一个正方形那么一定是最优的&#xff0c;那么我们先将能够围成的围成一个最大的正方形&#xff0c;然后剩下的在…

确保线程安全下使用Queue的Enqueue和Dequeue

场景是这样&#xff0c;假设有一台设备会触发类型为Alarm的告警信号&#xff0c;并把信号添加到一个Queue结构中&#xff0c;每隔一段时间这个Queue会被遍历检查&#xff0c;其中的每个Alarm都会调用一个相应的处理方法。问题在于&#xff0c;检查机制是基于多线程的&#xff0…

2017西安交大ACM小学期数据结构 [分块、二维矩阵]

Problem B 发布时间: 2017年6月28日 10:06 最后更新: 2017年6月28日 16:35 时间限制: 2000ms 内存限制: 32M 描述 给定一个nm的矩形, 其中第i行第j列的值为ai,j给出q个操作, 操作有两种 对于形如1x1y1x2y2z的操作, 将(x1,y1)-(x2,y2)这段矩形区域的所有元素加上z, 满足1≤…

18、java中的泛型

之前介绍集合时&#xff0c;可以看到有List<String>这样的写法&#xff0c;那么尖括号里的内容是什么呢&#xff1f;这是泛型&#xff0c;意思就是说声明的这个List集合只能存放String类型的元素。 泛型是什么&#xff1f; ‘泛’指一般、不深入&#xff0c;在这里可以认…