java基础(文件)随记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ITw333/article/details/80323650
<File类>
1.创建File对象的几种方式
例:路径为c:\\www\\io\\score.txt
a.  File file1 = new File("c:\\www\\io\\score.txt");
b.  File file1 = new File("c:\\www","io\\score.txt");
c.  File file = new File("c:\\www"); File file1 = new File(file,"io\\score.txt");
以上三种方式均为  是否是目录file1.isFirectory() false  是否是文件file1.isFile()  ture
如果路径不存在,均为false
2. 创建目录
File file1 = new File("c:\\www","io");
if(!file1.exists()){
 file1.mkdir() //创建一级目录
}
File file1 = new File("c:\\www","io\\set");
if(!file1.exists()){
 file1.mkdirs() //创建多级目录
}
3.创建文件
File file1 = new File("c:\\www\\io\\score.txt");
if(!file1.exists()){
 file1.creatNewFile() //创建文件,需要捕获异常
}
<FileInputStream和FileOutputStream>
1.返回值为-1时读到文件末尾
2.FileInputStream(文件路径/文件描述符/文件名) read()读取一个字符  read(byte[] b)  read(byte[] b,int off,int len),从b数组的off位置开始读取,读取len长度的字节,close()关闭文件
例:FileInputStream fis = new FileInputStream("文件路径"); //需要处理文件不存在异常
//int n=fis.read();  //需要处理IO异常
int n=0;
while((n=fis.read())!=-1)
{
 System.out.print((char)n);
}
fis.close();
例:FileInputStream fis = new FileInputStream("文件路径");
byte[] b = new byte[1024];
fis.read(b,0,5);  //从第0位开始,读取五个字节
System.out.print(new String(b));
fis.close();
3.FileOutputStream(文件路径/文件描述符/文件名) write(int b)将指定字节写入文件  write(byte[] b) write(byte[] b,int off,int len),从b数组的off位置开始写入,写入len长度的字节),close()关闭文件
例:FileOutputStream fos = new FileOutputStream(文件路径/文件描述符/文件名)//需要处理文件不存在异常
fos.write(50);/需要处理IO异常,注意:50写入会乱码,但不影响读取
fos.write('a');
fos.close();
此时写入文件会覆盖,如果不想被覆盖,而是追加打末尾 则FileOutputStream fos = new FileOutputStream(文件路径/文件描述符/文件名,ture)
4.不适合字符,适合二进制,可用于图片或者动图的拷贝
例:FileInputStream fis = new FileInputStream("happy.jpg");
FileOutputStream fos = new FileOutputStream("happycopy.jpg");
int n=0;
byte[] b = new byte[1024];
while((n=fis.read(b))!=-1){//unsigned byte [0 - 255] (byte)128=-128 (byte)129=-127 (byte)255=-1
 fos.write(b);
}
fis.close();
fos.close();
注意:上面的复制的图片的大小会比原来的大,更改:将while()循环内改为fos.write(b,0,n);
<缓存流BufferedInputStream和BufferedOutputStream>
1.缓冲区满了,自动执行写操作,不满需要调用flush()函数强制清空,写入文件
2.例:FileOutputStream fos = new FileOutputStream("www.txt");//处理异常
BufferedOutputStream bos = new BufferedOutputStream(fos);
FileInputStream fis = new FileInputStream("www.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
bos.write(50);
bos.write('a');
bos.flush();
System.out.print(bis.read());
System.out.print((char)bis.read);
fos.close();
bos.close();//调用flush或者close均可清空缓冲区
fis.close();
bis.close();
<字节字符转换流InputStreamReader和OutputStreamWriter>
1.例:FileInputStream fis = new FileInputStream("www.txt");//可在括号中添加编码方式
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
FileOutputStream ois = new FileOutputStream("www1.txt");
OutputStreamWriter osw = new OutputStreamWriter(ois);//可在括号中添加编码方式
BufferWriter bw = new BufferWriter(osw);
int n=0;
char[] cbuf = new char[10];
/*while((n=isr.read())!=-1){
 System.out.print((char)n);
}*/
while(n=isr.read(cbuf))!=-1){
 String s = new String(cbuf,0,n);
 System.out.print(s);
}
/*while(n=isr.read(cbuf))!=-1){
 String s = new String(cbuf,0,n);
 osw.writer(str);
}*/
while(n=isr.read(cbuf))!=-1){
 osw.writer(cbuf,0,n);
 osw.flush();
}
//带缓冲流的读写
while((n=br.read(cbuf))!=1){
 bw.writer(cbuf,0,n);
 bw.flush();
}
fis.close();
fos.close();
isr.close();
osw.close();
br.close();
bw.close();

猜你喜欢

转载自blog.csdn.net/ITw333/article/details/80323650