对 IO,NIO的简单操作,详细看如下代码
1、InputStream,OutputStream是对字节流的操作,Reader,Writer是对字符的操作
2、对大文件的拷贝使用RandomAccessFile类和NIO
1 import java.io.*; 2 import java.nio.ByteBuffer; 3 import java.nio.channels.FileChannel; 4 5 public class IOWriteRead { 6 7 public static void main(String[] args) { 8 // 一行一行读取文件内容 9 //readFile("d:\\a.txt"); 10 // 以字符的方式读取a文件写入到b文件 11 readFileByReader("d:\\a.txt","d:\\c.txt"); 12 // 以字节的方式读取a文件写入到b文件 13 //readFileByStream("d:\\a.txt","d:\\c.txt"); 14 //拷贝a文件到b文件 15 //copyFile("d:\\a.txt","d:\\c.txt"); 16 // 拷贝大文本,使用NIO,RandomAccessFile 17 //copyBigFile("d:\\a.txt","d:\\c.txt"); 18 19 } 20 21 22 // 一行一行读取文件内容 23 public static void readFile(String sourceFile){ 24 BufferedReader br = null; 25 try { 26 br = new BufferedReader(new FileReader(sourceFile)); 27 String s; 28 while (( s = br.readLine()) != null){ 29 System.out.println(s); 30 } 31 } catch (FileNotFoundException e) { 32 e.printStackTrace(); 33 } catch (IOException e) { 34 e.printStackTrace(); 35 }finally { 36 try { 37 br.close(); 38 } catch (IOException e) { 39 e.printStackTrace(); 40 } 41 } 42 } 43 44 // 以字符的方式读取a文件写入到b文件 45 public static void readFileByReader(String sourceFile,String targetFile){ 46 BufferedReader br =null; 47 //FileWriter fw = null; 48 PrintWriter pw = null; 49 try { 50 br = new BufferedReader(new FileReader(sourceFile)); 51 //fw = new FileWriter(targetFile); 52 //pw = new PrintWriter(new FileWriter(targetFile)); 53 pw = new PrintWriter(targetFile); 54 String s ; 55 while ((s = br.readLine()) != null){ 56 //fw.write(s+"\r\n"); 57 pw.write(s); 58 } 59 60 } catch (FileNotFoundException e) { 61 e.printStackTrace(); 62 } catch (IOException e) { 63 e.printStackTrace(); 64 }finally { 65 try { 66 //fw.close(); 67 pw.close(); 68 br.close(); 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 } 73 } 74 75 // 以字节的方式读取a文件写入到b文件 76 public static void readFileByStream(String sourceFile,String targetFile){ 77 BufferedInputStream in = null; 78 BufferedOutputStream out = null; 79 try { 80 in = new BufferedInputStream(new FileInputStream(sourceFile)); 81 out = new BufferedOutputStream(new FileOutputStream(targetFile)); 82 int len = in.available(); 83 if(len > 0){ 84 byte[] bytes = new byte[len]; 85 in.read(bytes, 0 ,len); 86 out.write(bytes); 87 } 88 } catch (FileNotFoundException e) { 89 e.printStackTrace(); 90 } catch (IOException e) { 91 e.printStackTrace(); 92 }finally { 93 try { 94 out.close(); 95 in.close(); 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 } 100 } 101 102 //拷贝a文件到b文件 103 public static void copyFile(String sourceFile,String targetFile){ 104 File file = new File(sourceFile); 105 if(!file.exists()){ 106 System.out.println(sourceFile+"不存在"); 107 } 108 FileInputStream in = null; 109 FileOutputStream out = null; 110 try { 111 in = new FileInputStream(sourceFile); 112 out = new FileOutputStream(targetFile); 113 int len = in.available(); 114 if(len > 0){ 115 byte[] bytes = new byte[len]; 116 in.read(bytes,0 , len); 117 out.write(bytes); 118 } 119 } catch (FileNotFoundException e) { 120 e.printStackTrace(); 121 } catch (IOException e) { 122 e.printStackTrace(); 123 }finally { 124 try { 125 out.close(); 126 in.close(); 127 } catch (IOException e) { 128 e.printStackTrace(); 129 } 130 } 131 } 132 133 134 // 拷贝大文本,使用NIO,RandomAccessFile 135 public static void copyBigFile(String sourceFile,String targetFile){ 136 File file = new File(sourceFile); 137 if(!file.exists()){ 138 System.out.println("文件不存在"); 139 } 140 FileChannel in =null; 141 FileChannel out =null; 142 try { 143 in = new RandomAccessFile(sourceFile,"r").getChannel(); 144 out = new FileOutputStream(targetFile).getChannel(); 145 ByteBuffer buffer = ByteBuffer.allocate(20*1024*1024); 146 while (in.read(buffer) != -1){ 147 buffer.flip(); 148 out.write(buffer); 149 buffer.clear(); 150 } 151 } catch (FileNotFoundException e) { 152 e.printStackTrace(); 153 } catch (IOException e) { 154 e.printStackTrace(); 155 }finally { 156 try { 157 out.close(); 158 in.close(); 159 } catch (IOException e) { 160 e.printStackTrace(); 161 } 162 } 163 164 } 165 }
原文地址:https://www.cnblogs.com/zhangcece/p/8799316.html
时间: 2024-10-11 20:27:48