黑马程序员-IO流

java io 里面的核心是讲诉java 的文件操作。

IO input/output ,进与出。

流 流水的流。 流进与流出。

java IO流分成两大类,字节流(InputStreamOutputStream)和字符流(Reader,Writer)。

这两大类里面都有两个重要的分类,输入流和输出流。如下图

*字节流:一个一个字节的读写操作

读取:

public class Test {
public static void main(String[] args) {
//文件
File f = new File("E:/x.txt");
//文件读取流管道
FileInputStream fis = null;
int i = 0;
String str = new String() ;
//字节容器
byte[] b = new byte[1024];
int y = 0;
try {
//将管道接进文件
fis = new FileInputStream(f);
//读取每一个字节
while((i = fis.read())!=-1) {
b[y++] = (byte)i;
}
//字节数组转义成字符串,再进行转码
str = new String(b,"GBK");
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//不为空就关闭流,不关闭会占用内存空间
if(fis !=null)
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


写入:

public class Test {
public static void main(String[] args) {
File f = new File("E:/x.txt");
// 文件读取流管道
FileOutputStream fos = null;
int i = 0;
String str = "adsfasdf";
// 字节容器
byte[] b = str.getBytes();
int y = 0;
try {
// 将管道接进文件 boolean是否在后面继续加,默认是false
fos = new FileOutputStream(f, true);
//间字节数值写入文件里
fos.write(b);
System.out.println("写完收工");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 不为空就关闭流,不关闭会占用内存空间
if (fos != null){
// 这个方法的作用是把缓冲区的数据强行输出
fos.flush();
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


字符流:在java里中文也等于一个字符,如果用于读取包含中文的文档,用字符流比较方便,不用自己再去转码。只是效率比较慢。

读取:

public class Test {
public static void main(String[] args) {
read();
}
public static String read() {
// 文件
File f = new File("E:/x.txt");
// 文件读取流管道
FileReader fis = null;
int i = 0;
String str = new String();
// 字符容器
char[] c = new char[1024];
int y = 0;
try {
// 将管道接进文件
fis = new FileReader(f);
// 读取每一个字符
while ((i = fis.read()) != -1) {
c[y++] =  (char) i;
}
// 字符数组转义成字符串,再进行转码
str = new String(c);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 不为空就 关闭流
if (fis != null)
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return str;
}
}


写入:


public class Test {
public static void main(String[] args) {
write();
}
public static void write() {
// 文件
File f = new File("E:/x.txt");
// 文件读取流管道
FileWriter fos = null;
String str = "adsfasdf";
try {
// 将管道接进文件 boolean是否在后面继续加,默认是false
fos = new FileWriter(f, true);
//间字符串数值写入文件里
fos.write(str);
System.out.println("写完收工");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 不为空就 关闭流
if (fos != null){
fos.flush();
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


实例:实现文件复制功能:(用字节流效率更高)采用缓冲流技术(BufferedInputStream ,BufferedOutputStream )


public class Test {
public static void main(String[] args) {
/** 复制的文件 **/
String fileName="E:/x.zip";
/**复制后生成的文件 **/
String copeToFileName="f:/xx.zip";
/**起始时间**/
long stratTime = System.currentTimeMillis();
copeOfFile(fileName,copeToFileName);
/**结束时间**/
long endTime = System.currentTimeMillis();
/** 所用的时间 主要测试效率的问题**/
System.out.println(endTime-stratTime);
}
/**
 * 复制到一个文件上
 * @param fileName 复制的文件
 * @param copeToFileName 复制后生成的文件 
 */
public static void copeOfFile(String fileName, String copeToFileName) {
File file = new File(fileName);
File copeFile = new File(copeToFileName);
/** 用缓冲技术较为快 **/
BufferedInputStream br = null;
BufferedOutputStream bw= null;
try {
br =new BufferedInputStream(new FileInputStream(file));
bw =new BufferedOutputStream(new FileOutputStream(copeFile));
int i = 0;
while((i =br.read())!=-1){
bw.write(i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭流
if(br != null)
br.close();
if(bw != null){
//这个方法的作用是把缓冲区的数据强行输出,重点
bw.flush();
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值