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. SQL Server 海量数据查询代码优化以及建议

    1.应尽量避免在  where  子句中对字段进行   null  值推断,否则将导致引擎放弃使用索引而进  行全表扫描,如:     select id from t where num is nu ...

  2. ZOJ问题(2010浙江大学研究生复试上机题目[找规律] hdoj 3788)

    ZOJ问题 pid=3788">点击打开链接 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. 2013级C++第12周(春)项目——成员的訪问属性、多重继承

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 第一部分 程序阅读 1.阅读程序.分析类中成员 ...

  4. 嵌入式开发之函数解析---ip地址2进制转字符inet_ntoa 调用中只生效一次

    inet_addr()   简述:将一个点间隔地址转换成一个in_addr. #include <winsock.h> unsigned long PASCAL FAR inet_addr ...

  5. HTTP要点概述:六,HTTP报文

    一,HTTP报文: 用于HTTP交互的信息称为HTTP报文.请求端(客户端)的HTTP报文叫做请求报文,响应端(服务器)的叫做响应报文.HTTP报文本身是由多行(用CR+LF换行)数据构成的字符串文本 ...

  6. java8新特性-方法引用

    方法引用:若 Lambda 体中的功能,已经有方法提供了实现,可以使用方法引用 (可以将方法引用理解为 Lambda 表达式的另外一种表现形式) 1. 对象的引用 :: 实例方法名2. 类名 :: 静 ...

  7. 利用JFreeChart生成折线图 (4) (转自 JSP开发技术大全)

    利用JFreeChart生成折线图 (4) (转自 JSP开发技术大全) 14.4 利用JFreeChart生成折线图 通过JFreeChart插件,既可以生成普通效果的折线图,也可以生成3D效果的折 ...

  8. python datatime日期和时间值模块

    datetime.time():是一个时间类,这个类接受4个参数,分别代表时,分,秒,毫秒.参数的默认值是为0 #!/usr/bin/env python #coding:utf8 import da ...

  9. linux下的C语言开发(静态库/动态库)

    动态链接库不是Linux独有的特性,在windows下面也存在这样的特性.一般来说,windows下面的动态连接库是以*.dll作为结尾的,而linux下面的动态连接库是以*.so结尾的.和静态链接库 ...

  10. [转] 本地项目上传github (新项目 / 旧项目)

    前置:安装Git Bash,在github上新建仓库repository 1.右键点击项目所在文件夹,运行: git bash here.在git bash窗口运行命令 git init 把这个目录变 ...