1.字节流  InputStream(抽象类)

 package ioStudy;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; /*
1.创建源
2.选择流
3.操作
4.释放资源
*/
public class IOstudy2 {
public static void main(String[] args) {
File file = new File("test.txt"); //创建源 InputStream is = null; try {
is = new FileInputStream(file); // 选择流
int temp;
while ((temp = is.read()) != -1) { //一次读一个字节 如果包含中文可能会出现乱码 考虑使用字符流
System.out.print((char) temp); //操作
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(is!=null) {
try {
is.close(); //释放资源
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
 package ioStudy;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; /*
1.创建源
2.选择流
3.操作
4.释放资源
*/
public class IOstudy3 {
public static void main(String[] args) {
File file = new File("test.txt");
InputStream is = null;
try {
is = new FileInputStream(file);
byte[] buffer = new byte[5];
int len;
while ((len = is.read(buffer)) != -1) { //一次读取多个
String s = new String(buffer,0,len);
System.out.print(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(is!=null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

2.OutputStream

 package ioStudy;

 import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; /*
1.创建源
2.选择流
3.操作
4.释放资源
*/
public class IOstudy4 {
public static void main(String[] args) {
File file = new File("output.txt"); // 不存在会自动创建
OutputStream os = null; try {
os = new FileOutputStream(file,true); //开启向后追加 不然每次都删掉原来的 再写 默认的是false
String temp = "hello world!\r\n";
byte[] b = temp.getBytes();
os.write(b, 0, b.length);
os.flush(); //刷新内存
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

3.实现文件的copy

 package ioStudy;

 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; /*
1.创建源
2.选择流
3.操作
4.释放资源
*/
public class Copy {
public static void main(String[] args) {
copy("test.txt","testcopy.txt");
} public static void copy(String source, String destination) {
File src = new File(source);
File dest = new File(destination);
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(src);
os = new FileOutputStream(dest); byte[] buffer = new byte[1034];
int len;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
os.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 流关闭的原则 先打开的后关闭
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

最新文章

  1. MySQL的简单使用和JDBC示例
  2. 黑马程序员+Winform基础(下)
  3. Javascript之运动框架2
  4. Effective C++条款01: 视C++为一个语言联邦
  5. Linux 混杂设备、外部中断和输入子系统
  6. android 内部存储相关知识点: getfilestreampath getDir 子文件夹
  7. SQL后台分页三种方案和分析
  8. Swift 2.0 UIAlertView 和 UIActionSheet 的使用
  9. webstorm安装与本地激活
  10. TCP:传输控制协议
  11. celery学习笔记2
  12. Numpy库的学习(五)
  13. SpringBoot之基础
  14. DOM对象与jquery对象相互转换
  15. CentOS7编译安装mysql-5.6.43
  16. 51Nod1123 X^A Mod B 数论 中国剩余定理 原根 BSGS
  17. 进程 day36
  18. Mac 安装 Ruby, Rails 运行环境
  19. unmappable character for US-ASCII
  20. C#学习笔记(20)——使用IComparer(自己写的)

热门文章

  1. COCOS学习笔记--即时动作ActionInstant
  2. Flex 页面启动事件
  3. Android实现多个倒计时优化与源代码分析
  4. 【Dairy】2016.10.17-1 OIer最悲剧的事情
  5. ORACLE分区表发挥性能
  6. [Codeforces Round511C] Enlarge GCD
  7. bzoj2419
  8. 如何给自己的博客上添加个flash宠物插件
  9. [入门帮助] Kafka入门经典教程
  10. 一个单例(Singleton),并说明单例的目的和好处