说不上技术分享,只能算是学习记录
I/O
File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import java.io.File; import java.io.IOException;
public class Demo01 { public static void main(String[] args) {
File file = new File("hello.txt");
System.out.println("相对路径:"+file.getPath() ); System.out.println("绝对路径:"+file.getAbsolutePath()); System.out.println("文件名称:"+file.getName() ); System.out.println("文件大小:"+file.length() );
System.out.println( file.isFile()==true? "文件":"非文件" ); System.out.println( file.isDirectory()==true? "目录":"非目录" ); boolean flag = file.exists(); try { if(flag) {
System.out.println("删除成功"); } else { file.createNewFile(); System.out.println("创建成功"); }
} catch (IOException e) { e.printStackTrace(); } } }
|
相对路径/绝对路径:
- 如果FIle("绝对路径"):getPath() 和getAbsolutePath()的结果一致,打印的都是绝对路径
- 如果FIle("相对路径"):getPath() 打印相对路径;getAbsolutePath()打印绝对路径
流
流:是一种FIFO的数据结构
说明:1.字节流就是 将内容转为了字节形式进行传输, 1 字节 ->8二进制
,二进制可以传输任何类型的数据,因此字节流也可以传输任何类型的数据。
2.字节流是8位通用字节流( 1 字节 ->8二进制 )
(字节流可以处理任何类型,处理文本文件以外的其他文件)
;字符流是16位的unicode字符流 (只用于处理字符,处理文本文件)
3.在while循环中 分批次传输时,字节流使用的缓冲区是
byte[],字符流使用的缓冲区是 char[]
输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream;
public class InputStreamDemo { public static void main(String[] args) { InputStream in = null ; try { in = new FileInputStream(new File("d:/abc.txt"));
System.out.println(in.available()); byte[] buf = new byte[in.available()] ; in.read(buf); System.out.println(new String(buf)); }catch (Exception e){ e.printStackTrace(); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
|
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream;
public class OutputStreamDemo {
public static void main(String[] args) { OutputStream out = null ; try { out = new FileOutputStream("d:/xyz.txt") ; out.write("helloworld123".getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
|
文件复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import java.io.*;
public class FileCopy { public static void main(String[] args) { InputStream in = null ; OutputStream out = null ; try { in = new FileInputStream("d:/abc.txt") ;
out = new FileOutputStream("d:/xyz.txt") ; byte []buf = new byte[10] ; int len = -1 ; while((len= in.read(buf)) != -1){ out.write(buf,0,len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { if(out !=null)out.close(); if(in !=null) in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
|
使用字符流进行文件的复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| import java.io.*;
public class FileCopyCharactor { public static void main(String[] args) { Reader reader = null ; Writer writer = null ; try { reader = new FileReader("d:/个人介绍.txt"); writer = new FileWriter("d:/个人完整介绍.txt") ;
char[] buf = new char[4]; StringBuffer sb = new StringBuffer() ;
int len = -1 ; while((len =reader.read( buf)) != -1 ){ sb.append(buf,0,len) ; }
System.out.println(sb);
String content = sb.toString() ; content= content.replace("{name}","颜群") .replace("{enterprise}","蓝桥学院") .replace("{weixin}","157468995");
writer.write(content); System.out.println("成功...");
} catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { if(writer!=null) writer.close(); if(reader!=null) reader.close(); } catch (IOException e) { e.printStackTrace(); }
} } }
|
自带缓冲区的字符流
缓冲区大小,一行
BufferedReader /BufferedWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import java.io.*;
public class FileCopyCharactorBuffered { public static void main(String[] args) { Reader reader = null ; Writer writer = null ;
BufferedReader br = null ; BufferedWriter bw = null ;
try { reader = new FileReader("d:/个人介绍.txt"); writer = new FileWriter("d:/个人完整介绍2.txt") ;
br = new BufferedReader( reader) ; bw = new BufferedWriter( writer);
StringBuffer sb = new StringBuffer() ;
String line = null ;
while( (line= br.readLine()) != null ){ sb.append(line) ; }
System.out.println(sb);
String content = sb.toString() ; content= content.replace("{name}","颜群") .replace("{enterprise}","蓝桥学院") .replace("{weixin}","157468995");
bw.write(content); System.out.println("成功...");
} catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally {
try { if(bw != null) bw.close(); if(br!=null ) br.close(); if(writer!=null) writer.close(); if(reader!=null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
|
带缓冲区的字符流 对象 = new 带缓冲区的字符流(字符流) ;
二进制流
DataInputStream/DataOutputStream
二进制流 对象 = new 二进制流(字节流) ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import java.io.*;
public class FileCopyData { public static void main(String[] args) { InputStream in = null ; OutputStream out = null ; InputStream dataInput = null ; OutputStream dataOutput = null ; try { in = new FileInputStream("d:/IMG_6285.JPG") ;
dataInput = new DataInputStream(in) ;
out = new FileOutputStream("e:/颜群微信3.jpg") ; dataOutput = new DataOutputStream(out );
byte []buf = new byte[10] ; int len = -1 ;
while( (len= dataInput.read(buf)) != -1){ dataOutput.write(buf,0,len); } System.out.println("成功");
} catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { if(dataOutput !=null) dataOutput.close(); if(dataInput !=null) dataInput.close(); if(out !=null)out.close(); if(in !=null) in.close(); } catch (IOException e) { e.printStackTrace(); } } } }
|
装饰模式:IO设计的核心思想
在不影响原有对象的前提下,无侵入的给一个对象增一些额外的功能。
new InputStreamReader( new FileInputStream(new File("d:/abc.txt")) )
;
参考
https://www.bilibili.com/video/BV114411W7id?p=56
总结
I/O基础还是很重要的。