// =====================IO字节流===================================
// 按流向分为:输入流和输出流
// 按操作类型分为字节流和字符流
/**
* 字节流的抽象父类 InputStream OutputStream 字符流的抽象父类 Reader Writer
*
*/
FileInputStream fis = new FileInputStream("xxx.txt");
int b;
// b= fis.read();//读取文件中的一个字节
while ((b = fis.read()) != -1) {
// System.out.println(b);
}
fis.close();
// 第二个参数是设置是否将内容追加到文件中
FileOutputStream fos = new FileOutputStream("xxx.txt", true);// 创建字节流对象,如果没有则创建文件
fos.write(100);// 向文件写入内容(覆盖式)
// 拷贝文件
/**
* 逐个字节拷贝,效率低 FileInputStream fis1 = new FileInputStream("变量替换.jpg");
* FileOutputStream fos1 = new FileOutputStream("copy.jpg"); int b1; while
* ((b1=fis1.read()) != -1) { fos1.write(b1); } fis1.close(); fos1.close();
*/
/**
* 一次读取,效率高,太耗计算机,有可能导致内存溢出 FileInputStream fis1 = new
* FileInputStream("变量替换.jpg"); FileOutputStream fos1 = new
* FileOutputStream("copy.jpg"); byte[] arr = new
* byte[fis.available()];//创建文件大小一样的数组,弊端:太大文件内存不允许 fis1.read(arr);
* fos1.write(arr); fis1.close(); fos1.close();
*/
/**
* 这种做法会出字节 FileInputStream fis1 = new FileInputStream("变量替换.jpg");
* FileOutputStream fos1 = new FileOutputStream("copy.jpg"); byte[] arr = new
* byte[1024]; int len; while ((len = fis1.read(arr)) != -1){ fos1.write(arr); }
* fis1.close(); fos1.close();
*/
/*FileInputStream fis1 = new FileInputStream("变量替换.jpg");
FileOutputStream fos1 = new FileOutputStream("copy.jpg");
byte[] arr = new byte[1024];
int len;
while ((len = fis1.read(arr)) != -1) {
fos1.write(arr, 0, len);
}
fis1.close();
fos1.close();
// BufferedInputStream和BufferedOutputStream拷贝
FileInputStream fis2 = new FileInputStream("变量替换.jpg");
FileOutputStream fos2 = new FileOutputStream("copy.jpg");
BufferedInputStream bis = new BufferedInputStream(fis2);
BufferedOutputStream bos = new BufferedOutputStream(fos2);
int b1;
while ((b1 = bis.read()) != -1) {
bos.write(b1);
}
bis.close();
bos.close();
// 流的标准异常处理
try (FileInputStream fis3 = new FileInputStream("变量替换.jpg");
FileOutputStream fos3 = new FileOutputStream("copy.jpg");) {
int b2;
while ((b2 = fis3.read()) != -1) {
fos3.write(b2);
}
}
// 图片加密
BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream("变量替换.jpg"));
BufferedOutputStream bos1 = new BufferedOutputStream(new FileOutputStream("copy.jpg"));
int b3;
while ((b3 = bis.read()) != -1) {
bos.write(b3 ^ 123);//同一个字节异或两次等于这个字节本身,读的时候再异或一次
}
bis1.close();
bos1.close();*/
// =====================IO字符流===================================
/**
* 字节流是可以直接读写字节的IO流
* 字节流读取字符,先要读到字节数据,然后转为字符,如果需要写出字符,需要吧字符转为字节再写出
*/
/**
* FileReader fr = new FileReader("字节流.txt");
FileWriter fw = new FileWriter("字节流2.txt");
int ch;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
fr.close();
fw.close();
*/
/**自定义字符拷贝
* FileReader fr = new FileReader("字节流.txt");
FileWriter fw = new FileWriter("字节流2.txt");
int len;
char[] arr = new char[1024];
while ((len = fr.read(arr)) != -1) {
fw.write(arr, 0, len);
}
fr.close();
fw.close();
*/
/**带缓冲的字符流
* //BufferedReader的read()方法读取字符时会一次读取若干字符到缓冲区,然后逐个返回给程序,降低读取文件的次数,提高效率
//BufferedWriter的write()方法写出字符时会先写到缓冲区,缓冲区写满时才会写到文件,降低写文件的次数,提高效率
BufferedReader br = new BufferedReader(new FileReader("字节流.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("字节流2.txt"));
int ch;
while ((ch=br.read()) != -1) {
bw.write(ch);
}
br.close();
bw.close();
*/
/**readLine()方法和newLine()方法
* //BufferedReader的readLine()方法可以读取一行字符(不包含换行符号)
//BufferedWriter的newLine()方法可以输出一个跨平台的换行符号“\r\n”
BufferedReader br = new BufferedReader(new FileReader("字节流.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("字节流2.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
*/