标签:book pytho io流 null row software 开始 cat block
本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识
InputStream包含如下三个方法:
FileInputStream继承InputStream,使用FileInputStream读取文件内容
代码1-1
import java.io.FileInputStream; import java.io.IOException; public class FileInputStreamTest { public static void main(String[] args) { if (args == null || args.length == 0) { throw new RuntimeException("请输入路径"); } FileInputStream fis = null; try { byte[] bbuf = new byte[1024]; int hasRead = 0; fis = new FileInputStream(args[0]); while ((hasRead = fis.read(bbuf)) > 0) { System.out.print(new String(bbuf, 0, hasRead)); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
代码1-1运行结果:
root@lejian:/home/software/.io# cat text Hello Spring Hello Hibernate Hello MyBatis root@lejian:/home/software/.io# java FileInputStreamTest text Hello Spring Hello Hibernate Hello MyBatis
Reader包含如下三个方法:
FileReader的父类继承自Reader,使用FileReader读取文件
代码1-2
import java.io.FileReader; import java.io.IOException; public class FileReaderTest { public static void main(String[] args) { if (args == null || args.length == 0) { throw new RuntimeException("请输入路径"); } FileReader fr = null; try { fr = new FileReader(args[0]); char[] cbuf = new char[32]; int hasRead = 0; while ((hasRead = fr.read(cbuf)) > 0) { System.out.print(new String(cbuf, 0, hasRead)); } } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { try { if (fr != null) { fr.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
代码1-2运行结果:
root@lejian:/home/software/.io# cat text Java编程思想 算法 编译原理 root@lejian:/home/software/.io# java FileReaderTest text Java编程思想 算法 编译原理
代码1-3
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStreamTest { public static void main(String[] args) { if (args == null || args.length < 2) { throw new RuntimeException("请输入两个路径"); } FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(args[0]); fos = new FileOutputStream(args[1]); byte[] bbuf = new byte[32]; int hasRead = 0; while ((hasRead = fis.read(bbuf)) > 0) { fos.write(bbuf, 0, hasRead); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
OutputStream和Writer:
Writer里还包含如下两个方法:
使用FileInputStream来执行输入,FileOutputStream来执行输出
代码1-3运行结果:
root@lejian:/home/software/.io# cat input Java编程思想 算法 编译原理 root@lejian:/home/software/.io# cat output root@lejian:/home/software/.io# java FileOutputStreamTest input output root@lejian:/home/software/.io# cat output Java编程思想 算法 编译原理
使用FileWriter来执行输出
代码1-4
import java.io.FileWriter; import java.io.IOException; public class FileWriterTest { public static void main(String[] args) { if (args == null || args.length == 0) { throw new RuntimeException("请输入路径"); } FileWriter fw = null; try { fw = new FileWriter(args[0]); fw.write("数据结构与算法\n"); fw.write("Python基础教程\n"); fw.write("C和指针\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (fw != null) { fw.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
代码1-4运行结果:
root@lejian:/home/software/.io# cat book root@lejian:/home/software/.io# java FileWriterTest book root@lejian:/home/software/.io# cat book 数据结构与算法 Python基础教程 C和指针
Java7之后,很多IO资源类都实现了AutoCloseable接口,只要在try语句中声明的IO流都可以自动关闭,但有一些IO流像FileWriter这样的输出流,如果IO流不显示的关闭,输出流缓冲区的数据无法flush到实际的物理节点,因为执行IO流的close()方法前,会先执行flush()方法
FileInputStream、FileReader、FileInputStream、FileWriter使用小结
标签:book pytho io流 null row software 开始 cat block
原文地址:http://www.cnblogs.com/baoliyan/p/6220092.html