




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、Java文件讀寫操作代碼實現(xiàn)package test;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java
2、.io.InputStream;import java.io.InputStreamReader;/* * 一、BufferedReader類 public class BufferedReader extends Reader * 從字符輸入流中讀取文本,緩沖各個字符,從而實現(xiàn)字符、數(shù)組和行的高效讀取。 可以指定緩沖區(qū)的大小,或者可使用默認的大小。大多數(shù)情況下,默認值足夠大。 * 通常,Reader 所作的每個讀取請求都會導致對底層字符或字節(jié)流進行相應的讀取請求。因此,建議用 BufferedReader包裝所有其 read() * 操作可能開銷很高的 Reader(如 FileReader
3、和 InputStreamReader)。 * BufferedReader流能夠讀取文本行,通過向BufferedReader傳遞一個Reader對象 * ,來創(chuàng)建一個BufferedReader對象,之所以這樣做是因為FileReader沒有提供讀取文本行的功能. * * 二、InputStreamReader類 * * InputStreamReader 將字節(jié)流轉換為字符流。是字節(jié)流通向字符流的橋梁。如果不指定字符集編碼,該解碼過程將使用平臺默認的字符編碼,如:GBK。 * * 構造方法: * * InputStreamReader isr = new InputStreamReade
4、r(InputStream * in);/構造一個默認編碼集的InputStreamReader類 * * InputStreamReader isr = new InputStreamReader(InputStream in,String * charsetName);/構造一個指定編碼集的InputStreamReader類。 * * 參數(shù) in對象通過 InputStream in = System.in;獲得。/讀取鍵盤上的數(shù)據(jù)。 * * 或者 InputStream in = new FileInputStream(String fileName);/讀取文件中的數(shù)據(jù)。可以看出 *
5、 FileInputStream 為InputStream的子類。 * * 主要方法:int read();/讀取單個字符。 int read(char cbuf);/將讀取到的字符存到數(shù)組中。返回讀取的字符數(shù)。 * * 三、FileWriter(少量文字) 和 BufferedWriter(大量文字)實現(xiàn)簡單文件寫操作 * author hulk */public class FileReadWriteUtil / "D:demo.txt"private static String PATH = "/home/hulk/devtools/adt-bundle-l
6、inux-x86_64-20140702/workspace/JavaTest/readme.txt"/ orprivate static boolean debugMode = true; /* * param args */public static void main(String args) / readSystemInputText();/讀取鍵盤輸入文字信息/ testBufferReader();String aaaaPath = "/home/hulk/aaaa.txt"String aaaPath = "/home/hulk/aaa.t
7、xt"String text = readResourceFile(aaaaPath).toString();/ 讀取文件資源String appendtext = "nnAAAAAAAAAAAAaaaaaaaaaaaaaaaanBBBBBBBBBBBBBBBBbbbbbbbbbbbbb"writeText(aaaPath, text, false);readResourceFile(aaaPath);/ 讀取文件資源private static void testBufferReader() try readFileNoBuffer(PATH, 2048);/
8、不使用緩沖區(qū)readFileByBuffer(PATH);/ 使用緩沖區(qū),適用于,讀取完整文件 catch (IOException e) e.printStackTrace();public static void setDebugMode(boolean debugMode) FileReadWriteUtil.debugMode = debugMode;/* * 沒有緩沖區(qū),只能使用read()方法,得指定要讀取的字符長度 * * throws IOException */public static String readFileNoBuffer(String path, int rea
9、dChars)throws IOException / 讀取字節(jié)流/ InputStream in = System.in;/讀取鍵盤的輸入。InputStream in = new FileInputStream(path);/ 讀取文件的數(shù)據(jù)。/ 將字節(jié)流向字符流的轉換。要啟用從字節(jié)到字符的有效轉換,/ 可以提前從底層流讀取更多的字節(jié).InputStreamReader isr = new InputStreamReader(in);/ 讀取char cha = new charreadChars;int len = isr.read(cha);String text = new Stri
10、ng(cha, 0, len);log(text);isr.close();return text;/* * 使用緩沖區(qū) 可以使用緩沖區(qū)對象的 read() 和 readLine()方法。 * * throws IOException */public static StringBuffer readFileByBuffer(String path) throws IOException / 讀取字節(jié)流InputStream in = new FileInputStream(path);/ 讀取文件上的數(shù)據(jù)。InputStreamReader isr = new InputStreamRead
11、er(in);/ 將字節(jié)流向字符流的轉換BufferedReader bufr = new BufferedReader(isr);/ 創(chuàng)建字符流緩沖區(qū)StringBuffer buffer = new StringBuffer();String line;while (line = bufr.readLine() != null) buffer.append(line).append("n");log(line);isr.close();return buffer;public static StringBuffer readText(String filePath) B
12、ufferedReader br = null;StringBuffer buffer = null;try br = new BufferedReader(new FileReader(filePath);String line = null;buffer = new StringBuffer();while (line = br.readLine() != null) buffer.append(line).append("n");log(line); catch (FileNotFoundException e) e.printStackTrace(); catch
13、(IOException e) e.printStackTrace(); finally if(br != null) try br.close(); catch (IOException e) e.printStackTrace();return buffer;public static StringBuffer readtextByChar(String filePath) StringBuffer text = null;FileReader reader = null;try reader = new FileReader(filePath);int ch = 0;text = new
14、 StringBuffer();while (ch = reader.read() != -1) text.append(char)ch); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); finally if(reader != null) try reader.close(); catch (IOException e) e.printStackTrace();return text;/* * 使用FileWriter類寫文本文件, 僅限制少量文字
15、 * Note: /使用這個構造函數(shù)時,如果存在filePath.txt文件,則先把這個文件給刪除掉,然后創(chuàng)建新的filePath.txt * param filePath * param text * return the FileWriter object, null if throws Exception */public static FileWriter writeText(String filePath, String text) return writeText(filePath, text, false);/* * 使用FileWriter類寫文本文件 * param file
16、Path * param text * param append true: 如果存在filePath.txt文件,則直接在后面追加字符串; false: 直接刪除原文件 * return the FileWriter object, null if throws Exception */public static FileWriter writeText(String filePath, String text, boolean append) FileWriter writer = null;try writer = new FileWriter(filePath, append);wri
17、ter.write(text); catch (IOException e) e.printStackTrace(); finally if(writer != null) try writer.close(); catch (IOException e) e.printStackTrace();return writer;/* * 向文件寫入字符串??梢詴r較大字符串. * 注意n不一定在各種計算機上都能產(chǎn)生換行的效果: * <p>可以用返回的BufferedWriter對象執(zhí)行一下函數(shù)實現(xiàn)換行: * BufferedWriter out = FileReadWriteUtil.w
18、riteByBuffer("/demo.txt", "AAA", true); * out.newLine();/實現(xiàn)換行 * param filePath * param text * return The BufferedWriter object, null if throws Exception */public static BufferedWriter writeBufferText(String filePath, String text, boolean append) BufferedWriter out = null; try out
19、 = new BufferedWriter(new FileWriter(filePath, append);out.write(text);out.close(); catch (IOException e) e.printStackTrace(); finally if(out != null) try out.close(); catch (IOException e) e.printStackTrace();return out;public static boolean makeFile(String filePath) File file = new File(filePath);
20、if(!file.exists() if(file.isFile() File p = file.getParentFile();if(!p.exists() makeDir(p.getAbsolutePath();try return file.createNewFile(); catch (IOException e) e.printStackTrace();return false; else System.out.print("Not a file path: " + filePath);return false; else return true;public s
21、tatic boolean makeDir(String dir) File p = new File(dir);if(!p.exists() return p.mkdirs(); else return true;/* * 鍵盤輸入讀取文字信息 */public static String readSystemInputText() BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in);System.out.println("請輸入一系列文字,可包括空格,完成后按回車即
22、可顯示出來:");System.out.print("請在這里輸入文字:");String text = null;try text = bufferedReader.readLine(); catch (IOException e) e.printStackTrace();log("你輸入的文字為:" + text);return text;/* * 按行讀取文件信息 * * param path * 文件存放路徑,;如果文件在項目根目錄下,直接用文件名即可eg. "readme.txt" */public static StringBuffer readResourceFile(String path) StringBuffer buffer = n
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 續(xù)保團隊數(shù)據(jù)分析工作總結
- 2024年份11月攜程平臺客房銷售不可抗力應對條款
- 班主任個人年度工作方案2025年演講稿
- 2025年春季幼兒園幼師個人工作方案
- 學校稱職校長2025年學期工作方案
- 2025年老師個人師德工作方案
- 生產(chǎn)安全月主題
- 物流技術與設備
- 甘肅省慶陽市合水縣2025屆數(shù)學三下期末調研模擬試題含解析
- 北京市首都師大附中2025年高三5月教學質量調研化學試題含解析
- 2020-2021學年江蘇省南京外國語河西初級中學等三校七年級(下)期中數(shù)學試卷
- 2025年慢性阻塞性肺疾病全球創(chuàng)議GOLD指南修訂解讀課件
- 10萬噸橡塑一體化能源再生項目環(huán)評報告表
- 農(nóng)村地區(qū)金融包容性對農(nóng)民收入的影響
- (完整版)Brownbear繪本
- 材料力學14章3靜不定結構中對稱與反對稱性質
- (完整版)海運提單(樣本).docx
- 攀巖墻施工方案(完整版)
- 計算機軟件技術專業(yè)《頂崗實習》課程標準
- 廚房排油煙不銹鋼風管施工方案(完整版)
- 貸款調查表-經(jīng)營類
評論
0/150
提交評論