java之IO整理(中)
Posted Ipeter
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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内存流:java.io.ByteArrayInputStreamjava.io.ByteArrayOutputStreamjava.io.CharArrayReaderjava.io(代码片段
java缓冲字符字节输入输出流:java.io.BufferedReaderjava.io.BufferedWriterjava.io.BufferedInputStreamjava.io.(代码片段