java IO 学习记录

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

---->连接服务器

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yzzzjj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值