一:打印流
/*System.out.println()重定向输出*/
/*public static void main(String[] args) {
System.out.println("MM");//直接输出到控制台
File file=new File("d:"+File.separator+"mm.txt");
try {
System.setOut(new PrintStream(new FileOutputStream(file)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("这些内容在文件中才能看到哦!");
}*/ /*System.err重定向 这个例子也提示我们可以使用这种方法保存错误信息*/
/*public static void main(String[] args) {
File file=new File("d:"+File.separator+"mm.txt");
System.out.println("这些在控制台输出!");
try {
System.setErr(new PrintStream(new FileOutputStream(file)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("这些内容在文件中才能看到哦!");
}*/ /*BufferedReader的小例子
注意: BufferedReader只能接受字符流的缓冲区,因为每一个中文需要占据两个字节,所以需要将System.in这个字节输入流变为字符输入流,采用:
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));*/
public static void main(String[] args) {
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
String str=null;
System.out.println("请输入内容:");
try {
str=bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("你输入的内容是:"+str);
}

  二:合并流:

package com.js.ai.modules.pointwall.testxfz;

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.OutputStream;
import java.io.SequenceInputStream;
/*将两个文本文件合并为另外一个文本文件*/
public class SequenceInputStreamDemo {
public static void main(String[] args) throws IOException {
File file1=new File("d:"+File.separator+"hello1.txt");
File file2=new File("d:"+File.separator+"hello2.txt");
File file3=new File("d:"+File.separator+"hello.txt");
InputStream inputStream1=new FileInputStream(file1);
InputStream inputStream2=new FileInputStream(file2);
OutputStream outputStream=new FileOutputStream(file3);
//合并流
SequenceInputStream sequenceInputStream=new SequenceInputStream(inputStream1, inputStream2);
int temp=0;
while((temp=sequenceInputStream.read())!=(-1)){
outputStream.write(temp);
}
inputStream1.close();
inputStream2.close();
outputStream.close();
sequenceInputStream.close();
}
}

  三:文件压缩:

/*压缩单个文件*/
public class ZipOutputStreamDemo1 {
public static void main(String[] args) throws IOException {
File file=new File("d:"+File.separator+"hello.txt");
File zipFile=new File("d:"+File.separator+"hello.zip");
InputStream inputStream=new FileInputStream(file);
ZipOutputStream zipOutputStream=new ZipOutputStream(new FileOutputStream(zipFile));
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
zipOutputStream.setComment("Hello");//设置注释
int temp=0;
while((temp=inputStream.read())!=(-1)){
zipOutputStream.write(temp);
}
inputStream.close();
zipOutputStream.close();
}
}

  

/*压缩多个文件*/
public class ZipOutputStreamDemo1 {
public static void main(String[] args) throws IOException {
File file=new File("d:"+File.separator+"temp");// 要被压缩的文件夹
File zipFile=new File("d:"+File.separator+"zipFile.zip");
InputStream inputStream=null;
ZipOutputStream zipOutputStream=new ZipOutputStream(new FileOutputStream(zipFile));
zipOutputStream.setComment("Hello");//设置注释
if(file.isDirectory()){
File[] files =file.listFiles();
for(int i=0;i<files.length;i++){
inputStream=new FileInputStream(files[i]);
zipOutputStream.putNextEntry(new ZipEntry(file.getName()+File.separator+files[i].getName()));
int temp=0;
while((temp=inputStream.read())!=(-1)){
zipOutputStream.write(temp);
}
inputStream.close();
}
}
zipOutputStream.close();
}
}

  四:文件解压

/*解压单个文件*/
public class ZipOutputStreamDemo1 {
public static void main(String[] args) throws IOException {
File file=new File("d:"+File.separator+"hello.zip");
File outFile=new File("d:"+File.separator+"outZipFile.txt");
ZipFile zipFile=new ZipFile(file);
ZipEntry entry=zipFile.getEntry("hello.txt");
InputStream inputStream=zipFile.getInputStream(entry);
OutputStream OutputStream=new FileOutputStream(outFile);
int temp=0;
while((temp=inputStream.read())!=(-1)){
OutputStream.write(temp);
}
inputStream.close();
OutputStream.close();
}
}

  

/*解压一个压缩文件中包含多个文件的情况*/
public class ZipOutputStreamDemo1 {
public static void main(String[] args) throws IOException {
File file=new File("d:"+File.separator+"zipFile.zip");
File outFile=null;
ZipFile zipFile=new ZipFile(file);
ZipInputStream zipInputStream=new ZipInputStream(new FileInputStream(file));
ZipEntry entry=null;
InputStream inputStream=null;
OutputStream OutputStream=null;
while((entry=zipInputStream.getNextEntry())!=null){
System.out.println("解压文件:"+entry.getName());
outFile=new File("d:"+File.separator+entry.getName());
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
if(!outFile.exists()){
outFile.createNewFile();
}
inputStream=zipFile.getInputStream(entry);
OutputStream=new FileOutputStream(outFile);
int temp=0;
while((temp=inputStream.read())!=(-1)){
OutputStream.write(temp);
}
inputStream.close();
OutputStream.close();
}
}
}

  五、回退流

public class PushBackInputStreamDemo {
public static void main(String[] args) throws Exception {
String str="hello,rollenholt";
PushbackInputStream push=null;
ByteArrayInputStream bat=null;
bat=new ByteArrayInputStream(str.getBytes());
push=new PushbackInputStream(bat);
int temp=0;
while((temp=push.read())!=(-1)){
if(temp==','){
push.unread(temp);
temp=push.read();
System.out.print("(回退:"+(char)temp+")");
}else {
System.out.print((char)temp);
}
}
}
}

  

java之IO整理(中)的更多相关文章

  1. java之IO整理(下)

    一:对象的序列化 对象序列化就是把一个对象变为二进制数据流的一种方法. 一个类要想被序列化,就行必须实现java.io.Serializable接口.虽然这个接口中没有任何方法,就如同之前的clone ...

  2. java之IO整理(上)

    /*//创建一个新文件 public static void main(String[] args) { File file=new File("D:\\hello.txt"); ...

  3. JAVA的IO学习

    IO 有具体的分类: 有具体的分类:1:根据处理的数类型不同:字节流和字符流.2:根据流向不同:输入流和输出流. =============(补充字节跟字符概念区分)================= ...

  4. Java之IO(九)其它字节流

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7063161.html 1.前言 之前的章节已经介绍了java的io包中所有成对(输入.输出对应)的字节流,本章介 ...

  5. Java之IO(八)PipedIutputStream和PipedOutputStream

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7056278.html 1.前言 本章介绍Java的IO体系中最后一对字节流--管道流.之前在字节数组流的时候就说 ...

  6. Java【IO流、字节流、字符流】

    1.内存是临时存储 Input输入(读取) output输出(输出) 流:数据(字符字节)1个字符=2个字节 1个字节=8个二进制位 输入:把硬盘中的数据读取到内存中 输出:把内存中的数据写入到硬盘中 ...

  7. java中的IO整理

    写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...

  8. 【转】 Java中的IO整理

    写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...

  9. 揭开Java IO流中的flush()的神秘面纱

    大家在使用Java IO流中OutputStream.PrintWriter --时,会经常用到它的flush()方法. 与在网络硬件中缓存一样,流还可以在软件中得到缓存,即直接在Java代码中缓存. ...

随机推荐

  1. c# 多线程调用窗体上的控件 示例

    private delegate void InvokeCallback(string msg); private void SetCountValue(string s) { if (this.fo ...

  2. ADO Recordset 对象链接

    http://baike.baidu.com/link?url=4Xdc46R8M5uj-BbOGaH761N5oDEYlGQJFeR2WbPwx1iQBusAUKU3qbWcHZCMmayatj9n ...

  3. 配置wampserver 虚拟主机

    1.修改http.conf 找到,#Include conf/extra/httpd-vhosts.conf,修改为(有的版本服务器,默认是开启的): 2.配置httpd-vhosts.conf文件, ...

  4. ubuntu 终端命令颜色的修改

    http://blog.chinaunix.net/uid-13954789-id-3137184.html http://blog.chinaunix.net/uid-26021340-id-348 ...

  5. Ubuntu网络代理问题

    问题描述 新开机的电脑,不开lantern就上不了网.很气. 解决过程 首先当然是寻求解决方案了.未果 然后就是妥协,每次先开一次lantern.(其实也不是很麻烦是吧,哎,是的是的) 今天早晨友人来 ...

  6. 如何修改localhost为自己指定的域名

    一般在windows电脑中localhost的配置一般都在电脑的C:\Windows\System32\drivers\etc这个路径下 ​进入后,打开hosts文件通过编辑器或者其他的软件打开,打开 ...

  7. 【剑指offer】滑动窗口的最大值,C++实现

    原创博文,转载请注明出处! # 题目 # 思路 利用C++中的双端队列保存有可能是滑动窗口最大值的下标,其中队首元素保存当前窗口最大值的下标.当滑动窗口改变时,更新队列.队列更新的规则:(1)新元素依 ...

  8. 【剑指offer】判断一个序列是否是二叉搜索树的后序遍历,C++实现

    原创文章,转载请注明出处! 本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 1.题目 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出N ...

  9. C#与sqlserver开发问题

    最近不停的在考虑C#读取数据性能问题第一种使用ado拼接sql连接数据库第二种使用ado调用存储过程第三种使用entityframework加linq第四种使用反射IList<T> 1.从 ...

  10. 开启opcache提高性能

    在开启opcache之前,我们先介绍一下编译与解释: 编译器是把源程序的每一条语句都编译成机器语言,并保存成二进制文件,这样运行时计算机可以直接以机器语言来运行此程序,速度很快:而解释器则是只在执行程 ...