文章目录
Java流的分类
按数据传输单位分:
-
字节流:
以字节为单位传输数据的流,8位
InputStream
和OutputStream
-
字符流:
以字符为单位传输数据的流,16位
Reader
和Writer
按流向分
输入流:
Java打开一个数据源到程序的流,从流中读取数据输出流:
把程序写入流中
都是单向通道
InputStream
inputstream类和outputstream类都为抽象类,不能创建对象,可以通过子类来实例化。
public abstract int read( ):
读取一个byte的数据,返回值是高位补0的int类型值
public int read(byte b[ ]):
读取b.length个字节的数据放到b数组中。返回值是读取的字节数。该方法实际上是调用下一个方法实现的
public int read(byte b[ ], int off, int len):
从输入流中最多读取len个字节的数据,存放到偏移量为off的b数组中
public int available( ):
返回输入流中可以读取的字节数。注意:若输入阻塞,当前线程将被挂起,如果InputStream对象调用这个方法的话,它只会返回0,这个方法必须由继承InputStream类的子类对象调用才有用
public long skip(long n):
忽略输入流中的n个字节,返回值是实际忽略的字节数, 跳过一些字节来读取
public int close( ) :
我们在使用完后,必须对我们打开的流进行关闭
OutputStream
public void write(byte b[ ]):
将参数b中的字节写到输出流。
public void write(byte b[ ], int off, int len) :
将参数b的从偏移量off开始的len个字节写到输出流。
public abstract void write(int b) :
先将int转换为byte类型,把低字节写入到输出流中。
public void flush( ) :
将数据缓冲区中数据全部输出,并清空缓冲区。
public void close( ) :
关闭输出流并释放与流相关的系统资源。
File类
- java.io包
- Java使用File创建对象来获取文件本身的信息:文件所在目录,文件长度,文件读写权限等
- !!!不能对文件内容进行读写
构造方法
public File(String name)
File file1 = new File(“D:\data.txt”);
键盘输入 D:/data.txt(使用/)
File file2 = new File(“data”);
jdk创建在源程序里
File(String parent,String child)
代表父目录和子目录
File f = new File(“D:\enjoy”,”data”);指向目录(路径)
File f2 = new File(“D:\enjoy”,”data.txt”)指向文件
File(File parent , String child)
File f = new File(“D:\enjoy”);
File f2 = new File(file1,”data.txt”)
成员方法
public String getName()
File dir = new File(“D:\Java”);
String fileName[] = dir.list();
public String getPath()
public Stirng getAbsolutePath()
public String[] list()
字符串数组返回File对象对应目录的所有子目录和文件名
public File[] listFiles()
File对象形式返回目录全部文件
File dir = new File("D:/Java");
File fileName[] = dir.listFiles();
s = null;
for(int i = 0 ; i < fileName.length ;i++)
{
if(fileName[i].getName().endsWith(".txt"))
{
if(s == null)
s = fileName[i];//返回第一个文件
System.out.println(fileName.getName());
}
}
public String[] list(FilenameFilter obj)
字符串形式返回指定类型文件
public File[] listFiles(FilenameFilter obj)
File对象返回目录指定类型文件
class FileAccept implements FilenameFilter//实现过滤接口
{
String str = null;
FileAccept(String s)
{
str = "."+s;
}
public boolean accept(File dir,String name)
{
return name.endsWith(str);
}
}
....
File dir = new File("D:/Java");
FileAccept acceptCodition = new FileAccept("txt");
File fileName[] = dir.listFiles(acceptCondition);
for(int i = 0;i < fileName.length;i++)
{
System.out.printf("\n文件名称%s,长度%d",fileName[i].getName(),fileName[i].length());
}
文件性质获取
fileName.endsWith(".txt")
判断文件类型
文件字节流
FileInputStream
构造方法
public FileInputStream(String name)
FileInputStream in = new FileInputStream(“a.txt”);
public FileInputStream(File file)
File file = new File(“data\b.txt”);
FileInputStream in = new FileInputStream(file);
常用方法
public int read() throws IOException
public int read(byte[] b) throws IOException
public int read(v=byte[] b,int off,int len) throws IOException
存在数组b里 off 从b的下标off开始存 len 读取数据字节个数
文件不存在 / 损坏 throws 异常
读取到文件末尾 返回-1
FileOutputStream
构造方法
public FileOutputStream(String name)
FileOutputStream out = new FileOutputStream(“a.txt”);
public FileOutptStream(File file)
常用方法
public void write(byte[] b)throws IOException
b中的内容写入
public void write(byte[] b,int off,int len)throws IOException
b中从off开始的len个字节的内容
顺序写内容,文件已存在 ,内容被覆盖;文件不存在,文件被创建
byte[] c = s.getBytes();
out.write( c );//写入一组
out.write((int)’\r’);//写入一个
FileOutputStream out = new FileOutputStream(f,true);
默认为false,即覆盖输入
true为追加输入
FileReader
操作Unicode,每个字符占两个字节
构造方法
FileReader(String name)
FileReader in = new FileReader(“a.txt”);
FileReader(File file)
File file = new File(“data\b.txt”);
FileReader in = new FileReader(file);
read() throws IOException
read(char[] cbuf) throws
FileWriter(String name)
FileWriter out = new FileWriter
关闭前flush,保证全部写入
write()写入时文件损坏则抛出异常
字符缓冲流
内存作为源或者目的地的流-内存流
字符缓冲流
文件-文件输入流(源)-缓冲输入流-内存(目的地)
内存(源)-缓冲输出流-文件输出流(目的地)
BufferedReader
FileReader One = new FIleReader(“a1.txt”);
BufferedReader Two = new BufferReader(One);
readLine()读一行
构造方法
FileWriter One = new FIleWriterr(“a1.txt”);
BufferedWriter Two = new BufferWriter(One);
newLine()增加空行
关闭顺序:
缓冲输入流- 输入流-缓冲输出流-输出流
对象流
对象输入流
构造方法
FileInputSream a = new FileInputStream(“tom。txt”)
ObjectInputStream in = new ObjectInputStream(a)
readObject 读出的是根类对象,有时候需要强制转换为子类对象
对象输出流
writeObject
一般结尾写一个x.writeObject(null)有利于再次打开文件时判定是否读完文件
先关闭对象,再关闭文件
Serializabla 序列化接口 不用写出任何方法
随机读写
RandomAccessFile 双向流既可读文件,又可写文件
RandomFile(String name,String mode)