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. SUSE Linux源代码编译安装MySQL 5.6

    这篇文章主要介绍了SUSE Linux下源代码编译方式安装MySQL 5.6过程分享,本文使用SUSE Linux Enterprise Server 10 SP3 (x86_64)系统,须要的朋友能 ...

  2. 关于微软网站维护培训时遇到的issues:​​​What is WEDCS?

    WEDCS (pronounced “wed-ex”), the Web Events Data Collection Service, is a system for collecting, com ...

  3. Python常用数据处理函数

    1.基本统计特征函数 方法名 函数功能 所属库 使用格式 sum() 计算数据样本综合(按列计算) Pandas D.sum() mean() 计算数据样本算数平均数 Pandas D.mean() ...

  4. HDU5465/BestCoder Round #56 (div.2) 二维树状数组

    Clarke and puzzle 问题描述 克拉克是一名人格分裂患者.某一天,有两个克拉克(aa和bb)在玩一个方格游戏. 这个方格是一个n*mn∗m的矩阵,每个格子里有一个数c_{i, j}c​i ...

  5. 2.eclipse 插件安装烦死人(2)

    安装插件的实际结果是:(烦死人),要不是很多插件找不到,要不就是版本不对,要不就是下载了装上没有效果,要不就是在线安装(速度爆慢),好不容易等到结果了,结果是些错…… 最后我的eclipse 3.5. ...

  6. Message: SyntaxError: unterminated string literal

    #Message: SyntaxError: unterminated string literalmytxt = words.replace('\n','').replace('\r','') js ...

  7. Oracle 11g密码过期问题及解决方案

    问题: 在自用的一个系统里,连接的是本地自建的一个数据库.用sqldeveloper登录数据库.提示如下图: 提示:密码过期 解决方案: 密码过期一般存在两种可能: 由于Oracle中默认在defau ...

  8. MySQL-day1数据库的安装与介绍

    一.mysql的安装步骤 以5.7.20版本为例: 第1步: 第2步: 第3步: 第4步: 第5步: 第6步: 第7步: 第8步: 第9步: 第10步: 第11步: 第12步: 第13步: 第14步: ...

  9. RDA Kconfig介绍 ***

    安装显示库: sudo apt-get install libncurses5-dev libncursesw5-dev 执行相关文件: 1.radisson.Kconfig 2.gen_radiss ...

  10. mybaties中,模糊查询的几种写法

    模糊查询: 工作中用到,写三种用法吧,第四种为大小写匹配查询 1. sql中字符串拼接 SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('% ...