一、基本概念
1、文件(File)
在计算机看来,文件和文件夹(目录,路径)是一种东西。通过File类来完成对文件的操作。其构造方法如下:
File(String pathname):通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例。
其常见方法有:
String[] | list() | 返回由此抽象路径名所表示的目录中的文件和目录的名称所组成字符串数组 |
File[] | listFiles() | 返回一个抽象路径名数组,这些路径名表示此抽象路径名所表示目录中的文件 |
案例:列出一个目录下所有的文件(文件夹不显示)
public class Test {
public static void showname(File file){
if(file.isDirectory()){
File[] ff=file.listFiles();
for (File fo : ff) {
showname(fo);//自己调用自己
}
}else{
System.out.println(file.getPath());
}
}
public static void main(String[] args) throws Exception{
File f=new File("D:"+File.separator+"ttt");
showname(f);
}
}
说明:字段 separator表示的是与系统有关的默认名称分隔符,出于方便考虑,它被表示为一个字符串。(windows用\,linux用/,使用separator自动调整)
2、字符编码
常见的字符编码
- gbk,简体中文
- big5,繁体中文
- iso-8859-1,纯英文
- Unicode,16进制编码,和iso-8859-1不兼容,但是表示的语言范围很广泛
- utf-8,全球文字字符都能表示
为了保证不出现乱码,在系统中所有的编码格式最好统一
案例1,本机的字符编码格式:
public static void main(String[] args) throws Exception{
System.out.println(System.getProperty("file.encoding"));
}
案例2,乱码的产生:
public static void main(String[] args) throws Exception{
File f=new File("D:\\ttt.txt");
OutputStream out=new FileOutputStream(f);
byte[] b="张三zhangsan333".getBytes("iso-8859-1");
out.write(b);
out.close();
}
二、文件流
1、基本介绍
流可以分为两种,一种是字节流,以字节为单位传输的流(万能):
- InputStream
- OutputStream
另一种为字符流,以字符为单位传输的流,只适合传递字符数据:
- Reader
- Writer
说明:
1.单位换算
- 1byte字节=8bit比特位
- kb,mb,gb,tb,pb
- 1字符=2字节
2.读写判断
- 应以内存的出入来判断使用输入流还是输出流
2、字节流
1.InputStream
常见方法:
void | close() | 关闭此输入流并释放与该流关联的所有系统资源 |
int | read(byte[] b) | 从输入流中读取一定数量的字节并将其存储在缓冲区数组 b 中。将字节数据读入b中并返回长度 |
案例,读数据:
public class Test2 {
public static void main(String[] args) throws Exception{
File f=new File("D:"+File.separator+"vvv.txt");
InputStream in=new FileInputStream(f);
byte[] b=new byte[1024*1024];
int len=in.read(b);
in.close();
String s=new String(b, 0, len);
System.out.println(s);
}
}
2.OutputStream
void | close() | 关闭此输出流并释放与此流有关的所有系统资源 |
void | write(byte[] b, int off, int len) | 将指定字节数组中从偏移量 off (起始位)开始的 len (写多长)个字节写入此输出流 |
void | write(byte[] b) | 将 b.length 个字节从指定的字节数组写入此输出流 |
案例,写数据:
public class Test1 {
public static void main(String[] args) throws Exception{
File f=new File("D:"+File.separator+"vvv.txt");
String s="i love java";
byte[] b=s.getBytes();
//OutputStream out=new FileOutputStream(f);
OutputStream out=new FileOutputStream(f,true);//多一个true参数,为追加效果
out.write(b);
out.close();
}
}
3、字符流
只能操作字符数据,和字节流类似
1.Reader
案例,读数据:
public class Test4 {
public static void main(String[] args) throws Exception{
File f=new File("D:"+File.separator+"vvv.txt");
Reader in=new FileReader(f);
char[] cs=new char[10240];
int len=in.read(cs);
in.close();
String s=new String(cs, 0, len);
System.out.println(s);
}
}
2.Writer
案例,写数据:
public class Test3 {
public static void main(String[] args) throws Exception{
File f=new File("D:"+File.separator+"vvv.txt");
Writer out=new FileWriter(f);
String s="i hate java";
out.write(s);
out.close();
}
}
3.BufferedReader
带缓冲区的Reader,其构造方法为:BufferedReader(Reader in)。常见方法为:
- String readLine():读取一个文本行。
案例,标准的键盘输入:
public static void main(String[] args) throws Exception{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入姓名:");
String s=in.readLine();//读取一行
System.out.println("-->"+s);
}
四、其它
1、打印流
打印流为PrintStream,使用如下:
public static void main(String[] args) throws Exception{
File f=new File("D:\\ttt.txt");
PrintStream ps=new PrintStream(f);
ps.println("vvv");
ps.close();
}
2、对象序列化
对象存在于内存中,不方便传输,对象的序列化就是为了方便对象传输的。其中:
- 序列化:把对象保存成文件【ObjectInputStream】
- 反序列化:把文件还原成对象【ObjectOutputStream】
案例,首先创建一个对象:
//实现Serializable,就可以被序列化
public class Student implements Serializable{
private int id;
private String name;
private String address;
//get\set
}
之后序列化:
public static void main(String[] args) throws Exception{
Student s=new Student();
s.setId(1);
s.setName("张三");
s.setAddress("河南郑州");
File f=new File("D:\\1.student");
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(s);
oos.close();
}
反序列化:
public static void main(String[] args) throws Exception{
File f=new File("D:\\1.student");
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(f));
Student s=(Student)ois.readObject();
System.out.println(s.getName()+"---"+s.getAddress());
}
说明:hibernate框架会用到序列化