Java基础之IO流
2019-08-04 本文已影响0人
晨冉1688
##Java基础之IO流
IO流常用几个类的关系如下:
IO.JPG
字节流
字节输入流FileInputStream 读取文件用法实例
通过字输入流从D盘中dd.txt文件中读取文件内容,采用字节输入流,每次读取一个字节的方式
public class MyInputStreamDemo {
public static void main(String[] args) throws Exception {
//创建字节输入流
FileInputStream fis = new FileInputStream("d:/dd.txt");
int by;
while ((by = fis.read()) != -1) {
System.out.print((char) by);
}
fis.close();
}
}
通过字节数组读取的方式如下
public class MyInputStreamDemo {
public static void main(String[] args) throws Exception {
//创建字节输入流
FileInputStream fis = new FileInputStream("d:/dd.txt");
//通过字节数组,每次读取一个字节数组的方式读取文件
byte[] arr =new byte[1024];
int len;
while ((len = fis.read(arr))!=-1){
String s = new String(arr,0,len);
System.out.println(s);
}
//释放资源
fis.close();
}
}
字节输出流FileOutputStream 写文件用法实例
通过一个一个字节的方式读取
public static void main(String[] args) throws IOException {
//创建字节输出流,并且指向d盘中的aa.txt文件
FileOutputStream fos = new FileOutputStream("d:/aa.txt");
int num = 10000;
//循环写num次'a'字符,并且通过System类来计算所耗时间
long startTime = System.currentTimeMillis();
for (int i = 0; i <num ; i++) {
fos.write('a');
}
long endTime = System.currentTimeMillis();
//释放资源
fos.close();
}
FileOutputStream输出流,还可以一次写一个字节数组
public static void main(String[] args) throws IOException {
FileOutputStream fos= new FileOutputStream("d:/bb.txt");
int num = 1000000;
//循环向d盘的bb.txt写入num次字符串"helloworld"
long startTime1 = System.currentTimeMillis();
for (int i = 0; i <num/10 ; i++) {
String s = "helloworld";
fos1.write(s.getBytes()); //getBytes()可以把一个字符串转换为一个字节数组
}
long endTime1 = System.currentTimeMillis();
System.out.println(endTime1 - startTime1);
//释放资源
fos.close();
}
在实际开发中,为了提高效率,一般采用带有缓冲功能的BufferedInputStream和BufferedOutputStream
BufferedInputStrem常见用法如下
从文件中读取内容,并且复制到其他地方
一个字节一个字节读取实例,比如拷贝文件,从E盘将py.exe,拷贝到D盘,并且命名为:copypy.exe,实现代码如下:
public static void main(String[] args) throws Exception {
//创建带缓冲的字节输入流对象
BufferedInputStream fis = new BufferedInputStream(new FileInputStream("E:/py.exe"));
//创建带缓冲的字节输出流对象
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream("D:/copypy.exe"));
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
//释放资源
fos.close();
fis.close();
}
一个字节数组的方式操作,比如拷贝文件,从E盘将py.exe,拷贝到D盘,并且命名为:copypy.exe,实现代码如下:
public static void main(String[] args) throws Exception {
//创建带缓冲的字节输入流对象
BufferedInputStream fis = new BufferedInputStream(new FileInputStream("E:/py.exe"));
//创建带缓冲的字节输出流对象
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream("D:/copypy.exe"));
byte[] arr = new byte[1024]; //一般采用1024的整数倍,根据计算机性能可以适当调整,一般一倍到8倍就可以了.
int len;
while ((len = fis.read(arr)) != -1) {
fos.write(arr, 0, len); //没读取一个字节数组,就写一个字节数组
fos.flush(); //刷新缓冲区,将文件写入到硬盘
}
//释放资源
fos.close();
fis.close();
}
字符流
InputStreamReader和OutputStreamWriter
//在采用GBk编码方式,通过字符输出流,向d盘test.txt文件写入内容,然后通过字符输入流读取文件内容,将读到的内容打印在控制台
public static void main(String[] args) throws Exception {
//采用GBK的编码格式,创建字符输出流对象
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("d:/test.txt"), "GBK");
String s = "你好,世界,Hello,World";
outputStreamWriter.write(s); //字符流中的write()方法,可以直接传入字符串
outputStreamWriter.close();
//采用GBK的编码格式,创建字符输入流对象
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("d:/test.txt"), "GBK");
//一个字节一个字节读取
int by;
while ((by = inputStreamReader.read())!=-1){
System.out.print((char)by); //读取一个字节,就输出一个字节
}
//一次读数组
char[] chs = new char[1024];
int len;
while ((len = inputStreamReader.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));
}
inputStreamReader.close();
}
InputStreamReader和OutputStreamWriter对应的子类分别为FileReader和FileWriter在windows中采用默认的GBK编码格式的时,可以使用FileReader和FileWriter创建字符流
读取D盘中的MyBufferedDemo.java文件,写入到E盘中,实现代码如下:
public static void main(String[] args) throws Exception{
//创建字符输入流对象
FileReader fileReader = new FileReader("D:\\MyBufferedDemo.java");
//创建字符输出流对象
FileWriter fileWriter = new FileWriter("E:\\MyBufferedDemocopy.java");
//通过字符数组的方式,来实现
char[] ch = new char[1024];
int len;
while ((len = fileReader.read(ch))!=-1){
fileWriter.write(ch,0,len);
fileWriter.flush();
}
//释放资源
fileWriter.close();
fileReader.close();
}
在实际开发中,为了提高效率,一般采用带有缓冲功能的BufferedWriter和BufferedReader
这里两个类中有特殊于其他类的方法:readLine() 和 newLine()
//从d盘的ab.txt文件中读取内容,并写入到e盘中的ab.txt文件中
public static void main(String[] args) throws Exception {
//创建字符输入流对象
BufferedReader br = new BufferedReader(new FileReader("d:/ab.txt"));
//创建字符输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("e:/ab.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine(); //跟据系统环境进行换行
bw.flush();
}
//释放资源
bw.close();
br.close();
}
带有缓冲功能的,通过数组方式操作,拷贝文本文件,字符流BufferedReader,BufferedWriter和字节流BufferedInputStream,BufferedOutputStream,三种方式效率对比代码如下:
public static void main(String[] args) throws Exception {
long start1 = System.currentTimeMillis();
methods1();
long end1 = System.currentTimeMillis();
System.out.println("readLine:" + (end1 - start1)); //1128毫秒
long start2 = System.currentTimeMillis();
methods2();
long end2 = System.currentTimeMillis();
System.out.println("字符数组:" + (end2 - start2)); //563毫秒
long start3 = System.currentTimeMillis();
methods3();
long end3 = System.currentTimeMillis();
System.out.println("字节数组:" + (end3 - start3)); //267毫秒
}
//readLine
private static void methods1() throws Exception {
BufferedReader br = new BufferedReader(new FileReader("d:/ab.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("ab.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
//字符数组缓冲
private static void methods2() throws Exception {
BufferedReader br = new BufferedReader(new FileReader("d:/ab.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("ab2.txt"));
char[] arr = new char[1024];
int len;
while ((len = br.read(arr))!=-1){
bw.write(arr,0,len);
bw.flush();
}
bw.close();
br.close();
}
//字节数组缓冲
private static void methods3() throws Exception {
BufferedInputStream br = new BufferedInputStream(new FileInputStream("d:/ab.txt"));
BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream("ab3.txt"));
byte[] arr = new byte[1024];
int len;
while ((len = br.read(arr))!=-1){
bw.write(arr,0,len);
bw.flush();
}
bw.close();
br.close();
}