java中讀取文件的方式

一、多種方式讀文件內容。

1、按字節讀取文件內容
2、按字符讀取文件內容
3、按行讀取文件內容
4、隨機讀取文件內容

None.gifimport java.io.BufferedReader;
None.gif
import java.io.File;
None.gif
import java.io.FileInputStream;
None.gif
import java.io.FileReader;
None.gif
import java.io.IOException;
None.gif
import java.io.InputStream;
None.gif
import java.io.InputStreamReader;
None.gif
import java.io.RandomAccessFile;
None.gif
import java.io.Reader;
ExpandedBlockStart.gif
publicclass ReadFromFile {
ExpandedSubBlockStart.gif
/**
InBlock.gif * 以字節爲單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
InBlock.gif *
@param fileName 文件的名
ExpandedSubBlockEnd.gif
*/
ExpandedSubBlockStart.gif
publicstaticvoid readFileByBytes(String fileName){
InBlock.gif File file
=new File(fileName);
InBlock.gif InputStream in
=null;
ExpandedSubBlockStart.gif
try{
InBlock.gif System.out.println(
"以字節爲單位讀取文件內容,一次讀一個字節:");
InBlock.gif
// 一次讀一個字節
InBlock.gif
in =new FileInputStream(file);
InBlock.gif
int tempbyte;
ExpandedSubBlockStart.gif
while((tempbyte=in.read()) !=-1){
InBlock.gif System.out.write(tempbyte);
ExpandedSubBlockEnd.gif }

InBlock.gif in.close();
ExpandedSubBlockStart.gif }
catch (IOException e) {
InBlock.gif e.printStackTrace();
InBlock.gif
return;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gif
try{
InBlock.gif System.out.println(
"以字節爲單位讀取文件內容,一次讀多個字節:");
InBlock.gif
//一次讀多個字節
InBlock.gif
byte[] tempbytes =newbyte[100];
InBlock.gif
int byteread =0;
InBlock.gif in
=new FileInputStream(fileName);
InBlock.gif ReadFromFile.showAvailableBytes(in);
InBlock.gif
//讀入多個字節到字節數組中,byteread爲一次讀入的字節數
ExpandedSubBlockStart.gif
while ((byteread = in.read(tempbytes)) !=-1){
InBlock.gif System.out.write(tempbytes,
0, byteread);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gif }
catch (Exception e1) {
InBlock.gif e1.printStackTrace();
ExpandedSubBlockStart.gif }
finally{
ExpandedSubBlockStart.gif
if (in !=null){
ExpandedSubBlockStart.gif
try{
InBlock.gif in.close();
ExpandedSubBlockStart.gif }
catch (IOException e1) {
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gif
/**
InBlock.gif * 以字符爲單位讀取文件,常用於讀文本,數字等類型的文件
InBlock.gif *
@param fileName 文件名
ExpandedSubBlockEnd.gif
*/
ExpandedSubBlockStart.gif
publicstaticvoid readFileByChars(String fileName){
InBlock.gif File file
=new File(fileName);
InBlock.gif Reader reader
=null;
ExpandedSubBlockStart.gif
try{
InBlock.gif System.out.println(
"以字符爲單位讀取文件內容,一次讀一個字節:");
InBlock.gif
// 一次讀一個字符
InBlock.gif
reader =new InputStreamReader(new FileInputStream(file));
InBlock.gif
int tempchar;
ExpandedSubBlockStart.gif
while ((tempchar = reader.read()) !=-1){
InBlock.gif
//對於windows下,這兩個字符在一起時,表示一個換行。
InBlock.gif
//但如果這兩個字符分開顯示時,會換兩次行。
InBlock.gif
//因此,屏蔽掉,或者屏蔽。否則,將會多出很多空行。
ExpandedSubBlockStart.gif
if (((char)tempchar) !=''''){
InBlock.gif System.out.print((
char)tempchar);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif reader.close();
ExpandedSubBlockStart.gif }
catch (Exception e) {
InBlock.gif e.printStackTrace();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gif
try{
InBlock.gif System.out.println(
"以字符爲單位讀取文件內容,一次讀多個字節:");
InBlock.gif
//一次讀多個字符
InBlock.gif
char[] tempchars =newchar[30];
InBlock.gif
int charread =0;
InBlock.gif reader
=new InputStreamReader(new FileInputStream(fileName));
InBlock.gif
//讀入多個字符到字符數組中,charread爲一次讀取字符數
ExpandedSubBlockStart.gif
while ((charread = reader.read(tempchars))!=-1){
InBlock.gif
//同樣屏蔽掉不顯示
ExpandedSubBlockStart.gif
if ((charread == tempchars.length)&&(tempchars[tempchars.length-1] !='''')){
InBlock.gif System.out.print(tempchars);
ExpandedSubBlockStart.gif }
else{
ExpandedSubBlockStart.gif
for (int i=0; i<charread; i++){
ExpandedSubBlockStart.gif
if(tempchars[i] ==''''){
InBlock.gif
continue;
ExpandedSubBlockStart.gif }
else{
InBlock.gif System.out.print(tempchars[i]);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gif }
catch (Exception e1) {
InBlock.gif e1.printStackTrace();
ExpandedSubBlockStart.gif }
finally{
ExpandedSubBlockStart.gif
if (reader !=null){
ExpandedSubBlockStart.gif
try{
InBlock.gif reader.close();
ExpandedSubBlockStart.gif }
catch (IOException e1) {
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gif
/**
InBlock.gif * 以行爲單位讀取文件,常用於讀面向行的格式化文件
InBlock.gif *
@param fileName 文件名
ExpandedSubBlockEnd.gif
*/
ExpandedSubBlockStart.gif
publicstaticvoid readFileByLines(String fileName){
InBlock.gif File file
=new File(fileName);
InBlock.gif BufferedReader reader
=null;
ExpandedSubBlockStart.gif
try{
InBlock.gif System.out.println(
"以行爲單位讀取文件內容,一次讀一整行:");
InBlock.gif reader
=new BufferedReader(new FileReader(file));
InBlock.gif String tempString
=null;
InBlock.gif
int line =1;
InBlock.gif
//一次讀入一行,直到讀入null爲文件結束
ExpandedSubBlockStart.gif
while ((tempString = reader.readLine()) !=null){
InBlock.gif
//顯示行號
InBlock.gif
System.out.println("line "+ line +": "+ tempString);
InBlock.gif line
++;
ExpandedSubBlockEnd.gif }

InBlock.gif reader.close();
ExpandedSubBlockStart.gif }
catch (IOException e) {
InBlock.gif e.printStackTrace();
ExpandedSubBlockStart.gif }
finally{
ExpandedSubBlockStart.gif
if (reader !=null){
ExpandedSubBlockStart.gif
try{
InBlock.gif reader.close();
ExpandedSubBlockStart.gif }
catch (IOException e1) {
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gif
/**
InBlock.gif * 隨機讀取文件內容
InBlock.gif *
@param fileName 文件名
ExpandedSubBlockEnd.gif
*/
ExpandedSubBlockStart.gif
publicstaticvoid readFileByRandomAccess(String fileName){
InBlock.gif RandomAccessFile randomFile
=null;
ExpandedSubBlockStart.gif
try{
InBlock.gif System.out.println(
"隨機讀取一段文件內容:");
InBlock.gif
// 打開一個隨機訪問文件流,按只讀方式
InBlock.gif
randomFile =new RandomAccessFile(fileName, "r");
InBlock.gif
// 文件長度,字節數
InBlock.gif
long fileLength = randomFile.length();
InBlock.gif
// 讀文件的起始位置
InBlock.gif
int beginIndex = (fileLength >4) ?4 : 0;
InBlock.gif
//將讀文件的開始位置移到beginIndex位置。
InBlock.gif
randomFile.seek(beginIndex);
InBlock.gif
byte[] bytes =newbyte[10];
InBlock.gif
int byteread =0;
InBlock.gif
//一次讀10個字節,如果文件內容不足10個字節,則讀剩下的字節。
InBlock.gif
//將一次讀取的字節數賦給byteread
ExpandedSubBlockStart.gif
while ((byteread = randomFile.read(bytes)) !=-1){
InBlock.gif System.out.write(bytes,
0, byteread);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gif }
catch (IOException e){
InBlock.gif e.printStackTrace();
ExpandedSubBlockStart.gif }
finally{
ExpandedSubBlockStart.gif
if (randomFile !=null){
ExpandedSubBlockStart.gif
try{
InBlock.gif randomFile.close();
ExpandedSubBlockStart.gif }
catch (IOException e1) {
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gif
/**
InBlock.gif * 顯示輸入流中還剩的字節數
InBlock.gif *
@param in
ExpandedSubBlockEnd.gif
*/
ExpandedSubBlockStart.gif
privatestaticvoid showAvailableBytes(InputStream in){
ExpandedSubBlockStart.gif
try{
InBlock.gif System.out.println(
"當前字節輸入流中的字節數爲:"+ in.available());
ExpandedSubBlockStart.gif }
catch (IOException e) {
InBlock.gif e.printStackTrace();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gif
publicstaticvoid main(String[] args) {
InBlock.gif String fileName
="C:/temp/newTemp.txt";
InBlock.gif ReadFromFile.readFileByBytes(fileName);
InBlock.gif ReadFromFile.readFileByChars(fileName);
InBlock.gif ReadFromFile.readFileByLines(fileName);
InBlock.gif ReadFromFile.readFileByRandomAccess(fileName);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

二、將內容追加到文件尾部




None.gifimport java.io.FileWriter;
None.gif
import java.io.IOException;
None.gif
import java.io.RandomAccessFile;
None.gif
ExpandedBlockStart.gif
/**
InBlock.gif* 將內容追加到文件尾部
ExpandedBlockEnd.gif
*/
ExpandedBlockStart.gif
publicclass AppendToFile {
InBlock.gif
ExpandedSubBlockStart.gif
/**
InBlock.gif * A方法追加文件:使用RandomAccessFile
InBlock.gif *
@param fileName 文件名
InBlock.gif *
@param content 追加的內容
ExpandedSubBlockEnd.gif
*/
ExpandedSubBlockStart.gif
publicstaticvoid appendMethodA(String fileName, String content){
ExpandedSubBlockStart.gif
try{
InBlock.gif
// 打開一個隨機訪問文件流,按讀寫方式
InBlock.gif
RandomAccessFile randomFile =new RandomAccessFile(fileName, "rw");
InBlock.gif
// 文件長度,字節數
InBlock.gif
long fileLength = randomFile.length();
InBlock.gif
//將寫文件指針移到文件尾。
InBlock.gif
randomFile.seek(fileLength);
InBlock.gif randomFile.writeBytes(content);
InBlock.gif randomFile.close();
ExpandedSubBlockStart.gif }
catch (IOException e){
InBlock.gif e.printStackTrace();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gif
/**
InBlock.gif * B方法追加文件:使用FileWriter
InBlock.gif *
@param fileName
InBlock.gif *
@param content
ExpandedSubBlockEnd.gif
*/
ExpandedSubBlockStart.gif
publicstaticvoid appendMethodB(String fileName, String content){
ExpandedSubBlockStart.gif
try{
InBlock.gif
//打開一個寫文件器,構造函數中的第二個參數true表示以追加形式寫文件
InBlock.gif
FileWriter writer =new FileWriter(fileName, true);
InBlock.gif writer.write(content);
InBlock.gif writer.close();
ExpandedSubBlockStart.gif }
catch (IOException e) {
InBlock.gif e.printStackTrace();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gif
publicstaticvoid main(String[] args) {
InBlock.gif String fileName
="C:/temp/newTemp.txt";
InBlock.gif String content
="new append!";
InBlock.gif
//按方法A追加文件
InBlock.gif
AppendToFile.appendMethodA(fileName, content);
InBlock.gif AppendToFile.appendMethodA(fileName,
"append end. ");
InBlock.gif
//顯示文件內容
InBlock.gif
ReadFromFile.readFileByLines(fileName);
InBlock.gif
//按方法B追加文件
InBlock.gif
AppendToFile.appendMethodB(fileName, content);
InBlock.gif AppendToFile.appendMethodB(fileName,
"append end. ");
InBlock.gif
//顯示文件內容
InBlock.gif
ReadFromFile.readFileByLines(fileName);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章