一、输入输出流原理
在Java程序中,对于数据的输⼊和输出以“流”(stream)的⽅式进⾏;程序中通过指定的⽅法来输⼊和输出数据
二、字节流
2.1、字节输入流FileInputStream
程序以字节为单位从磁盘文件读取数据
/***************创建输⼊入流InputStream-begin********************/
FileInputStream in = null;
try {
in = new FileInputStream("C:\\Users\\qinzhicong\\Documents\\hello.txt");
} catch (FileNotFoundException e) {
System.out.println("找不不到指定⽂文件");
System.exit(0);
}
/***************创建输⼊入流InputStream-end********************/
long num = 0; //⽤用于计数的变量量,作⽤用是计算⼀一共读了了多少个字节
int b = 0; //⽤用于记录读取位置的变量量
try {
/***************读取⽂文件-begin********************/
while((b = in.read()) != -1){
System.out.println((char)b);
num ++;
}
in.close();
System.out.println("共读取了了"+num+"个字节");
/***************读取⽂文件-end********************/
} catch (IOException e) {
System.out.println("⽂文件读取错误");
System.out.println(0);
}
2.2、字节输出流FileOutputStream
程序以字节为单位,向磁盘文件写入数据
/***************创建输出流FileOutputStream-begin********************/
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("C:\\Users\\qinzhicong\\Documents\\hello.txt");
out = new FileOutputStream("C:\\Users\\qinzhicong\\Documents\\hello1.txt");
} catch (FileNotFoundException e) {
System.out.println("找不不到指定⽂文件");
System.exit(0);
}
/***************创建输出流FileOutputStream-end********************/
int b = 0;
try {
/***************拷⻉贝⽂文件-begin********************/
while((b = in.read()) != -1){
out.write(b);
}
in.close();
out.close();
/***************拷⻉贝⽂文件-end********************/
} catch (IOException e) {
System.out.println("⽂文件复制错误");
System.exit(0);
}
三、字符流
3.1、字符输入流FileReader
程序以字符为单位从磁盘文件读取数据
FileReader fr = null;
try {
fr = new FileReader("C:\\Users\\qinzhicong\\Documents\\hello.txt");
int c = 0;
while((c = fr.read()) != -1){
System.out.println((char)c);
}
fr.close();
} catch (FileNotFoundException e) {
System.out.println("找不不到指定⽂文件");
System.exit(0);
} catch (IOException e) {
System.out.println("⽂文件读取错误");
System.exit(0);
}
3.2、字符输出流FileWriter
程序以字符为单位,向磁盘文件写入数据
FileWriter fw = null;
try {
fw = new FileWriter("C:\\Users\\qinzhicong\\Documents\\hello2.txt");
String str = "HelloWorld我的Java世界";
fw.write(str);
fw.close();
} catch (IOException e) {
System.out.println("⽂文件写⼊入错误");
System.exit(0);
}
四、缓冲流
缓冲流对读写的数据提供了缓冲的功能,提⾼了读写的效率
4.1、字节缓冲流
4.1.1、字节缓存输入流
try {
FileInputStream in = new FileInputStream("C:\\Users\\qinzhicong\\Documents\\hello.txt");
BufferedInputStream bis = new BufferedInputStream(in, 1024);
int b;
while((b = bis.read()) != -1){
System.out.println((char)b);
}
bis.close();
in.close();
} catch (FileNotFoundException e) {
System.out.println("⽂文件没有找到");
} catch (IOException e) {
System.out.println("⽂文件操作错误");
}
4.1.2、字节缓冲输出流
try {
FileOutputStream out = new FileOutputStream("C:\\Users\\qinzhicong\\Documents\\hello1.txt");
BufferedOutputStream bos = new BufferedOutputStream(out, 1024);
bos.write("helloMyJava".getBytes());
bos.close();
out.close();
} catch (FileNotFoundException e) {
System.out.println("⽂文件没有找到");
} catch (IOException e) {
System.out.println("⽂文件操作错误");
}
4.2、字符缓冲流
4.2.1、字符缓冲输入流
try {
FileReader fr = new FileReader("C:\\Users\\qinzhicong\\Documents\\hello.txt");
BufferedReader br = new BufferedReader(fr, 1024);
String str;
while((str = br.readLine()) != null){
System.out.println(str);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
System.out.println("⽂文件没有找到");
} catch (IOException e) {
System.out.println("⽂文件操作错误");
}
4.2.2、字符缓冲输出流
try {
FileWriter fw = new FileWriter("C:\\Users\\qinzhicong\\Documents\\hello2.txt");
BufferedWriter br = new BufferedWriter(fw);
br.write("来呀,相互伤害呀");
br.newLine();
br.write("来呀,相互伤害呀");
br.close();
fw.close();
} catch (FileNotFoundException e) {
System.out.println("⽂文件没有找到");
} catch (IOException e) {
System.out.println("⽂文件操作错误");
}
4.2.3、换行的方法
readLine:该⽅法是BufferedReader对象提供的⽅法,⽅法返回值是String类型,表⽰每次读取⼀⾏的内容
newLine:该⽅法是BufferedWriter对象提供的⽅法,表⽰向⽂件中写⼊⼀个换⾏符