流整理

流的概念和分类

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();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值