字节流读写文件:
1.字节流的缓冲区,提高读写效率:
//创建一个文件字节输出流,用于读取当前目录下的txt文件夹
FileInputStream in=new FileInputStream("text.txt");
//创建一个文件字节输出流,用于`将读取的数据写入text文件中
FileOutputStream out=new FileOutputStream("yy.txt",true);
byte[] by=new byte[1024];
int len;
while ((len=in.read(by))!=-1) {
out.write(by,0,len);
}
in.close();
out.close();
这里Byte[]数组,作为缓冲区用
2.字符流操作文件:
//创建一个Filewriter对象用于像文件中写入数据
FileReader fileReader=new FileReader("text.txt");
//创建一个BufferedReader缓冲对象
BufferedReader bufferedReader=new BufferedReader(fileReader);
//创建一个Filewriter对象用于像文件中读取数据
FileWriter fileWriter=new FileWriter("yy.txt");
//创建一个BufferedWreader缓冲对象
BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
String string;
while((string=bufferedReader.readLine())!=null){
bufferedWriter.write(string);
bufferedWriter.newLine();
}
bufferedReader.close();
bufferedWriter.close();
FileReader fileReader=new FileReader("text.txt");
//创建一个BufferedReader缓冲对象
BufferedReader bufferedReader=new BufferedReader(fileReader);
//创建一个Filewriter对象用于像文件中读取数据
FileWriter fileWriter=new FileWriter("yy.txt");
//创建一个BufferedWreader缓冲对象
BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
String string;
while((string=bufferedReader.readLine())!=null){
bufferedWriter.write(string);
bufferedWriter.newLine();
}
bufferedReader.close();
bufferedWriter.close();
3.lineNumberReader给代码加行号:
FileReader fileReader=new FileReader("test3.java");//创建字符输入`流
FileWriter fileWriter=new FileWriter("text.txt");//创建字符输出流
LineNumberReader liNumberReader=new LineNumberReader(fileReader);//包装
liNumberReader.setLineNumber(0);//设置读取文件的包装其实行
String line=null;
while ((line=liNumberReader.readLine())!=null) {
fileWriter.write(liNumberReader.getLineNumber()+":"+line);
fileWriter.write("\t\n");
}
liNumberReader.close();
fileWriter.close();
FileWriter fileWriter=new FileWriter("text.txt");//创建字符输出流
LineNumberReader liNumberReader=new LineNumberReader(fileReader);//包装
liNumberReader.setLineNumber(0);//设置读取文件的包装其实行
String line=null;
while ((line=liNumberReader.readLine())!=null) {
fileWriter.write(liNumberReader.getLineNumber()+":"+line);
fileWriter.write("\t\n");
}
liNumberReader.close();
fileWriter.close();
4.转换流:文件已字节形式保存,以字符形式输出
BufferedInputStream和BufferedOutputStream是过滤流,需要使用已存在的节点来构造,即必须先有InputStream或OutputStream,相对直接读写,这两个流提供带缓存的读写,提高了系统读写效率性能.BufferedInputStream读取的是字节byte,因为一个汉字占两个字节,而当中英文混合的时候,有的字符占一个字节,有的字符占两个字节,所以如果直接读字节,而数据比较长,没有一次读完的时候,很可能刚好读到一个汉字的前一个字节,这样,这个中文就成了乱码,后面的数据因为没有字节对齐,也都成了乱码.所以我们需要用BufferedReader来读取,它读到的是字符,所以不会读到半个字符的情况,不会出现乱码.
BufferedInputStream过滤流,datainputstream
BufferedInputStream和BufferedReader 一起出席,流的装换
PrintWriter他是不用进行流的转换
FileInputStream fileInputStream =new FileInputStream("text.txt");//创建字节输入流
InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);//将字节输入流转换成字符输入流
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);//将字符流对象进行包装
FileOutputStream fileOutputStream=new FileOutputStream("yy.txt");//创建字节输出流
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream);//将字节输出流转换成字符输出流
BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);//对字符输出流进行包装;
String line;
while ((line=bufferedReader.readLine())!=null) {//判断是否读完文件
bufferedWriter.write(line);//输出读到的文件
}
bufferedReader.close();
bufferedWriter.close();
InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);//将字节输入流转换成字符输入流
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);//将字符流对象进行包装
FileOutputStream fileOutputStream=new FileOutputStream("yy.txt");//创建字节输出流
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream);//将字节输出流转换成字符输出流
BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter);//对字符输出流进行包装;
String line;
while ((line=bufferedReader.readLine())!=null) {//判断是否读完文件
bufferedWriter.write(line);//输出读到的文件
}
bufferedReader.close();
bufferedWriter.close();
5.ObjectInputStrem和ObjectInputStream:
下面为写入文件 out (写入)
用objectinputstream来储存对象信息
Person person=new Person("pi", "zhangsan", 36);//创建一个Person对象
System.out.println("写入文件前");
System.out.println("Person对象的ID:"+person.getId());
System.out.println("Person对象的name:"+person.getName());
System.out.println("Person对象的age:"+person.getAge());
//创建文件输出流对象,将文件写入到text.txt里
FileOutputStream fileInputStream=new FileOutputStream("text.txt");
//创建对象输出流对象,用Objecinputstream进行处理
ObjectOutputStream objectOutputStream =new ObjectOutputStream(fileInputStream);
//将对象Person输出到流中
objectOutputStream.writeObject(person);
}
}
class Person implements Serializable{
private String id;
private String name;
private int age;
public Person(String id, String name,int age) {
// TODO Auto-generated constructor stub
super();
this.id=id;
this.name=name;
this.age=age;
}
public String getId(){
return id;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
System.out.println("写入文件前");
System.out.println("Person对象的ID:"+person.getId());
System.out.println("Person对象的name:"+person.getName());
System.out.println("Person对象的age:"+person.getAge());
//创建文件输出流对象,将文件写入到text.txt里
FileOutputStream fileInputStream=new FileOutputStream("text.txt");
//创建对象输出流对象,用Objecinputstream进行处理
ObjectOutputStream objectOutputStream =new ObjectOutputStream(fileInputStream);
//将对象Person输出到流中
objectOutputStream.writeObject(person);
}
}
class Person implements Serializable{
private String id;
private String name;
private int age;
public Person(String id, String name,int age) {
// TODO Auto-generated constructor stub
super();
this.id=id;
this.name=name;
this.age=age;
}
public String getId(){
return id;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
6.DataInputStream和DataoutputStream:
用datainputstream来保存成员信息
FileOutputStream fileOutputStream =new FileOutputStream("text.txt");
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fileOutputStream);
DataOutputStream dataOutputStream =new DataOutputStream(bufferedOutputStream);
dataOutputStream.writeByte(36);
dataOutputStream.writeChar('1');
dataOutputStream.writeBoolean(true);
dataOutputStream.writeUTF("同学,你好!");
dataOutputStream.close();
FileInputStream fileInputStream =new FileInputStream("text.txt");
BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream);
DataInputStream dataInputStream=new DataInputStream(bufferedInputStream);
System.out.println(dataInputStream.readByte());
System.out.println(dataInputStream.readChar());
System.out.println(dataInputStream.readBoolean());
System.out.println(dataInputStream.readUTF());
BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fileOutputStream);
DataOutputStream dataOutputStream =new DataOutputStream(bufferedOutputStream);
dataOutputStream.writeByte(36);
dataOutputStream.writeChar('1');
dataOutputStream.writeBoolean(true);
dataOutputStream.writeUTF("同学,你好!");
dataOutputStream.close();
FileInputStream fileInputStream =new FileInputStream("text.txt");
BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream);
DataInputStream dataInputStream=new DataInputStream(bufferedInputStream);
System.out.println(dataInputStream.readByte());
System.out.println(dataInputStream.readChar());
System.out.println(dataInputStream.readBoolean());
System.out.println(dataInputStream.readUTF());
7.PrintStream:
8.setin ,setout .seterr重定向静态方法
9. pipedinputstream和pipedoutputstre:
管道流必须先建立连接才进行通信,主要用于线程
10.byteArrayinputstrem和byteoutputstream:
11.chararrayreader和chararreaywriter:
12. Sequenceinputstream:
13.File类:
获得文件
File file =new File("C:\\Users\\lxb\\Documents\\Tencent Files\\2461552641\\FileRecv");
if (file.isDirectory()) {//判断是否是目录
String[] names=file.list();//获得文件的所有文件名清单
for(String name: names){
System.out.println(name);
}
}
//创建file对象
File file1=new File("C:\\Users\\lxb\\Documents\\Tencent Files\\2461552641\\FileRecv");
//创建过滤器对象
FilenameFilter filenameFilter=new FilenameFilter() {
//实现accept,暂时阻塞
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
File files=new File(dir, name);
//如果文件名以。java结尾返回true,否则返回false
if (files.isFile()&&name.endsWith(".java")) {
return true;
}
return false;
}
};
if (file.isDirectory()) {//判断是否是目录
String[] names=file.list();//获得文件的所有文件名清单
for(String name: names){
System.out.println(name);
}
}
过滤文件
//创建file对象
File file1=new File("C:\\Users\\lxb\\Documents\\Tencent Files\\2461552641\\FileRecv");
//创建过滤器对象
FilenameFilter filenameFilter=new FilenameFilter() {
//实现accept,暂时阻塞
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
File files=new File(dir, name);
//如果文件名以。java结尾返回true,否则返回false
if (files.isFile()&&name.endsWith(".java")) {
return true;
}
return false;
}
};
删除文件夹
File file2=new File("C:\\Users\\lxb\\Documents\\Tencent Files\\2461552641\\FileRecv");
delecteDir(file2);
}
private static void delecteDir(File file2) {
// TODO Auto-generated method stub
if (file2.exists()) {
File[] files=file2.listFiles();//得到所有文件名
for(File file:files){//遍历所有的子目录和文件
if (file.isDirectory()) {//如果是目录
delecteDir(file2);
}else{
file.delete();
}
}