Java读写文件的几种方式
生活随笔
收集整理的這篇文章主要介紹了
Java读写文件的几种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
Java中讀寫文件是非常基本的IO操作了,現在總結一下常見的用法。首先總結一下讀取文件的步驟:
注意:本文文件的字符集都是UTF-8,如果需要字符集轉換的請自行處理。
一:字節流讀取方式
一般字節流讀取方式用在讀取圖片或者固定格式文件的方式。
如果是一次讀取一個字節方式可以用如下方法:
當然也可以使用緩沖區一次讀多個字節
/*** 以字節為單位讀取文件內容,一次讀取1024個字節*/@Testpublic void inputStream2Test() {InputStream inputStream = null;try {inputStream = new FileInputStream(new File(inputStreamFilePath));byte[] temp = new byte[1024];//設置一個1024個字節大小的緩沖區int byteRead;while ((byteRead = inputStream.read(temp)) != -1) {System.out.write(temp, 0, byteRead);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}}二:字符流讀取方式
字符流讀取方式最常見的就是讀取txt文件操作了,原理就是將上面的字節流轉換成字符流。
代碼示例:
當然字符流也是可以設置緩沖區的
/*** InputStreamReader主要是將字節流轉字符流,* 可以一次讀一個緩沖區*/@Testpublic void inputStreamRead2Test() {InputStreamReader inputStreamReader = null;try {inputStreamReader = new InputStreamReader(new FileInputStream(new File(inputStreamFilePath)));char[] charBuff = new char[1024];//定義一個1024個字符緩沖區大小int charRead;while ((charRead = inputStreamReader.read(charBuff)) != -1) {System.out.println(new String(charBuff, 0, charRead));}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (inputStreamReader != null) {try {inputStreamReader.close();} catch (IOException e) {e.printStackTrace();}}}}三:BufferedReader方式
java中非常實用的給我們提供了緩沖區讀取文件的方法,提供了BufferedReader。實用方式如下:
/*** BufferedReader 默認會將字符流讀到一個緩沖區,緩沖區默認大小 8192*/@Testpublic void bufferReaderTest() {BufferedReader bufferedReader;try {bufferedReader = new BufferedReader(new FileReader(new File(inputStreamFilePath)));int len;while ((len = bufferedReader.read()) != -1) {System.out.print((char) len);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}更方便的是BufferedReader中提供了按行讀的readLine方法
/*** BufferedReader 默認會將字符流讀到一個緩沖區,提供readLine方法,按行讀取信息*/@Testpublic void bufferReader2Test() {BufferedReader bufferedReader;try {bufferedReader = new BufferedReader(new FileReader(new File(inputStreamFilePath)));String conStr;while ((conStr = bufferedReader.readLine()) != null) {System.out.println(conStr);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}三:寫文件
直接使用FileOutputStream方式
@Testpublic void outputStreamTest() {OutputStream outputStream = null;try {File file = new File(outputStreamFilePath);outputStream = new FileOutputStream(file);String hello = "你好,leo825,";outputStream.write(hello.getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (outputStream != null) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}}當然也可以使用直接使用BufferedOutputStream方式
/*** BufferedOutputStream輸出流,默認緩沖區8192*/@Testpublic void bufferedOutputStreamTest() {BufferedInputStream bufferedInputStream = null;BufferedOutputStream bufferedOutputStream = null;try {File inputStreamFile = new File(inputStreamFilePath);File outputStreamFile = new File(outputStreamFilePath);bufferedInputStream = new BufferedInputStream(new FileInputStream(inputStreamFile));bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputStreamFile));int len;while ((len = bufferedInputStream.read()) != -1) {bufferedOutputStream.write(len);}bufferedOutputStream.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (bufferedInputStream != null) {try {bufferedInputStream.close();} catch (IOException e) {e.printStackTrace();}}if (bufferedOutputStream != null) {try {bufferedOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}四:追加文件內容
方法一:使用FileWriter
/*** 向文件中追加內容*/@Testpublic void appendFile1Test() {FileWriter fw = null;PrintWriter pw = null;try {File file = new File(outputStreamFilePath);fw = new FileWriter(file, true);pw = new PrintWriter(fw);pw.append("你好,我是追加內容1\r\n");pw.flush();fw.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {pw.close();fw.close();} catch (IOException e) {e.printStackTrace();}}}方法二:使用BufferedWriter進行追加
/*** 使用BufferedWriter進行追加*/@Testpublic void appendFile2Test() {BufferedWriter bufferedWriter = null;try {File file = new File(outputStreamFilePath);bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));bufferedWriter.write("你好,我是追加內容2\r\n");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {bufferedWriter.close();} catch (IOException e) {e.printStackTrace();}}}方法三:使用RandomAccessFile結合seek方法進行追加
/*** 使用RandomAccessFile結合seek方法進行追加*/@Testpublic void appendFile3Test() {RandomAccessFile randomAccessFile = null;try {File file = new File(outputStreamFilePath);randomAccessFile = new RandomAccessFile(file, "rw");//打開一個隨機訪問文件流,按照讀寫方式long fileLength = randomAccessFile.length();//文件的長度,用來尋找文件尾部randomAccessFile.seek(fileLength);//將寫文件指針移動到文件尾部randomAccessFile.write("你好,我是追加內容3\r\n".getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {randomAccessFile.close();} catch (IOException e) {e.printStackTrace();}}}總結
以上是生活随笔為你收集整理的Java读写文件的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中的锁的概念大汇总
- 下一篇: JavaWeb的web.xml中cont