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();
}
}
}
}
}

java_IO_2的更多相关文章

  1. Android学习笔记之DocumentBuilder的使用....

    PS:当你的才华还撑不起你的野心时,那你需要静下心来学习..... 学习内容: 1.从服务器上获取XML文档... 2.解析XML文档中的内容...   XML文件想必大家都非常的熟悉,可扩展的标记语 ...

随机推荐

  1. HDU3367 Pseudoforest 【并查集】+【贪心】

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  2. SSL和SSH的差别

    有人说,SSH通常是用来提供安全的登录用的.SSL仅仅是一个在协议层中增加的一层用来提供安全.    SSH工作在TCP之上,能够在启动一个SSH应用后.在其通道里执行其他协议的应用.如邮件.    ...

  3. 《炉石传说》架构设计赏析(4):Asset管理

    欢迎转载,请注明作者[燕良@游戏开发]及原文地址:http://blog.csdn.net/neil3d/article/details/39580197 另外.欢迎大家来我的QQ群交流各种游戏引擎相 ...

  4. 嵌入式开发之cmos---前端采集aptina cmos

    http://wenku.baidu.com/link?url=NFl5ye1-o5GNMVGmxBmot1v1HQBOZRA2xo7__sgxxLnpHqodpqtfIW_pf4QNGRX4u8n8 ...

  5. grep 并列查询 效率 且 或

    find / | grep -v python | grep -v xl_ | grep -v xiaole |grep redis [root@hadoop3 ~]# find / | grep - ...

  6. HTML DOM Table 对象

    Table 对象 Table 对象代表一个 HTML 表格. 在 HTML 文档中 <table> 标签每出现一次,一个 Table 对象就会被创建. Table 对象集合 集合 描述 c ...

  7. [翻译]NUnit---Action Attributes(八)

    Attributes NUnit 1.0使用传统的基于继承和命名约定来识别测试.从2.0开始NUnit使用自定义特性来实现. 因为NUnit的test fixtures不是从框架类库继承,所以开发人员 ...

  8. bzoj 1370 Gang团伙

    题目大意: 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,满足 1. 我朋友的朋友是我的朋友 2. 我敌人的敌人是我的朋友 所有是朋友的人组成一个团伙 告诉你关于这n个人的m条信息,即某两个 ...

  9. 4.7.3 Canonical LR(1) Parsing Tables

    4.7.3 Canonical LR(1) Parsing Tables We now give the rules for constructing the LR(1) ACTION and GOT ...

  10. 洛谷P1365 WJMZBMR打osu! / Easy——期望DP

    题目:https://www.luogu.org/problemnew/show/P1365 平方和怎样递推? 其实就是 (x+1)^2 = x^2 + 2*x + 1: 所以我们要关注这里的 x — ...