Java IO 学习(二)---各种类的详细讲解
一,File类
1.概念:File 为文件和目录路径名的抽象表示。
抽象路径名由两部分组成:
- 可选系统有关的前缀字符串,如磁盘驱动符:/为unix根目录,\为Windows的根目录。
- 零个或多个字符串的序列
2.构造函数
-
-
File(File parent, String child)
Creates a new
File
instance from a parent abstract pathname and a child pathname string.
译:从父抽象路径名和子路径名字符串创建新的
File
实例。File(String pathname)
Creates a new
File
instance by converting the given pathname string into an abstract pathname.
译:通过将给定的路径名字符串转换为抽象路径名来创建新的
File
实例。File(String parent, String child)
Creates a new
File
instance from a parent pathname string and a child pathname string.
译:从父路径名字符串和子路径名字符串创建新的
File
实例。File(URI uri)
Creates a new
File
instance by converting the givenfile:
URI into an abstract pathname.
译:通过将给定的
file:
URI转换为抽象路径名来创建新的File
实例。
-
3.常用方法
方法名称 | 说明 |
---|---|
boolean canRead() | 测试应用程序是否能从指定的文件中进行读取 |
boolean canWrite() | 测试应用程序是否能写当前文件 |
boolean delete() | 删除当前对象指定的文件 . |
boolean exists() | 测试当前 File 是否存在 |
String getAbsolutePath() | 返回由该对象表示的文件的绝对路径名 |
String getName() | 返回表示当前对象的文件名 |
String getParent() | 返回当前 File 对象路径名的父路径名,如果此名没有父路径则为 null |
boolean isAbsolute() | 测试当前 File 对象表示的文件是否为一个绝对路径名 |
boolean isDirectory() | 测试当前 File 对象表示的文件是否为一个路径 |
boolean isFile() | 测试当前 File 对象表示的文件是否为一个“普通”文件 |
long lastModified() | 返回当前 File 对象表示的文件最后修改的时间 |
long length() | 返回当前 File 对象表示的文件长度 |
String[] list() | 返回当前 File 对象指定的路径文件列表 |
String[] list(FilenameFilter) | 返回当前 File 对象指定的目录中满足指定过滤器的文件列表 |
boolean mkdir() | 创建一个目录,它的路径名由当前 File 对象指定 |
boolean mkdirs() | 创建一个目录,它的路径名由当前 File 对象指定 |
boolean renameTo(File) | 将当前 File 对象指定的文件更名为给定参数 File 指定的路径名 |
package day6;
import java.io.File;
import java.io.IOException;
public class FileCreate {
public static void main(String[] args) {
//1.按string pathname创建
File file=new File("F:\\test\\file1");
File file2=new File("f:\\test\\file1.txt");
//2.按file 和string创建
File file3=new File(file, "a.txt");
boolean a=file3.exists();
System.out.println(a);
String stringfile3=file3.getAbsolutePath();
System.out.println(stringfile3);
//3.按string string 的方式创建
File file4=new File("f:\\test\\file", "adbuawdw.txt");
System.out.println("****");
boolean bb=file3.exists();
System.out.println(a);
String stringfile4=file4.getAbsolutePath();
System.out.println(stringfile4);
System.out.println("***");
System.out.println(file4.length());
try {
boolean bo=file.createNewFile();//创建一个file的新文件(没有的话创建,有得话不动)
boolean bo2=file.canExecute(); //是否能被执行
boolean bo3=file.canWrite(); //是否能被写入
int comp=file.compareTo(file2); //与另一个比较
file.delete(); //删除文件
file.deleteOnExit();//删除并退出
String string=file.getParent();
long longs=file.length(); //一个中文字符是2个字节
boolean b=file.mkdir();
String []strings=file.list(); //列出本路径下的所有文件
for(int i=0;i<strings.length;++i) {
System.out.println(strings[i]);
}
System.out.println("\r\n");
File []files=file.listFiles();
for(int i=0;i<strings.length;++i) {
System.out.println(strings[i]);
}
boolean isd=file.isDirectory(); //判断是否为目录
System.out.println(isd); //判断是否为文件
boolean isf=file.isFile();
System.out.println(isf);
System.out.println(longs);
System.out.println(string);
System.out.println(comp);
System.out.println(bo);
System.out.println(bo2);
System.out.println(bo3);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
二,字节流
Inputstream是所有字节流的超类,说是一个抽象类,应此所有继承他的子类都要重新定义抽象方法。
1.字节输入流Inputstream 的源码
package java.io;
import java.util.Arrays;
import java.util.Objects;
public abstract class InputStream implements Closeable {
// MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to
// use when skipping.
private static final int MAX_SKIP_BUFFER_SIZE = 2048;
private static final int DEFAULT_BUFFER_SIZE = 8192;
public abstract int read() throws IOException;
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
public int read(byte b[], int off, int len) throws IOException {
Objects.requireNonNull(b);
Objects.checkFromIndexSize(off, len, b.length);
if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
public byte[] readAllBytes() throws IOException {
byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
int capacity = buf.length;
int nread = 0;
int n;
for (;;) {
// read to EOF which may read more or less than initial buffer size
while ((n = read(buf, nread, capacity - nread)) > 0)
nread += n;
// if the last call to read returned -1, then we're done
if (n < 0)
break;
// need to allocate a larger buffer
if (capacity <= MAX_BUFFER_SIZE - capacity) {
capacity = capacity << 1;
} else {
if (capacity == MAX_BUFFER_SIZE)
throw new OutOfMemoryError("Required array size too large");
capacity = MAX_BUFFER_SIZE;
}
buf = Arrays.copyOf(buf, capacity);
}
return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
}
public int readNBytes(byte[] b, int off, int len) throws IOException {
Objects.requireNonNull(b);
Objects.checkFromIndexSize(off, len, b.length);
int n = 0;
while (n < len) {
int count = read(b, off + n, len - n);
if (count < 0)
break;
n += count;
}
return n;
}
public long skip(long n) throws IOException {
long remaining = n;
int nr;
if (n <= 0) {
return 0;
}
int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
byte[] skipBuffer = new byte[size];
while (remaining > 0) {
nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
if (nr < 0) {
break;
}
remaining -= nr;
}
return n - remaining;
}
public void close() throws IOException {}
public synchronized void mark(int readlimit) {}
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
public boolean markSupported() {
return false;
}
public long transferTo(OutputStream out) throws IOException {
Objects.requireNonNull(out, "out");
long transferred = 0;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int read;
while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
out.write(buffer, 0, read);
transferred += read;
}
return transferred;
}
}
2.InputStream 类的常用子类如下。
- ByteArrayInputStream 类:将字节数组转换为字节输入流,从中读取字节。
- FileInputStream 类:从文件中读取数据。
- PipedInputStream 类:连接到一个 PipedOutputStream(管道输出流)。
- SequenceInputStream 类:将多个字节输入流串联成一个字节输入流。
- ObjectInputStream 类:将对象反序列化。
三,字符流