1.SequenceInputStream序列流:能将其他输入流的串联
    用处:读完第一个再去读第二个输入流
    用法:构造方法:SequenceInputStream(InputStream s1,InputStream s2):串联2个InputStream还可以,多个就很麻烦;
    SequenceInputStream(Enumeration<? extends InputStream> e):Enumation对象需要通过Vector中获取,Vector是一个集合不过因为考虑线程安全效率比较低,他返回的的集合对象就是最初的Enumeration:
    例子:将一个歌分成4份;再将4份合拼为1份

 /**
* 将一个文件切割为4份再将4份合拼为1份
* @author Administrator
*
*/
public class sequenceInputStream { public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//cutFile("C:/1.mp3","C:/mp3");
mergeFile("C:/mp3","C:/hepin.mp3");
}
/**
* 将文件以2M为一个单位分成多份
* @param pathFrom 被分文件位置
* @param pathTo 分成文件存放文件夹位置
* @throws IOException IO异常
*/
public static void cutFile(String pathFrom,String pathTo) throws IOException{
File f=new File(pathFrom);
FileInputStream fs=new FileInputStream(f);
int length=0;
byte[] buffer=new byte[1024*1024*2];
for(int i = 0 ; (length = fs.read(buffer))!=-1 ; i++){
FileOutputStream fos=new FileOutputStream(new File(pathTo,"part"+i+".mp3"));
fos.write(buffer, 0, length);
fos.close();
}
fs.close();
}
/**
* 将一个文件夹中的多个文件合拼为一个文件
* @param pathFrom 存放多个文件的文件夹位置
* @param pathTo 合拼文件存放位置
* @throws IOException
*/
public static void mergeFile(String pathFrom,String pathTo) throws IOException{
File f=new File(pathFrom);//找到目标文件夹将其中的所有子文件都放到Vector中变成Enumation集合中去
File[] files=f.listFiles();
Vector<FileInputStream> vector=new Vector<FileInputStream>();
for(File file:files){
FileInputStream fs1=new FileInputStream(file);
vector.add(fs1);
}
Enumeration<FileInputStream> e=vector.elements();
SequenceInputStream sis=new SequenceInputStream(e); FileOutputStream fos=new FileOutputStream(pathTo);
int length=0;
byte[] buffer=new byte[1024];
while((length=sis.read(buffer))!=-1){
fos.write(buffer,0,length);
}
fos.close();
sis.close();
}
}

2.ObjectInputStream,ObjectionStream对象输入输出流
当我们想要将一个对象存到硬盘中时我们就需要这个东西将对象序列化和反序列化
  (一)用法跟其他的流都是一样的
  (二)注意:1.对象要想写到文件上,对象所属类必须实现Serialization接口;Serialization接口没有任何的方法,只是一个标识接口;
        2.对象反序列化时并不会调用构造方法
        3.serializationUID用于记录类的版本信息的,它由一个类的类名,成员,包名,工程名算出来的数字
        4.使用ObjectInputStream反序列化时,会先读取文件中的serializationUID,然后与本地的比较,不一样就反序列化失败
        5.如果序列化反序列化可能会修改类成员,那么最好一开始就给类指定serializationUID,之后jvm就不会再计算这个值了
        6.如果对象的一个成员不想序列化到硬盘上,可以使用关键字transient修饰。
        7.一个类维护了另一个类,那么另一个类也需要实现Serialization
  (三)例子:

class Address implements Serializable{

    String country; 

    String city;

    public Address(String country,String city){
this.country = country;
this.city = city;
} } class User implements Serializable{ private static final long serialVersionUID = 1L; 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 Demo3 { 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(); //创建对象肯定要依赖对象所属 的class文件。
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(); }
}

3.Properties配置文件类
  (一)用于生产和读取配置文件的
  (二)注意:1.配置文件中使用中文在用store方法生成配置文件时只能用字符流,如果用字节流生成配置文件默认用的是iso885ma才可以
        2.如果配置文件内容发生变化,需要重新生成配置文件
  (三)例子:

基本用法在里面:

public class Demo4 {

    public static void main(String[] args) throws IOException {
creatProperties(); // readProperties();
} //读取配置文件爱你的信息
public static void readProperties() throws IOException{
//创建Properties对象
Properties properties = new Properties();
//加载配置文件信息到Properties中
properties.load(new FileReader("F:\\persons.properties"));
//遍历
Set<Entry<Object, Object>> entrys = properties.entrySet();
for(Entry<Object, Object> entry :entrys){
System.out.println("键:"+ entry.getKey() +" 值:"+ entry.getValue());
}
//修改狗娃的密码
//把修改后的Properties再生成一个配置文件
properties.setProperty("狗娃", "007");
properties.store(new FileWriter("F:\\persons.properties"), "hehe"); } //保存配置文件文件的信息。
public static void creatProperties() throws IOException{
//创建Properties
Properties properties = new Properties();
properties.setProperty("狗娃", "123");
properties.setProperty("狗剩","234");
properties.setProperty("铁蛋","345");
// 遍历Properties
/*Set<Entry<Object, Object>> entrys = properties.entrySet();
for(Entry<Object, Object> entry :entrys){
System.out.println("键:"+ entry.getKey() +" 值:"+ entry.getValue());
}*/ //使用Properties生产配置文件。
//properties.store(new FileOutputStream("F:\\persons.properties"), "haha"); //第一个参数是一个输出流对象,第二参数是使用一个字符串描述这个配置文件的信息。
properties.store(new FileWriter("F:\\persons.properties"), "hehe");
} }

使用properties实现本软件只能 运行三次,超过了三次之后就提示购买正版,退jvm.

public class Demo5 {

    public static void main(String[] args) throws IOException {
File file = new File("F:\\count.properties");
if(!file.exists()){
//如果配置文件不存在,则创建该配置文件
file.createNewFile();
} //创建Properties对象。
Properties properties = new Properties();
//把配置文件的信息加载到properties中
properties.load(new FileInputStream(file));
FileOutputStream fileOutputStream = new FileOutputStream(file); int count = 0; //定义该变量是用于保存软件的运行次数的。
//读取配置文件的运行次数
String value = properties.getProperty("count");
if(value!=null){
count = Integer.parseInt(value);
} //判断使用的次数是否已经达到了三次,
if(count==3){
System.out.println("你已经超出了试用次数,请购买正版软件!!");
System.exit(0);
} count++;
System.out.println("你已经使用了本软件第"+count+"次");
properties.setProperty("count",count+"");
//使用Properties生成一个配置文件
properties.store(fileOutputStream,"runtime"); } }

4.printStream打印流
  (一)可以打印任意类型的数据而且打印之前会先变成字符串再打印
  (二)设置标准默认输出流输入流
      System.in;System.out都是可以设置流对象的一般指向printStream流对象
  (三)例子:

class Animal{

    String name;

    String color;

    public Animal(String name,String color){
this.name = name;
this.color = color;
} @Override
public String toString() {
return "名字:"+this.name+ " 颜色:"+ this.color;
} } public class Demo6 { public static void main(String[] args) throws IOException {
/*FileOutputStream fileOutputStream = new FileOutputStream("F:\\a.txt");
fileOutputStream.write("97".getBytes());
fileOutputStream.close();*/ //打印流可以打印任何类型的数据,而且打印数据之前都会先把数据转换成字符串再进行打印。
File file = new File("F:\\a.txt");
//创建一个打印流
PrintStream printStream = new PrintStream(file);
/*
printStream.println(97);
printStream.println(3.14);
printStream.println('a');
printStream.println(true);
Animal a = new Animal("老鼠", "黑色");
printStream.println(a); //默认标准的输出流就是向控制台输出的,
System.setOut(printStream); //重新设置了标准的输出流对象
System.out.println("哈哈,猜猜我在哪里!!");
*/ //收集异常的日志信息。
File logFile = new File("F:\\2015年1月8日.log");
PrintStream logPrintStream = new PrintStream( new FileOutputStream(logFile,true) );
try{
int c = 4/0;
System.out.println("c="+c);
int[] arr = null;
System.out.println(arr.length); }catch(Exception e){
e.printStackTrace(logPrintStream); } } }

5.编码解码
  byte[] buf = str.getBytes(); //使用gbk进行编码
  str = new String(buf,"iso8859-1");//解码
6. 转换流:
  (一)分类:
    输入字节流的转换流:InputStreamReader 是字节流通向字符流的桥
    输出字节流的转换流:OutputStreamWriter 可以把输出字节流转换成输出字符流 。
  (二)注意:
    转换时可以设置编码表读写文件
  (三)用法:
    //创建字节流的转换流并且指定码表进行读取
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");
7.递归
   例子:
    a.列出一个文件夹的子孙文件与目录。
    b.列出指定目录中所有的子孙文件与子孙目录名,要求名称前面要有相应数量的空格:
      第一级前面有0个,第二级前面有1个,第三级前面有2个...,以此类推。

    c.列出指定目录中所有的子孙文件与子孙目录名,要求要是树状结构,效果如下所示:
|--src
| |--cn
| | |--itcast
| | | |--a_helloworld
| | | | |--HelloWorld.java
| | | |--b_for
| | | | |--ForTest.java
| | | |--c_api
| | | | |--Student.java
|--bin
| |--cn
| | |--itcast
| | | |--i_exception
| | | | |--ExceptionTest.class
| | | |--h_linecount
| | | | |--LineCounter3.class
| | | | |--LineCounter2.class
| | | | |--LineCounter.class
|--lib
| |--commons-io.jar

   d: 删除一个非空的文件夹。
*/

public class Demo10 {

    public static void main(String[] args) {
/* File dir = new File("F:\\1208project\\day22");
listFiles3(dir,"|--");*/ File dir = new File("F:\\aa");
deleteDir(dir);
} //删除了一个非空的目录
public static void deleteDir(File dir){ // bb
File[] files = dir.listFiles(); //列出了所有的子文件
for(File file : files){
if(file.isFile()){
file.delete();
}else if(file.isDirectory()){
deleteDir(file);
}
}
dir.delete();
} public static void listFiles3(File dir,String space){ //space 存储的是空格
File[] files = dir.listFiles(); //列出所有 的子文件
for(File file : files){
if(file.isFile()){
System.out.println(space+file.getName());
}else if(file.isDirectory()){
System.out.println(space+file.getName());
listFiles3(file,"| "+space);
} } } //列出一个文件夹的子孙文件与目录。
public static void listFiles2(File dir,String space){ //space 存储的是空格
File[] files = dir.listFiles(); //列出所有 的子文件
for(File file : files){
if(file.isFile()){
System.out.println(space+file.getName());
}else if(file.isDirectory()){
System.out.println(space+file.getName());
listFiles2(file," "+space);
} } } //列出一个文件夹的子孙文件与目录。
public static void listFiles1(File dir){
File[] files = dir.listFiles(); //列出所有 的子文件
for(File file : files){
if(file.isFile()){
System.out.println("文件名:"+file.getName());
}else if(file.isDirectory()){
System.out.println("文件夹:"+file.getName());
listFiles1(file);
} } } }

java入门了解12的更多相关文章

  1. [Java入门笔记] 面向对象编程基础(二):方法详解

    什么是方法? 简介 在上一篇的blog中,我们知道了方法是类中的一个组成部分,是类或对象的行为特征的抽象. 无论是从语法和功能上来看,方法都有点类似与函数.但是,方法与传统的函数还是有着不同之处: 在 ...

  2. Java入门记(五):容器关系的梳理(下)——Map

    注意:阅读本文及相关源码时,需要数据结构相关知识,包括:哈希表.链表.红黑树. Map是将键(key)映射到值(value)的对象.不同的映射不能包含相同的键:每个键最多只能映射到一个值.下图是常见M ...

  3. Android初涉及之Android Studio&JAVA入门--二月不能不写东西

    是的,我还没有放弃写博客. 是的,我也没有放弃PHP的学习. 是的,我要开始学学最TM火的Android开发了. 你呢 1.Android Studio 一.概况 安装和配置什么的就不具体说了,网上一 ...

  4. Java入门教程总目录

    Java入门教程总目录 持续更新中... 1.Java常识汇总 2.Java框架对比 3.Java技术路线 4.Java编码规范 5.Java环境变量配置 6.枚举 7.操作符 12.定时任务

  5. Java入门的程序汇总

    Java入门的基础程序,虽然很简单,也要多练习,下面有重点的总结一下 1.HelloWorld这个不说了 2.常量与变量部分 基本数据类型使用 public class Javashujuleixin ...

  6. JAVA入门第一季(mooc-笔记)

    笔记相关信息 /** * @subject <学习与创业>作业1 * @author 信管1142班 201411671210 赖俊杰 * @className <JAVA入门第一季 ...

  7. JAVA入门--目录

    在此记录自己的JAVA入门笔记,备忘 JAVA入门[1]--安装JDK JAVA入门[2]-安装Maven JAVA入门[3]—Spring依赖注入 JAVA入门[4]-IntelliJ IDEA配置 ...

  8. Java入门1

    一.eclipse的简单使用 1.新建项目 在package explorer的空白处点击右键,新建一个项目(new->Java Project)或者点击菜单栏的File->JavaPro ...

  9. Java入门(三):变量和运算符

    上次谈到了Java的基本数据类型,今天接着聊Java的变量.运算符. 一.变量 1.变量的分类 变量分为成员变量.局部变量和常量,其中成员变量又分为实例变量.类变量. 2.变量的定义 语法:变量类型( ...

随机推荐

  1. 使用React的static方法实现同构以及同构的常见问题

    代码地址请在github查看,假设有新内容.我会定时更新.也欢迎您star,issue,共同进步 1.我们服务端渲染数据从何而来 1.1 怎样写出同构的组件 服务端生成HTML结构有时候并不完好.有时 ...

  2. SAP abab 设定动态标题 SET TITLEBAR XXXX WITH XXXX

    供ALV输出EXCEL时调用. 动态地改变TITLEBAR上的文字描写叙述,详细使用方法例如以下: 1.双击TITLEBAR.进入TITLEBAR的编辑框,在这里写入&1,相当于宏定义的宏的參 ...

  3. 从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named

    这一章节我们来讨论一下基于java的标准注解装配标签@Inject的限定器@Named. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_s ...

  4. CentOS7 安装 Node.js

    1.首先安装node.js 的版本管理工具 NVM,执行以下命令: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/ ...

  5. android Notification 的使用(锁定通知栏)

    近期一直在研究 android .并一边研究一边做应用.当中遇到了把程序通知常驻在 Notification 栏,而且不能被 clear 掉(就像android QQ一样)的问题.经过研究实现了其功能 ...

  6. A/B测试与灰度发布

    1.A/B测试与灰度发布的理论 产品是多维度的,设计体验.交互体验.系统质量.运营支持等等, 测试的目的是为了系统最终的交付,一套各方面都足够好的系统,而不是文档上定义的系统,系统是需要不断进化的. ...

  7. 【BZOJ4009】[HNOI2015]接水果 DFS序+整体二分+扫描线+树状数组

    [BZOJ4009][HNOI2015]接水果 Description 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果.由于她已经DT FC 了The big black, ...

  8. js跨浏览器复制: ZeroClipboard

    实例结构: demo.html <script type="text/javascript" src='http://code.jquery.com/jquery.js'&g ...

  9. Django之stark组件2

    action批量处理功能 用户可以自定义批量处理功能,但是默认的有批量删除功能. ***思路*** 1,定义一个列表用来装action的类名并extend用户自定义列表 2.循环该列表获取到函数名(用 ...

  10. Redis通过PUBLISH / SUBSCRIBE 等命令实现了订阅与发布模式

    # 切换目录 [root@localhost /]# cd /opt/redis-4.0.10 # 启动客户端 -p 指定端口 [root@localhost ~]# redis-cli -p 638 ...