一:打印流
/*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. pcm ulaw alaw转换

    static byte ALawCompressTable[] = { 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5 ...

  2. Unity喷墨效果Shader实现

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  3. No form of payment has been added yet.

    You may select a form of payment after your account balance reaches $10.00. Learn more 显然是说达到10美元以后才 ...

  4. EP-N8530S USB WIFI 驱动移植

    /*********************************************************************** * EP-N8530S USB WIFI 驱动移植 * ...

  5. learn go defer

    package main // 参考文档: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.4.md im ...

  6. 【剑指offer】数组中的逆序对。C++实现

    原创文章,转载请注明出处! 博客文章索引地址 博客文章中代码的github地址 # 题目 # 思路 基于归并排序的思想统计逆序对:先把数组分割成子数组,再子数组合并的过程中统计逆序对的数目.统计逆序对 ...

  7. pandas.read_csv()参数(转载)

    文章转载地址 pandas.read_csv参数整理 读取CSV(逗号分割)文件到DataFrame 也支持文件的部分导入和选择迭代 更多帮助参见:http://pandas.pydata.org/p ...

  8. php 配置上传大文件

    打开php.ini,首先找到file_uploads = on ;是否允许通过HTTP上传文件的开关.默认为ON即是开upload_tmp_dir ;文件上传至服务器上存储临时文件的地方,如果没指定就 ...

  9. 线性回归 Linear regression(3) 线性回归的概率解释

    这篇博客从一种方式推导了Linear regression 线性回归的概率解释,内容来自Standford公开课machine learning中Andrew老师的讲解. 线性回归的概率解释 在Lin ...

  10. 每天一个linux命令:【转载】rm命令

    今天学习一下linux中删除文件和目录的命令: rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除.对于链接文件,只是删除 ...