Java.io包含多种可以对文件和目录进行读写等操作的类。主要分为输入流和输出流,因为传输格式的不同,又可分为字节流和字符流。字节流以字节为单位,字符流以字符为单位。
File类:
构造方法:File(String pathname)
其他方法:
boolean exists()
boolean isFile()
boolean isDirectory()
String getName()
String getAbsolutePath()
long length()
boolean createNewFile()
throws IOException
boolean delete()
File[] listFiles()
FileInputStream是文件输入流,继承于InputStream,它读取的是字节流。
FileOutputStream是文件输出流,即把数据从内存中输出,它继承于OutputStream。
FileInputStream两个常用的构造方法:
1.FileInputStream(File file)
throws FileNotFoundException
2.FileInputStream(String name)
throws FileNotFoundException
其他常用方法:
int read();
int read(byte[] b);
void read(byte b[],int off,int len)
void close();
FileOutputStream四个常用的构造方法:
1.FileOutputStream(File file);
2.FileOutputStream(File file, boolean append)
3.FileOutputStream(String name)
4.FileOutputStream(String name, boolean append);
其他常用方法:
void write(int b)
void write(byte[] b)
void close()
void write(byte b[],int off,int len)
其他常用方法:
int read();
int read(byte[] b);
void close();
将整型数值转换为字节型:
将字符串转换为字节型:
流在使用完之后要关闭,关闭顺序为先打开的后关闭,后打开的先关闭.