java之IO整理(中)
一:打印流
/*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整理(中)的更多相关文章
- java之IO整理(下)
一:对象的序列化 对象序列化就是把一个对象变为二进制数据流的一种方法. 一个类要想被序列化,就行必须实现java.io.Serializable接口.虽然这个接口中没有任何方法,就如同之前的clone ...
- java之IO整理(上)
/*//创建一个新文件 public static void main(String[] args) { File file=new File("D:\\hello.txt"); ...
- JAVA的IO学习
IO 有具体的分类: 有具体的分类:1:根据处理的数类型不同:字节流和字符流.2:根据流向不同:输入流和输出流. =============(补充字节跟字符概念区分)================= ...
- Java之IO(九)其它字节流
转载请注明源出处:http://www.cnblogs.com/lighten/p/7063161.html 1.前言 之前的章节已经介绍了java的io包中所有成对(输入.输出对应)的字节流,本章介 ...
- Java之IO(八)PipedIutputStream和PipedOutputStream
转载请注明源出处:http://www.cnblogs.com/lighten/p/7056278.html 1.前言 本章介绍Java的IO体系中最后一对字节流--管道流.之前在字节数组流的时候就说 ...
- Java【IO流、字节流、字符流】
1.内存是临时存储 Input输入(读取) output输出(输出) 流:数据(字符字节)1个字符=2个字节 1个字节=8个二进制位 输入:把硬盘中的数据读取到内存中 输出:把内存中的数据写入到硬盘中 ...
- java中的IO整理
写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...
- 【转】 Java中的IO整理
写在前面:本文章基本覆盖了java IO的全部内容,java新IO没有涉及,因为我想和这个分开,以突出那个的重要性,新IO哪一篇文章还没有开始写,估计很快就能和大家见面.照旧,文章依旧以例子为主,因为 ...
- 揭开Java IO流中的flush()的神秘面纱
大家在使用Java IO流中OutputStream.PrintWriter --时,会经常用到它的flush()方法. 与在网络硬件中缓存一样,流还可以在软件中得到缓存,即直接在Java代码中缓存. ...
随机推荐
- 源码编译tmux
(1)clone 源代码仓库: $ git clone https://github.com/tmux/tmux.git (2) 编译之前先安装libevent,去官网下载tar包: http://l ...
- ionic安装插件常用命令
常见插件查找网站: http://ngcordova.com/docs/plugins http://cordova.apache.org/plugins/ $ ionic plugin list / ...
- 迭代器、foreach循环、泛型集合
集合的迭代 语法:Iterator<Object> it=集合.iterator(); while(it.hasNext()){ Object obj=it.next(); } is.ha ...
- 从JDK源码角度看Boolean
Java的Boolean类主要作用就是对基本类型boolean进行封装,提供了一些处理boolean类型的方法,比如String类型和boolean类型的转换. 主要实现源码如下: public fi ...
- 【剑指offer】数组中出现次数超过数组长度一半的数字,C++实现
原创博文,转载请注明出处! # 题目 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过 ...
- 【集成学习】sklearn中xgboost模块中plot_importance函数(绘图--特征重要性)
直接上代码,简单 # -*- coding: utf-8 -*- """ ################################################ ...
- html 刷新 按钮 代码
<input type=button value=刷新 onclick="history.go(0)"> <input type=button value=刷新 ...
- C++中atof函数的实现和atoi的实现
在C++中有两个系统函数可以实现字符串转浮点型和字符串转整形,下面实现一下这两个函数. #include <iostream> #include <string> using ...
- 让svn具有分布式的功能。
最近开发遇到了个难事.公司的svn库不能随便提交,必须要经过验证.但是平时修改太多,如果不提交到svn说不定前面被删掉的代码后面又需要了.svn自带的relocate和switch都不能达到要求.找遍 ...
- Python IDE in Sublime
(最近换了电脑,然后忘了把 ST 的配置搬过来,所以重新折腾了一遍 Sublime 中的 Python 环境配置) 以下插件均通过 Package Control 安装. SublimeREPL 快捷 ...