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. 【BZOJ3925】[ZJOI2015] 地震后的幻想乡(状压期望DP)

    点此看题面 大致题意: 有\(n\)个点和\(m\)条边,每条边的权值是一个\(0\sim1\)的随机实数,要你用\(n-1\)条边将图联通,问这\(n-1\)条边中边权最大值的期望最小值. 提示 这 ...

  2. 如何让图片相对于上层DIV始终保持水平、垂直都居中

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. sqlldr将txt导入oracle数据库

    注意事项: 1.userid 和 control关键字不要缺少: 2.注意数据库格式:test/test@数据库IP:1521/Oracle8,最后一个是tnsnames中的service_name, ...

  4. CUDA的软件体系

    CUDA的软件堆栈由以下三层构成:CUDA Library.CUDA runtime API.CUDA driver API,如图所示,CUDA的核心是CUDA C语言,它包含对C语言的最小扩展集和一 ...

  5. 图片url转base64

    var xhr = new XMLHttpRequest() // 配置的代理,解决跨域问题 xhr.open('GET', url.replace('http://xxx.com', '/img') ...

  6. CentOS---JDK安装与配置

    1.先查看一下CentOS中存在的jdk安装包信息 # rpm -qa | grep java 查看CentOS安装的jdk版本 #java -version 2.分别执行以下命令将所有相关包都删除 ...

  7. 微信小程序第3课 目录结构及小知识点

    目录 目录结构 安装包下载地址 一.pages目录介绍 二.index目录介绍 index.js(相当JavaScript文件,必不可少的) index.json(可以不需要) index.wxml( ...

  8. [项目2] 10mins

    1.准备工作 M层:生成虚假数据 from django.db import models from faker import Factory # Create your models here. c ...

  9. 6 json和ajax传递api数据

    1 2 3 4 https://swapi.co/ <h1>Hello Reqwest!</h1> <script> var a = {} reqwest({ ur ...

  10. python协程--yield和yield from

    字典为动词“to yield”给出了两个释义:产出和让步.对于 Python 生成器中的 yield 来说,这两个含义都成立.yield item 这行代码会产出一个值,提供给 next(...) 的 ...