字节流
BufferedReader、BufferedWriter实现文件的复制
以字节为单位来读取文件,适用所有文件(mp3 mp4 txt exe docx .......)
字符流
以字符为单位来读取文件, 只适用 txt文件 文本文档的读取
FileInputStream 、FileOutputStream实现文件的删除复制
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Dome_FMove {
public static void main(String[] args) {
// TODO Auto-generated method stub
//移动目标文件 #
File file_del = new File("#");
File file_new = new File("#");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file_del);
fos = new FileOutputStream(file_new);
//缓冲区
byte[] buffer = new byte[2048];
int len = -1;
while((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
// file_del.delete(); 无法删除 因为文件流还在占用
// 2. 在虚拟机运行后删除
file_del.deleteOnExit();
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
if(fis != null) fis.close();
if(fos != null) fos.close();
}catch(Exception e) {
e.printStackTrace();
}
}
// 1.在关闭后删除
// file_del.delete();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 备注:
*
* 文件的复制 字节流适用于 所有文件
*
* .write(buffer, 0, len); 防止 缓冲区的数据多余 截取文件内容
*
* @version
*
*/
public class Dome_Copy {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("d:/java.txt");
fos = new FileOutputStream("src/dome/day09/test.ignore");
// 缓冲区
byte[] buffer = new byte[2048];
int len = -1;
while((len = fis.read(buffer)) != -1) {
//将读取出的内容 写入一个文件中
//从 0 开始 文件末尾结束
fos.write(buffer, 0, len);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(fis != null) {
fis.close();
}
if(fos != null) {
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
BufferedReader、BufferedWriter实现文件的复制
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 备注:
* BufferReader BufferWriter 缓冲字符流
* 需要接收一个 字符流 new FileReader or new FileWrither
*
* readLine() 读一行
* 利用
* StringBuffer sb = new StringBuffer();
* sb.append(); 字符拼接
*
* bw.write(sb.toString());
* sb.toString() 利用toString() 转为字符 写入其中
* 优点:
* 读写 效率高
*
* @version
*
*/
public class Demo_BufferReader {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader fr = null;
BufferedReader br = null;
FileWriter fw = null;
BufferedWriter bw = null;
try {
fr = new FileReader("");
br = new BufferedReader(fr);
fw = new FileWriter("");
bw = new BufferedWriter(fw);
StringBuffer sb = new StringBuffer();
String line = null;
while((line = br.readLine()) != null) {
sb.append(line+"\n");
}
bw.write(sb.toString());
bw.flush();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
//关闭流 顺序和 new的 顺序相反
if( bw != null) bw.close();
if( fw != null) fw.close();
if( br != null) br.close();
if( fr != null) fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
FileReader、FileWriter
实现文件的复制
import java.io.FileReader;
import java.io.FileWriter;
/** * 备注:
*
* 字符流 FileReader FileWriter
* 字符占两个字节 --> char
* 定义一个 char数组 作为缓冲区
* char[] buffer = new char[128];
* fw.write("可以接受字符");
* @version
*
*/
public class Dome_FRString {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("#");
fw = new FileWriter("#");
fw.write("可以接受字符");
// 1 .
// int c= -1;
// while((c = fr.read()) != -1) {
// System.out.println((char)c);
// }
// 2. 定义一个 char数组
char[] buffer = new char[128];
int len = -1 ;
while((len = fr.read(buffer)) != -1) {
fw.write(buffer, 0, len);
}
fw.flush();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(fr != null) fr.close();
if(fw != null) fw.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
字符替换 str.replace(old , new)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 备注:
*
* 字符替换 str.replace(old , new);
*
* 读取文件 输出文件 实例化Student 替换相关信息 写入文件 输出文件
* Student.java
* private String name;
* private Integer age;
* @version
*
*/
public class Demo_replace {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("src/demo/day09/Student.txt");
File file_up = new File("src/demo/day09/test.ignore");
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
fw = new FileWriter(file_up);
bw = new BufferedWriter(fw);
String line = null;
StringBuffer sb = new StringBuffer();
while((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println("old:"+sb.toString());
Student student = new Student("李元芳",28);
// sb.replace(0, "{name}", student.getName());
// sb.replace(sb.indexOf("{"), sb.indexOf("}")+1, student.getName());
// sb.replace(sb.indexOf("{"), sb.indexOf("}")+1, student.getAge().toString());
String str = sb.toString();
String str2 = str.replace("{name}", student.getName()).replace("{age}", student.getAge().toString());
// str.replace("{name}", "aaaa");
System.out.println("new:"+str2);
bw.write(str2.toString());
bw.flush();
// fr1 = new FileReader(file_up);
// br = new BufferedReader(fr1);
// String line1 = null;
// while((line1 = br.readLine()) != null) {
// System.out.println(line1);
// }
//
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(bw != null) bw.close();
if(fw != null) fw.close();
if(br != null) br.close();
if(fr != null) fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
以上都是自己在学Java时的笔记
发个博客 纪念点滴
如有雷同,不如加个联系呗