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. 【BZOJ4001】[TJOI2015] 概率论(卡特兰数)

    点此看题面 大致题意: 问你一棵\(n\)个节点的有根二叉树叶节点的期望个数. 大致思路 看到期望,比较显然可以想到设\(num_i\)为\(i\)个节点的二叉树个数,\(tot_i\)为所有\(i\ ...

  2. 【BZOJ4571】[SCOI2016] 美味(主席树)

    点此看题面 大致题意: 给你一个序列\(a\),然后每次询问\(max_{i=l}^r(a_i+x)\ xor\ b\). 大致思路 首先,我们要知道一个简单的性质:位运算时位与位之间是互不影响的. ...

  3. 3218: 字符串字符统计—C语言

    3218: 字符串字符统计—C语言 时间限制: 1 Sec  内存限制: 128 MB提交: 270  解决: 129[提交][状态][讨论版][命题人:smallgyy] 题目描述 编写一函数,由实 ...

  4. appium---adb通过wifi连接手机

    前几天接到领导的安排,想要测试下apk的耗电量,可以通过手机adb命令进行监控手机电量的变化:但是这样如果通过USB连接手机的话,USB就会自动给手机进行充电,无法达到我们想要的结果,于是想到了通过w ...

  5. ZJOI2004 午餐

    题目传送门 嗯--我承认我看了题解,不过好歹有了点自己的思路,大约蒙出来了\(30\%\)(个人感觉)-- 学会\(DP\),任重而道远啊! Step1.贪心排序 先将每个人按吃饭的快慢排序,然后再进 ...

  6. 从多个textarea中随机选取一个内容

    <div id="IMContentTest"> <textarea name="IMContent" class="IMClass ...

  7. HDU 2045 LELE的RPG难题

    递推 枚举起点状态 #include <algorithm> #include <iostream> #include <cstring> #include < ...

  8. sendmail安装与配置

    一.安装sendmail与mail 1.安装sendmail:  1) centos下可以安装命令:yum -y install sendmail 2) 安装完后启动sendmail命令:servic ...

  9. 二、MySQL 管理

    MySQL 管理 启动及关闭 MySQL 服务器 Windows 系统下 在 Windows 系统下,打开命令窗口(cmd),进入 MySQL 安装目录的 bin 目录. 启动: cd c:/mysq ...

  10. 为什么90%的IT人员都不适合做老大?

    什么是格局? 格局就是能够很好的平衡短期利益和长期利益. 过分注重短期利益的人必然会失去长期利益,到头来一定会很普通. 例如:跳槽不断,可能短期薪资会增长,但长期来看后劲可能会不足,未来发展空间会变窄 ...