Java的讀寫文件方法在工作中相信有很多的用處的,本人在之前包括現在都在使用Java的讀寫文件方法來處理數據方面的輸入輸出,確實很方便。奈何我的記性實在是叫人著急,很多時候既然都會想不起來怎麼寫了,不過我的Java代碼量也實在是少的可憐,所以應該多多練習。這裡做一個總結,集中在一起方面今後查看。
Java讀文件
1 package 天才白癡夢; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileReader; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.io.RandomAccessFile; 11 import java.io.Reader; 12 13 public class JavaIO { 14 15 /** 16 * 采用的是操作系統底層默認的編碼方式,GBK等,非UTF8 17 * */ 18 19 /** 20 * 以字節為單位讀取文件內容,常用於讀取二進制文件,比如圖片、影像、聲音等文件 21 * */ 22 public static void readFileByBytes(String filename) { 23 File file=new File(filename); 24 FileInputStream in=null; 25 try { 26 System.out.println("以字節為單位讀取文件,一次讀一個字節: "); 27 in=new FileInputStream(file); 28 int temp=0; 29 while ((temp=in.read()) != -1) { 30 System.out.println(temp); 31 } 32 in.close(); 33 } catch (IOException e) { 34 e.printStackTrace(); 35 return ; 36 } 37 try { 38 System.out.println("以字節為單位讀取文件,一次讀多個字節: "); 39 byte[] temp=new byte[100]; 40 int byteread=0; 41 in=new FileInputStream(file); 42 JavaIO.showAvailableBytes(in); 43 while ((byteread=in.read(temp)) != -1) { 44 System.out.write(temp,0,byteread); 45 } 46 } catch (Exception e1) { 47 e1.printStackTrace(); 48 } finally { 49 if (in != null) { 50 try { 51 in.close(); 52 } catch (IOException e1) { 53 54 } 55 } 56 } 57 } 58 /** 59 * 以字符為單位讀取文件,常用於讀文本,數字等類型的文件 60 * */ 61 public static void readFileByChar(String filename) { 62 File file=new File(filename); 63 Reader reader=null; 64 try { 65 System.out.println("以字符為單位讀取文件內容,一次一個字節:"); 66 //InputStreamReader類:是字節向字符轉換的橋梁 67 reader=new InputStreamReader(new FileInputStream(file)); 68 int temp; 69 while ((temp=reader.read()) != -1) { 70 if (((char)temp) != '\r') { 71 System.out.println((char)temp); 72 } 73 } 74 reader.close(); 75 } catch (Exception e) { 76 e.printStackTrace(); 77 } 78 try { 79 System.out.println("以字符為單位讀取文件內容,一次讀多個字節: "); 80 char[] temp=new char[30]; 81 int charread=0; 82 reader=new InputStreamReader(new FileInputStream(filename)); 83 while ((charread=reader.read(temp)) != -1) { 84 if ((charread == temp.length) && (temp[temp.length-1]!='\r')) { 85 System.out.println(temp); 86 } else { 87 for (int i=0; i<charread; i++) { 88 if (temp[i] == '\r') { 89 break; 90 } else { 91 System.out.println(temp[i]); 92 } 93 } 94 } 95 } 96 } catch (Exception e) { 97 e.printStackTrace(); 98 } finally { 99 if (reader != null) { 100 try { 101 reader.close(); 102 } catch (IOException e) { 103 104 } 105 } 106 } 107 } 108 /** 109 * 以行為單位讀取文件,常用於讀面向行的格式化文件 110 * */ 111 public static void readFileByLine(String filename) { 112 File file=new File(filename); 113 BufferedReader reader=null; 114 try { 115 System.out.println("以行為單位讀取文件內容,一次讀一整行: "); 116 reader=new BufferedReader(new FileReader(file)); 117 String temp=null; 118 int line=1; 119 while ((temp=reader.readLine()) != null) { 120 System.out.println("line " + line + ": " + temp); 121 line++; 122 } 123 reader.close(); 124 } catch (IOException e) { 125 e.printStackTrace(); 126 } finally { 127 if (reader != null) { 128 try { 129 reader.close(); 130 } catch (IOException e) { 131 132 } 133 } 134 } 135 } 136 /** 137 * 隨機讀取文件內容 138 * */ 139 public static void readFileByRandomAccess(String filename) { 140 RandomAccessFile randomfile=null; 141 try { 142 System.out.println("隨機讀取一段文件內容"); 143 randomfile=new RandomAccessFile(filename,"r"); 144 long fileLength=randomfile.length(); 145 int beginIndex=(fileLength > 4 ? 4 : 0); 146 randomfile.seek(beginIndex); 147 byte[] bytes=new byte[10]; 148 int byteread=0; 149 while ((byteread=randomfile.read(bytes)) != -1) { 150 System.out.write(bytes,0,byteread); 151 } 152 } catch (IOException e) { 153 e.printStackTrace(); 154 } finally { 155 if (randomfile != null) { 156 try { 157 randomfile.close(); 158 } catch (IOException e) { 159 160 } 161 } 162 } 163 } 164 private static void showAvailableBytes(InputStream in) { 165 try { 166 System.out.println("當前字節輸入流中的字節數為:" + in.available()); 167 } catch (IOException e) { 168 e.printStackTrace(); 169 } 170 } 171 public static void main(String[] args) { 172 String filename="E:\\BaiYiShaoNian.txt"; 173 JavaIO.readFileByBytes(filename); 174 JavaIO.readFileByChar(filename); 175 JavaIO.readFileByLine(filename); 176 JavaIO.readFileByRandomAccess(filename); 177 } 178 }
Java寫文件
1 package 天才白癡夢; 2 3 import java.io.BufferedWriter; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 import java.io.OutputStreamWriter; 10 11 public class JavaIO2 { 12 13 public static void main(String[] args) throws IOException { 14 String Path="E:\\天才白癡夢\\JAVA"; 15 File file=new File("E:\\天才白癡夢\\JAVA","BaiYiShaoNian.txt"); 16 if (!file.exists()) { 17 try { 18 file.createNewFile(); 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } 22 } 23 /** 24 * Java寫入文件的三種方法 25 * */ 26 FileOutputStream fos=null; 27 BufferedWriter bw=null; 28 FileWriter fw=null; 29 int value=1000; 30 31 try { 32 fos=new FileOutputStream(new File(Path+"fos.txt")); 33 long begin=System.currentTimeMillis(); 34 for (int i=1; i<=value; i++) { 35 fos.write(5); 36 } 37 long end=System.currentTimeMillis(); 38 System.out.println("TheCostTime of FileOutputStream is : " + (end-begin)); 39 fos.close(); 40 41 bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8")); 42 begin=System.currentTimeMillis(); 43 for (int i=1; i<=value; i++) { 44 bw.write(5); 45 bw.newLine(); 46 } 47 bw.close(); 48 end=System.currentTimeMillis(); 49 System.out.println("TheCostTime of BufferedWriter is : " + (end-begin)); 50 51 fw=new FileWriter(Path+"fw.txt"); 52 begin=System.currentTimeMillis(); 53 for (int i=1; i<=value; i++) { 54 fw.write(5); 55 } 56 fw.close(); 57 end=System.currentTimeMillis(); 58 System.out.println("TheCostTime of FileWriter is : " + (end-begin)); 59 60 61 } catch (Exception e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } finally { 65 try { 66 fos.close(); //FileOutputStream 67 bw.close(); //BufferedWriter 68 fw.close(); //FileWriter 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } 72 } 73 74 } 75 }