文件File类、IO流
File
构造函数
方法
遍历目录
过滤器
FIleNameFliter,自动调用accept方法
@Override
public boolean accept(File dir, String name) {
File f = new File(dir.getPath() + File.separator + name);
if(name.endsWith(".java") || f.isDirectory()){
return true;
}
return false;
}
输入输出流
抽象类
InputStream/OutputStream
子类
类型
读(从磁盘input到内存)
字节输入流 FileInputStream
字符输入流 FileReader
写 (从内存output到磁盘文件)
字节输出流 FileOutputStream
字符输出流 FileWriter
纯文本内容建议使用字符输出流
带有图片影音等建议使用字节输出流
方法
-
构造函数
FileInputStream (File对象或String的path)
FileReader (File对象或String的path)
FileOutputStream (File对象或String的path)
FileOutputStream (File对象或String的path,Boolean是否是追加模式)
FileWriter(File对象或String的path)
FileWriter(File对象或String的path,Boolean是否是追加模式) -
read/write();
参数无参,一次读一个字节/字符; 字节流可以有参byte数组(作为缓冲区),自定义的数组长度是 字符流可以有参char数组(作为缓冲区),自定义的数组长度是多少,一次就读/写几个
返回
Inputstream返回读到的字符的ASCII码值
reader返回有效读取的字符/字节数。
可以记录下来并在输出时只输出有效的字符数(后面 无效的字符 数不要)
- flush();
字节流flush()方法无效;
字符流一定要刷新后才能把缓冲区的内容强制输出; - close();
相当于刷新+关闭资源;资源关闭后无法再使用;
public static void main(String[] args) throws IOException {
File f1=new File("D://dIntro.txt");
cut(f1, "E://dIntroCopy.txt");
}
public static File cut(File f1,String newpath) throws IOException {
FileReader fr1 = new FileReader(f1);
FileWriter fw1 = new FileWriter(newpath);
char[] b = new char[1024];
int len = 0;
while((len=fr1.read(b))!=-1){
String s1 = new String(b);
fw1.write(s1,0,len);
}
fw1.close();//不关闭资源无法执行删除;但关闭资源后无法再使用的
fr1.close();//不关闭资源无法执行删除;但关闭资源后无法再使用的
f1.delete();
return new File(newpath);
}
缓冲流(带缓冲区的输入输出流)
提交了文件的读写效率(时间效率)
类型及构造函数
BufferedInputStream (InputStream对象)
BufferedReader (Reader对象)
BufferedOutputStream (OutputStream对象)
BufferedWriter(Reader对象)
方法
- 同输入输出流
区别:所有方法都要刷新才能输出结果(因为都是带缓冲区的了) - 特有方法
BufferedWriter
newLine();新建一行/换行,无返回
可以根据不同系统自适应调用换行的转义符
*换行 Windows: ‘/r/n’
一般换行都是操作字符,直接用bufferedWriter,调用newline方法即可*
BufferedReader
readLine();读取一行,返回String,返回空时读完;
public static void main(String[] args) throws IOException {
File f1=new File("D://dIntro.txt");
cut(f1, "E://dIntroCopy.txt");
}
public static File cut(File f1,String newpath) throws IOException {
BufferedReader fr1 = new BufferedReader(new FileReader(f1));
BufferedWriter fw1 = new BufferedWriter(new FileWriter(newpath));
String string= null;
while((string=fr1.readLine())!=null){
fw1.write(string);
fw1.newLine();
}
fw1.close();
fr1.close();
f1.delete();
return new File(newpath);
}
转换流
InputStreamReader(InputStream对象,String编码格式)
OutputStreamWriter(OutputStream对象,String编码格式)
可以在构造函数中更改编码格式,解决乱码问题;
输出记得刷新
本人本机使用ISO8859-1或gbk编码格式可以成功写入写出中文(utf-8不可以)
InputStreamReader isr1 = new InputStreamReader(new FileInputStream(new File("D://b5.txt")),"ISO8859-1");
OutputStreamWriter isw1 = new OutputStreamWriter(new FileOutputStream(new File("D://b52.txt")),"ISO8859-1");
int j =0 ;
char[] cs = new char[1024];
while((j=isr1.read(cs))!=-1){
isw1.write(cs,0,j);
}
isw1.flush();
isw1.close();
打印流
PrintStream(String路径或file对象)
方法
-
write(byte[])方法
PrintStream ps = new PrintStream("D://a.txt"); ps.write("".getBytes());
-
与系统输出绑定
PrintStream ps = new PrintStream("D://a.txt"); System.setOut(ps); System.out.println("这里是要输出的内容"); //要输出的内容会从打印流对象指定的地址输出而非控制台输出
内存流
类型
字符内存流
ByteArrayOutputStream
ByteArrayInputStream
字节内存流(读中文比较好~)
CharArrayReader
CharArrayWriter
特点
无关磁盘,仅在内存进行操作,故:
- 没有在磁盘的地址(不用指定虚拟路径,构造函数可完全无参)
- 无法持久化使用写入或读出的数据(因为没有实际的一个文件存储它,只能在项目中作为内存中的变量使用)
- close方法即便有,也没有实际意义(close之后仍可以使用)
public static void main(String[] args) throws IOException {
ByteArrayOutputStream bas = new ByteArrayOutputStream();
bas.write("abc".getBytes());
byte[] b = bas.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
int i =0;
while((i=bais.read())!= -1 ){
System.out.println((char)i);
}
}
public static void main(String[] args) throws IOException {
CharArrayWriter caw = new CharArrayWriter();
caw.write("啊哈哈");
caw.close();//关了也白关
caw.write("诶嘿嘿!!");
char[] b = caw.toCharArray();
CharArrayReader car = new CharArrayReader(b);
int i =0;
while((i=car.read())!= -1 ){
System.out.println((char)i);
}
}
对象流
序列化流:对象→字节 ObjectOutputStream(OutputStream对象(文件地址为想要存储的地址))
反序列化流:字节→对象 ObjectInputStream(InputStream对象(文件地址为之前存储的地址))
-
对象实现序列化serializable接口
*无需重写任何方法,只是可序列化的状态标记,避免抛出 NotSerializableException *
实际开发中,实体类与数据库需要发生联系的,基本都要实现序列化接口,以便对象被写入磁盘public class Test1 implements Serializable{ private String testName; }
-
创建对象实例,想要将其存储下来(输出到磁盘)
Test1 t1 = new Test1(); t1.testName="测试1";
-
创建ObjectOutputStream往磁盘写入User对象
ObjectOutputStream oop = new ObjectOutputStream(new FileOutputStream("D://a.txt")); oop.writeObject(t1);
-
创建ObjectInputStream对象读取User对象
ObjectInputStream oip = new ObjectInputStream(new FileInputStream("D://a.txt")); Test1 t2 = (Test1)oip.readObject(); System.out.println(t2.testName);
public class Test1 implements Serializable{
private String testName;
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream oop = new ObjectOutputStream(new FileOutputStream("D://a.txt"));
Test1 t1 = new Test1();
t1.testName="测试1";
oop.writeObject(t1);
oop.close();
ObjectInputStream oip = new ObjectInputStream(new FileInputStream("D://a.txt"));
Test1 t2 = (Test1)oip.readObject();
System.out.println(t2.testName);
oip.close();
}
}
- transient
实现可序列化接口的类中,如果想要某个属性被标记为瞬时的(不可序列化的),添加关键字 transient即可。这样序列化存储此类对象时不会存储这个属性(同样也不能从对象流中再读取出来) - 父类实现可序列化接口,子类即可序列化的;子类的特有属性也是可序列化的(除非标记为transient);
properties
-
配置properties类型文件
键=值 键=值 键=值 ...
properties文件用于存储属性的键值对,用于便捷更改配置文件而无需改动其他代码
-
创建properties对象
Properties p = new properties();
-
加载一个输入流对象来读properties文件
InputStream is = 需要读配置文件的类名.class.getResourceAsStream("/properties文件名"); //记得加斜杠 p.load(InputStream流对象);
-
根据键获取值
p.getProperty(键);