Day 18:SequenceInputStream、合并切割mp3、对象输入输出流对象
SequenceInputStream用例题讲述用法
需求:1.把a.txt与b.txt 文件的内容合并
2.把a.txt与b.txt 、c.txt文件的内容合并
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList; public class Demo1 {
public static void main(String[] args) throws IOException {
fangfa1();
} private static void fangfa1() throws IOException {
// TODO Auto-generated method stub
File file1 = new File("F:\\a.txt");
File file2 = new File("F:\\b.txt");
File file3 = new File("F:\\c.txt"); FileInputStream filein1 = new FileInputStream(file1);
FileInputStream filein2 = new FileInputStream(file2);
FileOutputStream fileout3 = new FileOutputStream(file3); ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
list.add(filein1);
list.add(filein2); byte[] buf = new byte[1024];
int content;
for(int i = 0;i < list.size();i++) {
FileInputStream filein = list.get(i);
while((content = filein.read(buf))!=1) {
fileout3.write(buf, 0, content);
}
filein.close();
}
fileout3.close();
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList; public class Demo2 {
public static void main(String[] args) throws IOException {
fangfa1();
} private static void fangfa1() throws IOException {
// TODO Auto-generated method stub
File file1 = new File("F:\\a.txt");
File file2 = new File("F:\\b.txt");
File file3 = new File("F:\\c.txt"); FileInputStream filein1 = new FileInputStream(file1);
FileInputStream filein2 = new FileInputStream(file2);
FileOutputStream fileout3 = new FileOutputStream(file3); SequenceInputStream sinput = new SequenceInputStream(filein1, filein2); byte[] buf = new byte[1024];
int content;
while((content = sinput.read(buf))!=1) {
fileout3.write(buf, 0, content);
}
sinput.close();
fileout3.close();
}
}
//第二题
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector; public class Demo3 {
public static void main(String[] args) throws IOException {
fangfa1();
} private static void fangfa1() throws IOException {
// TODO Auto-generated method stub
File file1 = new File("F:\\a.txt");
File file2 = new File("F:\\b.txt");
File file3 = new File("F:\\c.txt");
File file4 = new File("F:\\d.txt"); FileInputStream filein1 = new FileInputStream(file1);
FileInputStream filein2 = new FileInputStream(file2);
FileInputStream filein3 = new FileInputStream(file3);
FileOutputStream fileout4 = new FileOutputStream(file4); Vector<FileInputStream> vector = new Vector<FileInputStream>();
vector.add(filein1);
vector.add(filein2);
vector.add(filein3);
Enumeration<FileInputStream> it = vector.elements();
SequenceInputStream sinput = new SequenceInputStream(it); byte[] buf = new byte[1024];
int content;
while((content = sinput.read(buf))!=1) {
fileout4.write(buf, 0, content);
}
sinput.close();
fileout4.close();
}
}
上题第三段代码用Vector集合的原因是在SequenctInputStream中有一个带参数的构造方法调用的是Vector的迭代器!
需求: 把一首mp3先切割成n份,然后再把这些文件合并起来。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector; public class Demo4 {
public static void main(String[] args) throws IOException {
//jianqie();//剪切
hebing();//合并
} public static void hebing() throws IOException{
File dir = new File("F:\\music");
Vector<FileInputStream> vector = new Vector<FileInputStream>();
File[] files = dir.listFiles();
for(File file : files){
if(file.getName().endsWith(".mp3")){
vector.add(new FileInputStream(file));
}
}
Enumeration<FileInputStream> e = vector.elements(); SequenceInputStream inputStream = new SequenceInputStream(e); FileOutputStream fileOutputStream = new FileOutputStream("F:\\合并.mp3"); byte[] buf = new byte[1024];
int length = 0 ;
while((length = inputStream.read(buf))!=-1){
fileOutputStream.write(buf,0,length);
}
fileOutputStream.close();
inputStream.close();
} public static void jianqie() throws IOException{
File file = new File("F:\\1.mp3");
File dir = new File("F:\\music"); FileInputStream fileInputStream = new FileInputStream(file); byte[] buf = new byte[1024*1024];
int length = 0;
for(int i = 0 ; (length = fileInputStream.read(buf))!=-1 ; i++){
FileOutputStream fileOutputStream = new FileOutputStream(new File(dir,"part"+i+".mp3"));
fileOutputStream.write(buf,0,length);
fileOutputStream.close();
}
fileInputStream.close();
}
}
对象的输入输出流 : 对象的输入输出流主要的作用是用于写对象的信息与读取对象的信息。 对象信息一旦写到文件上那么对象的信息就可以做到持久化了
对象的输出流: ObjectOutputStream .
对象的输入流: ObjectInputStream
ObjectOutputStream的使用步骤:
对象输入输出流要注意的细节:
1. 如果对象需要被写出到文件上,那么对象所属的类必须要实现Serializable接口。 Serializable接口没有任何的方法,是一个标识接口而已。
2. 对象的反序列化创建对象的时候并不会调用到构造方法的、
3. serialVersionUID 是用于记录class文件的版本信息的,serialVersionUID这个数字是通过一个类的类名、成员、包名、工程名算出的一个数字。
4. 使用ObjectInputStream反序列化的时候,ObjeectInputStream会先读取文件中的serialVersionUID,然后与本地的class文件的serialVersionUID
进行对比,如果这两个id不一致,那么反序列化就失败了。
5. 如果序列化与反序列化的时候可能会修改类的成员,那么最好一开始就给这个类指定一个serialVersionUID,如果一类已经指定的serialVersionUID,然后
在序列化与反序列化的时候,jvm都不会再自己算这个 class的serialVersionUID了。
6. 如果一个对象某个数据不想被序列化到硬盘上,可以使用关键字transient修饰。
7. 如果一个类维护了另外一个类的引用,那么另外一个类也需要实现Serializable接口。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable; class Address implements Serializable{
String country;
String city; public Address(String country,String city){
this.country = country;
this.city = city;
}
} class User implements Serializable{ String userName ;
String password;
transient int age;// transient 透明
Address address ; public User(String userName , String passwrod) {
this.userName = userName;
this.password = passwrod;
} public User(String userName , String passwrod,int age,Address address) {
this.userName = userName;
this.password = passwrod;
this.age = age;
this.address = address;
}
@Override
public String toString() {
return "用户名:"+this.userName+ " 密码:"+ this.password+" 年龄:"+this.age+" 地址:"+this.address.city;
}
} public class Demo5 {
public static void main(String[] args) throws IOException, Exception {
writeObj();
//readObj();
}
//把文件中的对象信息读取出来
public static void readObj() throws IOException, ClassNotFoundException{
//找到目标文件
File file = new File("F:\\obj.txt");
//建立数据的输入通道
FileInputStream fileInputStream = new FileInputStream(file);
//建立对象的输入流对象
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
//读取对象信息
User user = (User) objectInputStream.readObject();
System.out.println(user);
} //定义方法把对象的信息写到硬盘上
public static void writeObj() throws IOException{
//把user对象的信息持久化存储。
Address address = new Address("中国","广州");
User user = new User("admin","123",15,address);
//找到目标文件
File file = new File("F:\\obj.txt");
//建立数据输出流对象
FileOutputStream fileOutputStream = new FileOutputStream(file);
//建立对象的输出流对象
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
//把对象写出
objectOutputStream.writeObject(user);
//关闭资源
objectOutputStream.close();
}
}
Day 18:SequenceInputStream、合并切割mp3、对象输入输出流对象的更多相关文章
- 对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- (JAVA)从零开始之--对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- java 对象输入输出流
对象的输入输出流的作用: 用于写入对象 的信息读取对象的信息. 对象的持久化. 比如:用户信息. ObjectInputStream : 对象输入流 ...
- 对象输入输出流ObjectInputStream、ObjectOutputStream(对象的序列化与反序列化)
如题 所有关联的类需要继承Serializable 接口 文件为空,直接反序列化为发生错误; 毕竟对象为null , 序列化到文件里不是空空的! 以下笔记的原文连接: https://www.cnbl ...
- serialVersionUID序列化版本号与ObjectOutputStream对象输入输出流
1. 观察ObjectOutputStream 我们观察ObjectOutputStream就可以发现该类没有无参构造,只有有参构造,所以他是一个包装流 2. 具体使用: public static ...
- CString与输入输出流对象问题。
在C++ 编程出现:cin>>Id;没有与这些操作匹配的">>"运算符: 你要看你的Id的数据类型,如果是CString等字符串,要用cin.getline ...
- 输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- 《三》Java IO 字节输入输出流
那么这篇博客我们讲的是字节输入输出流:InputStream.OutputSteam(下图红色长方形框内),红色椭圆框内是其典型实现(FileInputSteam.FileOutStream) ...
- Java IO详解(一)------字节输入输出流
File 类的介绍:http://www.cnblogs.com/ysocean/p/6851878.html Java IO 流的分类介绍:http://www.cnblogs.com/ysocea ...
随机推荐
- textarea不允许修改大小
参考:http://www.runoob.com/jsref/dom-obj-textarea.html resize:none;
- 等级保护2.0-mysql
控制点 安全要求 要求解读 测评方法 预期结果或主要证据 身份鉴别 a)应对登录的用户进行身份标识和鉴别,身份标识具有唯一性,身份鉴别信息具有复杂度要求并定期更换 应检查MySQL数据库的口令策略配置 ...
- Memcached 最新版本发布,不再仅仅是个内存缓存了
导读 Memcached 1.5.18和之后版本可以在服务重启时恢复内存缓存.新版本还通过DAX文件系统挂载来实现缓存持久性功能. 可以通过在启动选项使用该功能: -e /tmpfs_mount/me ...
- Linux用户和用户组管理命令
一.用户管理命令 1.useradd 创建用户或更新默认新用户的信息 使用方法 useradd [options] 用户名 选项: useradd -u 指定UID具体数值, ...
- 题解:luogu P3909
这个题拖了快三个月了,只因缺个快速乘(气愤.jpg). 题目链接:P3909 异或之积 你确定没人用前缀和,后缀和吗? 蒟蒻想法与众不同! 我们实验\(A[]={1,2,3,4}\). 这里计不乘6时 ...
- 攻防世界--web新手练习区(1)
1. 题目描述:X老师想让小明同学查看一个网页的源代码,但小明却发现鼠标右键不管用了. http://111.198.29.45:53629 通过阅读题目描述分析,我们需要查看源码,但是鼠标右键 ...
- NO31 配置网卡--主机名--网络故障排查面试题--DNS
修改网卡配置信息: 修改主机名规范的三个步骤: 配置默认网关: DNS解析过程,用命令看: DNS相关命令: 口述DNS解析过程: 客户端(电脑)通过浏览器输入域名,先找hosts文件及本地dns缓 ...
- java多线程(待完善)
1.小型系统 // 线程完成的任务(Runnable对象)和线程对象(Thread)之间紧密相连 class A implements Runnable{ public void run(){ // ...
- python+ selenium + webdriver的环境准备
web自动化安装 1.安装最新的selenium pip install -U selenium 2.安装chrom浏览器和chromdriver的下载 http://chromedriver.sto ...
- webpack 命令 Module build failed (from ./node_modules/babel-loader/lib/index.js) 错误问题解决方案
在项目中运行的时候出现报错,错误为Module build failed (from ./node_modules/babel-loader/lib/index.js) 解决方案: 控制台输入 np ...