流的概念和分类
1.按照数据流方向分为输入流,输出流
2.处理单位不同分为字节流和字符流
3.功能不同分为节点流和处理流
字节输入流 inputstream 字节输出流 outputstream
字符输入流 reader 字符输出流writer
4一个重要的类 File
listRoots();
getFreeSpace();
getTotalSpace();
file.exits();
file.isfile();
file.canRead();
file.canWrite();
file.isHidden();
file.canExcute();
file.getAbsolutePath();
file.getParents();
file.length();
file.lastModified();
file.siDirectory();
file.listFiles();返回File[]
file.getpath();
获得根目录磁盘
File[] disks=File.listRoots();
for(int i=0;i<disks.length;i++){
File disk=disks[i];
System.outprint("磁盘的名称是:"+disk+"\t");
long g=1024*1024*1024;
System.outprint("总空间大小是:"+dist.getTotalSpace()/g+"G\t");
System.outprint("剩余空间大小是:"+dist.getFreeSpace()/g+"G\t");
递归循环输出目录下所有文件
/**
* 使用递归
* @param files 文件夹下的所有文件集合
*/
public static void openFile(File[]files){
for(File file : files) {
System.out.println(file.getPath());
if((!file.getName().equals("$RECYCLE.BIN"))&&file.isDirectory()){
openFile(file.listFiles());
}
}
}5.1.字节输入流inputStream
//向输出流中写入一个字节数据,该字节数据为参数b的低8位
void write(int b) throws IOException
//将一个字节类型的数组中的数据写入输出流
void write(byte[] b) throws IOException
//将一个字节类型的数组中的从指定位置(off)开始的
//len个字节写入到输出流
void write(byte[] b, int off, int len)
throws IOException
//关闭流释放内存资源
void close() throws IOException
//将输出流中缓冲的数据全部写出到目的地
void flush() throws IOException
程序
File file = new File("C:/FileTest/a.txt");
InputStream input = new FileInputStream(file);
int i;
//读取一个字节并以整数的形式返回(0~255),
//如果返回-1已到输入流的末尾。
while((i=input.read())!=-1){
System.out.println((char)i);
}
input.close();
byte[] bs = new byte[(int)file.length()];
input.read(bs);
input.close();
String s = new String(bs);
System.out.println(s);
input.close();
outPutStream 字节输出流
//向输出流中写入一个字节数据,该字节数据为参数b的低8位
void write(intb) throwsIOException
//将一个字节类型的数组中的数据写入输出流
void write(byte[] b) throws IOException
//将一个字节类型的数组中的从指定位置(off)开始的
//len个字节写入到输出流
void write(byte[] b, intoff, int len)
throws IOException
//关闭流释放内存资源
void close() throws IOException
//将输出流中缓冲的数据全部写出到目的地
void flush() throws IOException
输出中文
String s="包含的字符串";
output.(s.getBytes());
out.flush();
out.close();
Reader
//读取一个字符并以整数的形式返回(0 到 65535),
//如果返回-1已到输入流的末尾。
int read() throws IOException
//读取一系列字符并存储到一个数组buffer,
//返回实际读取的字符数,如果读取前已到输入流的末尾返回-1
int read(char[] cbuf) throws IOException
//读取length个字符
//并存储到一个数组buffer,从off位置开始存,最多读取len
//返回实际读取的字符数,如果读取前以到输入流的末尾返回-1
int read(char[] cbuf, int off, int len)
throws IOException
//关闭流释放内存资源
void close() throws IOException
//文件输入流
File file = new File("C:/FileTest/b.txt");
Reader reader = new FileReader(file);
int i;
while((i=reader.read())!=-1){
System.out.print((char)i);
}
reader.close();
writer
//向输出流中写入一个字符数据,该字节数据为参数b的低16位
void write(int c) throws IOException
//将一个字符类型的数组中的数据写入输出流,
void write(char[] cbuf) throws IOException
//将一个字符类型的数组中的从指定位置(offset)开始的
//length个字符写入到输出流
void write(char[] cbuf, int offset, int length)
throws IOException
//将一个字符串中的字符写入到输出流
void write(String string) throws IOException
//将一个字符串从offset开始的length个字符写入到输出流
void write(String string, int offset, int length)
throws IOException
//关闭流释放内存资源
void close() throws IOException
//将输出流中缓冲的数据全部写出到目的地
void flush() throws IOException
程序
//文件输出流
File file = new File("C:/FileTest/c.txt");
Writer fw = new FileWriter(file);
fw.write("你好");
fw.flush();
fw.close();
COPY文件到另一个
节点流
处理流
使用字符流COPY文件
Filefile = new File("c:/FileTest/duo.txt");
File dest = newFile("c:/FileTest/dest.txt");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(dest);
System.out.println(new Date());
int i;
while((i=fr.read())!=-1){
fw.write(i);
}
System.out.println(new Date());
fw.flush();
fw.close();
fr.close();