1.什么是IO流
I:input O:output
流:stream,用来实现程序或进程间的通信,或读读写外围设备、外部文件等
2.IO流分为几类
3.什么是字节流?什么是字符流?
4.字节流和字符流的区别?
a,读写的时候一个是按字节读写,一个是按字符
b,需要对内容按行处理,一般选择字符流
c,只是读写文件,和文件内容无关的(下载、复制等),一般选择字节流
5.字符流的常用类有哪些
BufferedReader\BufferedWriter
6.实现文件复制的思路和步骤是什么
//用字节流方式实现
import java.io.FileNotFoundException;
import java.io.IOException;
public class Try1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
java.io.FileInputStream intput = null;
java.io.FileOutputStream output = null;
int n = 0;
try {
intput = new java.io.FileInputStream("D:\\bbb\\infor.txt");
output = new java.io.FileOutputStream("D:\\bbb\\infor4.txt");
do {
try {
n = intput.read();
output.write(n);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while (n!=0);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
intput.close();
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//用字符流实现
import java.io.IOException;
public class Try2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
java.io.FileReader reader = null;
java.io.BufferedReader bReader = null;
java.io.BufferedWriter writer = null;
try {
reader =new java.io.FileReader("D:\\bbb\\infor.txt");
bReader =new java.io.BufferedReader(reader);
writer =new java.io.BufferedWriter(new java.io.FileWriter("D:\\bbb\\infor9.txt"));
String temp ="";
while((temp=bReader.readLine())!=null) {
writer.write(temp);
writer.newLine();
}
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
writer.close();
bReader.close();
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
7.如何使用字符流进行读写文件
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* 用字符流对文件进行写入
* 使用字符流-writer-bufferedweiter
* @author Shinelon
*
*/
public class Try3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("D:\\bbb\\infor.txt"));
writer.write("无锡");
writer.write("Hello World");
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
06_10补
IO流有很多异常要处理,在mian方法直接抛出异常给Exception,要比在后来用try-cash要更加简单,清楚,省事
package com.lenovo.s0607_IO;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
/**
* 用字节流实现写入文件
* 使用字节流-OutputStream-FileOutputStream
* byte 是字节数据类型 ,是有符号型的,占1 个字节;大小范围为-128—127
* char 是字符数据类型 ,是无符号型的,占2字节(Unicode码 )
*
*/
public class A1 {
public static void main(String[] args) ***throws Exception*** {
OutputStream output = null;
output = new java.io.FileOutputStream(new File("d:" + File.separator + "Lenovo.txt"), true);
// true表示追加新的内容,并不覆盖原有的内容
String s = "\r\n你好中国";// "\r\n"换行
byte[] b = s.getBytes();
// out.write(b);
// 用循环的方式,每次只写入一个内容
for (int i = 0; i < b.length; i++) {
output.write(b[i]);
}
output.close();
}
}
----------
package com.lenovo.s0607_IO;
/**
* 用字节流实现读取文件
* 使用字节流-InputStream-FileInputStream
*
*/
import java.io.File;
public class A2 {
public static void main(String[] args) throws Exception{
File f = new File("d:"+File.separator+"Lenovo.txt");
java.io.InputStream input = null;
input = new java.io.FileInputStream(f);
byte[] b = new byte[(int) f.length()];
//int neirong = input.read(b);
for(int i=0;i<b.length;i++) {
b[i]=(byte) input.read();
}
input.close();
System.out.println("内容为:\r\n"+new String(b));
}
}
----------
package com.lenovo.s0607_IO2;
/**
* 用字符流实现文件读取
* 使用字符流-reader-FileReader
*
*/
import java.io.File;
public class A1 {
public static void main(String[] args) throws Exception{
File f = new java.io.File("d:"+java.io.File.separator+"Lenovo.txt");
java.io.Reader reader = null;
reader = new java.io.FileReader(f);
char[] c = new char[(int) f.length()];
//int neirong = reader.read(c);
int i = 0;
int temp = 0;
while((temp=reader.read())!=-1) {
c[i]=(char)temp;
i++;
}
System.out.println("内容为"+new String(c));
}
}
----------
package com.lenovo.s0607_IO2;
/**
* 使用字符流实现写入文件
* 用字符流-Writer-FileWriter
*/
import java.io.File;
public class A2 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File f = new File("d:" + java.io.File.separator + "Lenovo.txt");
java.io.Writer write = null;
write = new java.io.FileWriter(f, true);
String s = "你好";
write.write(s);
write.flush();
// 或者这个也能达到同样的效果
// write.close();
}
}