0x00 前言
最近这几个月一直在学java,这几天为了搞懂Java的读写代码,特意总结了一下写法和意义。
0x01 代码详情
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| package readwrite_test;
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;
public class zhuge_1 { public static void main(String[] args) { readFileChar("E:/11.txt"); readFileByte("E:/11.txt"); writeFileChar("E:/11.txt","E:/22.txt"); writeFileByte("E:/11.txt","E:/22.txt"); } private static void readFileChar(String path){
FileReader fr = null; BufferedReader br = null; try { File file = new File(path); fr = new FileReader(file); br = new BufferedReader(fr); String line = ""; while((line = br.readLine()) != null){ System.out.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(br != null) br.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void readFileByte(String path){
FileInputStream fis = null; BufferedInputStream bis = null; try { File file = new File(path); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); int buf = 0; while((buf=bis.read()) != -1){ System.out.print((char)buf); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(bis != null) bis.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void writeFileChar(String path1,String path2){
File file1 = new File(path1); File file2 = new File(path2); FileReader fr = null; BufferedReader br = null; FileWriter fw = null; BufferedWriter bw = null; try { fr = new FileReader(file1); br = new BufferedReader(fr); fw = new FileWriter(file2); bw = new BufferedWriter(fw); String line = ""; while((line=br.readLine()) != null){ bw.write(line+"\n"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(br != null) br.close(); } catch (IOException e) { e.printStackTrace(); } try { if(bw != null) bw.close(); } catch (IOException e) { e.printStackTrace(); } } } private static void writeFileByte(String path1,String path2){ File file1 = new File(path1); File file2 = new File(path2); FileInputStream fis = null; BufferedInputStream bis = null; FileOutputStream fos = null; BufferedOutputStream bos = null; try { fis = new FileInputStream(file1); bis = new BufferedInputStream(fis); fos = new FileOutputStream(file2); bos = new BufferedOutputStream(fos); int tmp = 0; while((tmp = bis.read()) != -1){ bos.write(tmp); } bos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(bis != null) bis.close(); } catch (IOException e) { e.printStackTrace(); } try { if(bos != null) bos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
|