java.io.File 类
1、凡是与输入、输出相关的类、接口等都定义在Java.io 包下
2、File是一个类,可以有构造器创建其对象。此对象对应着一个文件(.txt .avi .doc .ppt .mp3)或文件目录
3、File类对象是与平台无关的。
4、File中的方法,仅涉及到如何创建、删除、重命名等。对于内容,必须用IO流完成。
5、File类的对象常作为io流的具体类的构造器的形参。
6、File的静态属性String separator存储了当前系统的路径分隔符。在UNIX中,此字段为‘/’,在Windows中,为‘\’
File类下的方法
访问文件名:
getName()
getPath()
getAbsoluteFile()
getAbsolutePath()
getParent()
renameTo(File newName) 重命名为//file1.renameTo(file2):file1重命名为file2.要求:file1文件一定存在,file2一定不存在
文件检测
exists()
canWrite()
canRead()
isFile()
isDirectory()
获取常规文件信息
lastModified()
length()
文件操作相关
createNewFile()
delete()
目录操作相关
mkDir() 创建一个文件目录。只有在上层文件目录存在的情况下,才能返回true
mkDirs() 创建一个文件目录。若上层文件目录不存在,一并创建
list()
listFiles()
Java IO原理
什么是标准的I/O流?
在java语言中,用stdin表示键盘,用stdout表示监视器。他们均被封装在System类的类变量in 和out中,对应于系统调用System.in和System.out。这样的两个流加上System.err统称为标准流,它们是在System类中声明的3个类变量:
public static InputStream in
publicstaticPrintStream out
public static PrintStream err
流的分类
- 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
- 按数据流的流向不同分为:输入流,输出流
- 按流的角色的不同分为:节点流,处理流


节点流可以从一个特定的数据源读写数据
处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。
IO 流体系

只有 FileInputStream、FileReader、FileOutputStream、FileWriter 是节点流,下面的都是处理流
程序中打开的文件 IO 资源不属于内存里的资源,垃圾回收机制无法回收该资源,所以应该显式关闭文件 IO 资源。
主要流介绍
字节流 FileInputStream、FileOutputStream
public void testFileInputStream1() throws IOException{
File file=new File("Hello.txt");
FileInputStream fis=new FileInputStream(file);
int b;
while((b=fis.read())!=-1){
System.out.println((char)b);
}
fis.close();
}
public void copyFile(String src,String dest){
File file1=new File(src);
File file2=new File(dest);
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream(file1);
fos=new FileOutputStream(file2);
byte[] b=new byte[20];
int len;
while((len=fis.read(b))!=-1){
fos.write(b, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
字符流:FileReader和FileWriter
public void testFileReaderWriter(){
FileReader fr=null;
FileWriter fw=null;
try{
File src=new File("Hello.txt");
File dest=new File("Hello1.txt");
fr=new FileReader(src);
fw=new FileWriter(dest);
char[] c=new char[24];
int len;
while((len=fr.read(c))!=-1){
fw.write(c,0,len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(fw!=null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fr!=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
缓冲流->属于处理流->能提高效率
字节缓冲流 BufferedInputStream、BufferedOutputStream
字符缓冲流 BufferedReader、BufferedWriter
@Test
public void testCopyFile() {
File file1 = new File("1.png");
File file2 = new File("2.png");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
bos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
public void testBufferedReader(){
BufferedReader br=null;
BufferedWriter bw=null;
try {
File file=new File("Hello.txt");
File file1=new File("Hello5.txt");
FileReader fr=new FileReader(file);
FileWriter fw=new FileWriter(file1);
br = new BufferedReader(fr);
bw=new BufferedWriter(fw);
String str=null;
while((str=br.readLine())!=null){
System.out.println(str);
bw.write(str);
bw.newLine();
bw.flush();
}
}catch (IOException e) {
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
如何实现字符流与字节流之间的转化?
转换流:InputStreamReader OutputStreamWriter
编码:字符串-》字符数组
解码:字符数组-》字符串
转换流的编码应用
可以将字符按指定编码格式存储。
可以对文本数据按指定编码格式来解读。
指定编码表的动作由构造器完成。
public void test1(){
File file=new File("Hello.txt");
BufferedReader br=null;
BufferedWriter bw=null;
try {
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis,"GBK");
br = new BufferedReader(isr);
File file1=new File("Hello6.txt");
FileOutputStream fos=new FileOutputStream(file1);
OutputStreamWriter osw= new OutputStreamWriter(fos,"GBK");
bw = new BufferedWriter(osw);
String str=null;
while((str=br.readLine())!=null){
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
标准的输入输出流
标准的输出流:System.out
标准的输入流;System.in
public void test2(){
BufferedReader br=null;
try {
InputStream is=System.in;
InputStreamReader isr=new InputStreamReader(is);
br = new BufferedReader(isr);
String str;
while(true){
System.out.println("请输入字符串");
str=br.readLine();
if(str.equalsIgnoreCase("e")||str.equalsIgnoreCase("exit")){
break;
}
String str1=str.toUpperCase();
System.out.println(str1);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
打印流
PrintStream(字节打印流)和PrintWriter(字符打印流)
提供了一系列重载的print和println方法,用于多种数据类型的输出
PrintStream和PrintWriter的输出不会抛出异常
PrintStream和PrintWriter有自动flush功能
System.out返回的是PrintStream的实例
@Test
public void printStreamWriter(){
FileOutputStream fos=null;
try {
fos=new FileOutputStream(new File("print.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
PrintStream ps=new PrintStream(fos,true);
if(ps!=null){
System.setOut(ps);
}
for(int i=0;i<=255;i++){
System.out.print((char)i);
if(i%50==0){
System.out.println();
}
}
ps.close();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
数据流
为了方便地操作Java语言的基本数据类型的数据,可以使用数据流。
@Test
public void testData(){
DataOutputStream dos=null;
try {
FileOutputStream fos=new FileOutputStream("data.txt");
dos = new DataOutputStream(fos);
dos.writeUTF("我爱你");
dos.writeBoolean(true);
dos.writeLong(454646656);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(dos!=null){
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testData1(){
DataInputStream dis=null;
try {
dis=new DataInputStream(new FileInputStream(new File("data.txt")));
String str=dis.readUTF();
System.out.println(str);
boolean b=dis.readBoolean();
System.out.println(b);
long l=dis.readLong();
System.out.println(l);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(dis!=null){
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
对象流
ObjectInputStream和OjbectOutputSteam
用于存储和读取对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
对象序列化(Serialize):允许把内存中的Java对象转换成平台无关的二进制字节流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象
反序列化(Deserialize):用ObjectInputStream类从IO流中恢复该Java对象
ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
凡是实现Serializable接口的类都有一个表示序列化版本标识符的静态变量:
- private static final long serialVersionUID;
- serialVersionUID用来表明类的不同版本间的兼容性
- 如果类没有显示定义这个静态变量,它的值是Java运行时环境根据类的内部细节自动生成的。若类的源代码作了修改,serialVersionUID 可能发生变化。故建议,显示声明
public class TestObjextInputOutputStream {
@Test
public void testObjextInputStream(){
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream("person.txt"));
Person p1=(Person) ois.readObject();
System.out.println(p1);
Person p2=(Person) ois.readObject();
System.out.println(p2);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if(ois!=null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testObjectOutputStream(){
Person p1=new Person("小明",23);
Person p2=new Person("红米",21);
ObjectOutputStream oos=null;
try {
oos = new ObjectOutputStream(new FileOutputStream("person.txt"));
oos.writeObject(p1);
oos.flush();
oos.writeObject(p2);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(oos!=null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class Person implements Serializable{
private static final long serialVersionUID=45646;
String name;
Integer age;
public Person(String name,Integer age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
RandomAccessFile 类
RandomAccessFile 类支持 “随机访问” 的方式,程序可以直接跳到文件的任意地方来读、写文件
- 支持只访问文件的部分内容
- 可以向已存在的文件后追加内容
RandomAccessFile 对象包含一个记录指针,用以标示当前读写处的位置。RandomAccessFile 类对象可以自由移动记录指针:
- long getFilePointer():获取文件记录指针的当前位置
- void seek(long pos):将文件记录指针定位到 pos 位置
构造器
public RandomAccessFile(File file, String mode)
public RandomAccessFile(String name, String mode)
- r: 以只读方式打开
- rw:打开以便读取和写入
- rwd:打开以便读取和写入;同步文件内容的更新
- rws:打开以便读取和写入;同步文件内容和元数据的更新
@Test
public void test1(){
RandomAccessFile raf1=null;
RandomAccessFile raf2=null;
try {
raf1=new RandomAccessFile(new File("hello.txt"), "r");
raf2=new RandomAccessFile(new File("hello1.txt"), "rw");
byte[] b=new byte[20];
int len;
while((len=raf1.read(b))!=-1){
raf2.write(b, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(raf2!=null){
try {
raf2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(raf1!=null){
try {
raf1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test2(){
RandomAccessFile raf=null;
try {
raf=new RandomAccessFile(new File("hello1.txt"), "rw");
raf.seek(3);
raf.write("xy".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(raf!=null){
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test3(){
RandomAccessFile raf=null;
try {
raf=new RandomAccessFile(new File("hello1.txt"), "rw");
raf.seek(4);
String str=raf.readLine();
raf.seek(4);
raf.write("xy".getBytes());
raf.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(raf!=null){
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}