import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.OutputStreamWriter;

import java.io.PrintStream;

import java.io.RandomAccessFile;

import java.io.Serializable;

import org.junit.Test;

public class TestOtherStream {

/**

*RandomAccessFile:支持随机访问

*1.可以充当一个输入流,又可以充当输出流(test3)

*2.支持文件的开头读取,写入,

*3.也可以支持从任意位置的的读取,写入(test4)

*

*支持随机

* @throws Exception

*/

@Test

public void test3() throws Exception{

File file = new File("hello.txt");

File file1 = new File("hello1.txt");

RandomAccessFile raf = new RandomAccessFile(file, "r");

RandomAccessFile raf1 = new  RandomAccessFile(file1,"rw");

byte[] b = new byte[20];

int len;

while((len = raf.read(b)) != -1){

raf1.write(b, 0, len);

}

raf1.close();

raf.close();

}

//实现了覆盖的操作

@Test

public void test4() throws Exception{

File file = new File("hello.txt");

RandomAccessFile raf = new RandomAccessFile(file, "rw");

raf.seek(3);

raf.write("xy".getBytes());

}

//实现插入

@Test

public void test5() throws Exception{

File file = new File("hello1.txt");

RandomAccessFile raf = new RandomAccessFile(file, "rw");

raf.seek(4);

String str = raf.readLine();

raf.seek(4);

raf.write("xy".getBytes());

raf.write(str.getBytes());

}

//实现插入(复杂文件,也就是有多行)

@Test

public void test6() throws Exception{

File file = new File("hello1.txt");

RandomAccessFile raf = new RandomAccessFile(file, "rw");

raf.seek(4);

byte[] b = new byte[20];

int len;

StringBuffer sb = new StringBuffer();

while((len = raf.read(b)) != -1){

sb.append(new String(b,0,len));

}

raf.seek(4);

raf.write("xy".getBytes());

raf.write(sb.toString().getBytes());

}

//反序列号,将硬盘中的文件通过ObjectIntputStream转为相应的对象,存储到硬盘中

@Test

public void testObject1() throws Exception{

FileInputStream fis = new FileInputStream("/Users/lixiuming/Desktop/node/person.txt");

ObjectInputStream  ois = new ObjectInputStream(fis);

Person p1 = new Person();

p1 = (Person) ois.readObject();

Person p2 = new Person();

p2 = (Person) ois.readObject();

System.out.println(p1+"*******"+p2);

}

// 对象的序列化过程,讲内存中的对象通过ObjectOutputStream转化二进制流,存在硬盘中

@Test

public void testObject() {

Person p1 = new Person("小米", 20);

Person p2 = new Person("红米", 23);

ObjectOutputStream oos = null;

try {

FileOutputStream fos = new FileOutputStream("/Users/lixiuming/Desktop/node/person.txt");

oos = new ObjectOutputStream(fos);

oos.writeObject(p1);

oos.flush();

oos.writeObject(p2);

oos.flush();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (oos != null) {

try {

oos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

// 用数据流来解析文件

@Test

public void testData1() throws Exception {

File file = new File("data.txt");

FileInputStream fis = new FileInputStream(file);

DataInputStream dis = new DataInputStream(fis);

// int len;

// byte[] b = new byte[20];

// while((len = dis.read(b)) != -1){

// String str = new String(b,0,len);

// System.out.println(str);

// }

String str = dis.readUTF();

Boolean b = dis.readBoolean();

long l = dis.readLong();

System.out.println("str:" + str + "," + "Bollean:" + b + "," + "long:" + b);

}

/**

* 数据流 ,用来处理基本数据类型,String , 字节数组的数据:DatainputStream,DataOutputStream

*

* @throws Exception

*/

@Test

public void testData() throws Exception {

FileOutputStream fos = new FileOutputStream("data.txt");

DataOutputStream dos = new DataOutputStream(fos);

dos.writeUTF("ksdflskdfhslkdf");

dos.writeBoolean(true);

dos.writeLong(123123);

dos.close();

fos.close();

}

/**

* 打印流(处理流),字节流,PrintStream 字符流,printWriter

*

* @throws Exception

*

*

*/

@Test

public void printSrtreamWriter() throws Exception {

File file = new File("hello7.txt");

FileOutputStream fos = new FileOutputStream(file);

// 设置打印位置

PrintStream ps = new PrintStream(fos, true);

System.setOut(ps);

for (int i = 0; i < 255; i++) {

System.out.print((char) i);

if (i % 50 == 0) {

System.out.println();// 换行

}

}

ps.close();

}

/**

* 标注的输入输出流 标准的输出流(字节流),System.in, 标准的输入流(字节流) System.out

* 键盘输入,把输入内容转化成大写,输出,当直接输入“e”或者“exit”时,退出程序

*

* @throws Exception

*/

@Test

public void test2() throws Exception {

InputStream is = System.in;

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String str = null;

while (true) {

System.out.print("请输入字符串:");

str = br.readLine();

if (str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")) {

break;

} else {

String str1 = str.toUpperCase();

System.out.println(str1);

}

}

br.close();

isr.close();

is.close();

}

/**

* 转换流,InputStreamReader OutputStreamWriter 编码:将字符串转化为字节数组 解码:字节数组转化为字符串

*

*/

@Test

public void test1() throws Exception {

// 解码

File file = new File("hello3.txt");

FileInputStream fis = new FileInputStream(file);

InputStreamReader isr = new InputStreamReader(fis, "utf-8");

BufferedReader br = new BufferedReader(isr);

// 编码

File file2 = new File("hello6.txt");

FileOutputStream fos = new FileOutputStream(file2);

OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");

BufferedWriter bw = new BufferedWriter(osw);

int len;

char[] c = new char[100];

while ((len = br.read(c)) != -1) {

// String str = new String(c,0,len);

// System.out.println(str);

bw.write(c, 0, len);

bw.flush();

}

bw.close();

osw.close();

fos.close();

br.close();

isr.close();

fis.close();

}

}

/**

* 要实现序列号的类, 要求是可序列化的:实现两个接口之一,Serializable

* 提供一个序列号,作用是:防止出错

* 不可序列号:static和transient修饰的成员变量

*/

class Person implements Serializable {

private static final long serialVersionUID = 1L;

String name;

int 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;

}

public Person(String name, int age) {

super();

this.name = name;

this.age = age;

}

public Person() {

super();

}

@Override

public String toString() {

return "Person [name=" + name + ", age=" + age + "]";

}

}

OtherStream的更多相关文章

  1. Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Spark Streaming简介 1.1 概述 Spark Streaming 是Spa ...

  2. Flink DataStream API Programming Guide

    Example Program The following program is a complete, working example of streaming window word count ...

  3. Spark Streaming官方文档学习--上

    官方文档地址:http://spark.apache.org/docs/latest/streaming-programming-guide.html Spark Streaming是spark ap ...

  4. Flink Program Guide (2) -- 综述 (DataStream API编程指导 -- For Java)

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  5. Spark Streaming笔记——技术点汇总

    目录 目录 概况 原理 API DStream WordCount示例 Input DStream Transformation Operation Output Operation 缓存与持久化 C ...

  6. Apache Spark 2.2.0 中文文档 - Spark Streaming 编程指南 | ApacheCN

    Spark Streaming 编程指南 概述 一个入门示例 基础概念 依赖 初始化 StreamingContext Discretized Streams (DStreams)(离散化流) Inp ...

  7. Spark Streaming编程指南

    Overview A Quick Example Basic Concepts Linking Initializing StreamingContext Discretized Streams (D ...

  8. Spark Streaming中的操作函数分析

    根据Spark官方文档中的描述,在Spark Streaming应用中,一个DStream对象可以调用多种操作,主要分为以下几类 Transformations Window Operations J ...

  9. Spark的Streaming和Spark的SQL简单入门学习

    1.Spark Streaming是什么? a.Spark Streaming是什么? Spark Streaming类似于Apache Storm,用于流式数据的处理.根据其官方文档介绍,Spark ...

随机推荐

  1. python_42_文件补充

    m=['红烧肉\n','熘肝尖','西红柿炒鸡蛋','腊八粥','油焖大虾'] fname=input("请输入文件名:")#输入xxx f=open(fname,'w',enco ...

  2. python换行

    python中如果一行代码太长,看着不方便时,怎么办? 只需要在需要换行的地方添加上符号 \ 就行了.

  3. Oracle 分区表的索引、分区索引

    对于分区表,可以建立不分区索引.也就是说表分区,但是索引不分区.以下着重介绍分区表的分区索引. 索引与表一样,也可以分区.索引分为两类:locally partition index(局部分区索引). ...

  4. Vue之Vue-touch的使用

    最近项目中,有的页面发现设置返回键看起来怪怪的,感觉与整体不协调,于是就考虑使用手势滑动事件来实现返回功能~ 开叉查阅资料~找到了vue-touch,使用起来可谓是简单粗暴啊,适合我这样的快速开发人员 ...

  5. expect用法举例

    1 expect -c 'spawn su - oracle -s check_tablespace.shexpect "Password:"send "oracle\n ...

  6. BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)

    题意 给你一张无向图,保证从1号点到每个点的最短路唯一.对于每个点求出删掉号点到它的最短路上的最后一条边(就是这条路径上与他自己相连的那条边)后1号点到它的最短路的长度 Sol emmm,考场上想了个 ...

  7. Linux 连接 Internet

    本文根据<鸟哥的Linux私房菜-服务器架设篇>第四章总结 Linux 连接 Internet 前的注意事项 想要连接 Internet 需要配置一组合法的 IP 参数,主要是 IP.Ne ...

  8. Python_列表、字典、字符串、集合操作

    一.list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素.对于list的操作,我们要学会增删改查. 查 我们可以直接索引查找,也可以通过切片 ...

  9. DevOps - 部署系统 - Cobbler

    Cobbler简介 Cobbler由python语言开发,是对PXE和Kickstart以及DHCP的封装.融合很多特性,提供了CLI和Web的管理形式.更加方便的实行网络安装.适用场景:需要大批量的 ...

  10. Mysql关闭和修改密码

    数据库的关闭方法: 1.优雅的关闭数据库的方法:mysqladmin -uroot -p123456 shutdown 2.脚本关闭:/etc/init.d/mysqld stop 3.使用kill信 ...