1、重要的方法
File 类 分隔符 ---> File.separator
new File("路径") ---> 创建一个File对象
public boolean createNewFile() throws IOException ---> 判断文件是否存在,不存在将创建文件
public boolean delete() ---> 删除文件
public boolean exists() ---> 判断文件是否存在
public boolean mkdirs() ---> 创建文件(可创建多个文件夹),一般使用为 file.getParentFile().mkdirs();
public boolean mkdir() ---> 创建目录(单级)
public String getParent() ---> 获取文件路径,返回String
public File getParentFile() ---> 获取文件路径,返回对象
public boolean canRead() ---> 是否可读
public boolean canWrite() ---> 是否可写
public boolean canExecute() ---> 是否可执行
public File getAbsoluteFile() ---> 获取文件的绝对路径
public String getName() ---> 获取文件或者目录的名称
public boolean isFile() ---> 当前是否为文件
public boolean isDirectory() ---> 当前是否为目录
public long lastModified() ---> 返回文件最后一次修改的日期,返回时long
public long length() ---> 获取文件长度
------------------------------------------------------
输入输出流
字节流
InputStream 字节输入流、OutputStream 字节输出流、Writer 字符输出流、Reader 字符输入流
OutputStream
public abstract void write(int b) throws IOException ---> 输出一个字符数据
public void write(byte b[]) throws IOException ---> 输出全部字节数组的数据
public void write(byte b[], int off, int len) throws IOException ---> 输出部分字节数组的数据
public FileOutputStream(File file, boolean append) throws FileNotFoundException ---> append 为 true时为文件追加
InputStream
public int read() throws IOException ---> 读取一个字节
public int read(byte b[]) throws IOException ---> 读取全部字节数组,返回个数,没有的返回-1
public int read(byte b[], int off, int len) throws IOException ---> 读取部分字节数组
public long transferTo(OutputStream out) throws IOException ---> 自动转换InputStream和OutputStream
Writer
public void write(String str) throws IOException ---> 写入字符串
public void write(String str, int off, int len) throws IOException ---> 写入部分字符串
Appendable append(char c) throws IOException ---> 追加字符串
Reader
public int read(char cbuf[], int off, int len) throws IOException ---> 读取部分字符串
InputStream、OutputStream、Writer、Reader 都是Closeable 的子类其中又是 AutoCloseable的子类所以可以自动关闭
try (InputStream inputStream = new FileInputStream(file);){
代码
} catch (Exception e){
}
转换流 OutputStreamWriter、InputStreamReader
OutputStreamWriter
InputStreamReader
在java.io包中对内存操作流提供两类支持
public class ByteArrayInputStream extends InputStream
构造方法 ---> public ByteArrayInputStream(byte[] buf) 输入为字节数组
public class ByteArrayOutputStream extends OutputStream
构造方法 ---> public ByteArrayOutputStream()
*** public byte[] toByteArray() ----> 读取全部内容
随机读取文件 RandomAccessFile
public RandomAccessFile(File file, String mode) throws FileNotFoundException ----> mode为对文件操作的格式 r,w,d,s
重要方法
public void seek(long pos) throws IOException ----> 游标位置
public int read(byte b[], int off, int len) throws IOException ----> 读取部分
private native void writeBytes(byte b[], int off, int len) ----> 写入部分
其他方法用时查找源码
缓冲流
缓冲输入流
public class BufferedReader extends Reader
public String readLine() throws IOException ----> 读取一行
public class BufferedInputStream extends FilterInputStream
网络编程 ---- 了解
public class ServerSocket extends Object implements Closeable
public Socket accept() throws IOException ----> 用来连接socket
public class Socket extends Object implements Closeable
public Socket(String host, int port) throws UnknownHostException,IOException
---->连接服务器