Java讀寫文件辦法總結(推舉)。本站提示廣大學習愛好者:(Java讀寫文件辦法總結(推舉))文章只能為提供參考,不一定能成為您想要的結果。以下是Java讀寫文件辦法總結(推舉)正文
Java的讀寫文件辦法在任務中信任有許多的用途的,自己在之前包含如今都在應用Java的讀寫文件辦法來處置數據方面的輸出輸入,確切很便利。若何怎樣我的忘性其實是叫人焦急,許多時刻既然都邑想不起來怎樣寫了,不外我的Java代碼量也其實是少的不幸,所以應當多多演習。這裡做一個總結,集中在一路方面往後檢查。
Java讀文件
package 天賦呆子夢;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
public class JavaIO {
/**
* 采取的是操作體系底層默許的編碼方法,GBK等,非UTF8
* */
/**
* 以字節為單元讀取文件內容,經常使用於讀取二進制文件,好比圖片、影象、聲響等文件
* */
public static void readFileByBytes(String filename) {
File file=new File(filename);
FileInputStream in=null;
try {
System.out.println("以字節為單元讀取文件,一次讀一個字節: ");
in=new FileInputStream(file);
int temp=0;
while ((temp=in.read()) != -1) {
System.out.println(temp);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return ;
}
try {
System.out.println("以字節為單元讀取文件,一次讀多個字節: ");
byte[] temp=new byte[100];
int byteread=0;
in=new FileInputStream(file);
JavaIO.showAvailableBytes(in);
while ((byteread=in.read(temp)) != -1) {
System.out.write(temp,0,byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以字符為單元讀取文件,經常使用於讀文本,數字等類型的文件
* */
public static void readFileByChar(String filename) {
File file=new File(filename);
Reader reader=null;
try {
System.out.println("以字符為單元讀取文件內容,一次一個字節:");
//InputStreamReader類:是字節向字符轉換的橋梁
reader=new InputStreamReader(new FileInputStream(file));
int temp;
while ((temp=reader.read()) != -1) {
if (((char)temp) != '\r') {
System.out.println((char)temp);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符為單元讀取文件內容,一次讀多個字節: ");
char[] temp=new char[30];
int charread=0;
reader=new InputStreamReader(new FileInputStream(filename));
while ((charread=reader.read(temp)) != -1) {
if ((charread == temp.length) && (temp[temp.length-1]!='\r')) {
System.out.println(temp);
} else {
for (int i=0; i<charread; i++) {
if (temp[i] == '\r') {
break;
} else {
System.out.println(temp[i]);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
/**
* 以行動單元讀取文件,經常使用於讀面向行的格局化文件
* */
public static void readFileByLine(String filename) {
File file=new File(filename);
BufferedReader reader=null;
try {
System.out.println("以行動單元讀取文件內容,一次讀一整行: ");
reader=new BufferedReader(new FileReader(file));
String temp=null;
int line=1;
while ((temp=reader.readLine()) != null) {
System.out.println("line " + line + ": " + temp);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
/**
* 隨機讀取文件內容
* */
public static void readFileByRandomAccess(String filename) {
RandomAccessFile randomfile=null;
try {
System.out.println("隨機讀取一段文件內容");
randomfile=new RandomAccessFile(filename,"r");
long fileLength=randomfile.length();
int beginIndex=(fileLength > 4 ? 4 : 0);
randomfile.seek(beginIndex);
byte[] bytes=new byte[10];
int byteread=0;
while ((byteread=randomfile.read(bytes)) != -1) {
System.out.write(bytes,0,byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomfile != null) {
try {
randomfile.close();
} catch (IOException e) {
}
}
}
}
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("以後字節輸出流中的字節數為:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String filename="E:\\BaiYiShaoNian.txt";
JavaIO.readFileByBytes(filename);
JavaIO.readFileByChar(filename);
JavaIO.readFileByLine(filename);
JavaIO.readFileByRandomAccess(filename);
}
}
Java寫文件
package 天賦呆子夢;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class JavaIO2 {
public static void main(String[] args) throws IOException {
String Path="E:\\天賦呆子夢\\JAVA";
File file=new File("E:\\天賦呆子夢\\JAVA","BaiYiShaoNian.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Java寫入文件的三種辦法
* */
FileOutputStream fos=null;
BufferedWriter bw=null;
FileWriter fw=null;
int value=1000;
try {
fos=new FileOutputStream(new File(Path+"fos.txt"));
long begin=System.currentTimeMillis();
for (int i=1; i<=value; i++) {
fos.write(5);
}
long end=System.currentTimeMillis();
System.out.println("TheCostTime of FileOutputStream is : " + (end-begin));
fos.close();
bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8"));
begin=System.currentTimeMillis();
for (int i=1; i<=value; i++) {
bw.write(5);
bw.newLine();
}
bw.close();
end=System.currentTimeMillis();
System.out.println("TheCostTime of BufferedWriter is : " + (end-begin));
fw=new FileWriter(Path+"fw.txt");
begin=System.currentTimeMillis();
for (int i=1; i<=value; i++) {
fw.write(5);
}
fw.close();
end=System.currentTimeMillis();
System.out.println("TheCostTime of FileWriter is : " + (end-begin));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fos.close(); //FileOutputStream
bw.close(); //BufferedWriter
fw.close(); //FileWriter
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
以上這篇Java讀寫文件辦法總結(推舉)就是小編分享給年夜家的全體內容了,願望能給年夜家一個參考,也願望年夜家多多支撐。