當前位置:首頁 » 編程語言 » javafile內容

javafile內容

發布時間: 2023-09-19 10:07:52

java中讀取文件(二進制,字元)內容的幾種方

JAVA中讀取文件內容的方法有很多,比如按位元組讀取文件內容,按字元讀取文件內容,按行讀取文件內容,隨機讀取文件內容等方法,本文就以上方法的具體實現給出代碼,需要的可以直接復制使用

public class ReadFromFile {
/**
* 以位元組為單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以位元組為單位讀取文件內容,一次讀一個位元組:");
// 一次讀一個位元組
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以位元組為單位讀取文件內容,一次讀多個位元組:");
// 一次讀多個位元組
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 讀入多個位元組到位元組數組中,byteread為一次讀入的位元組數
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}

/**
* 以字元為單位讀取文件,常用於讀文本,數字等類型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字元為單位讀取文件內容,一次讀一個位元組:");
// 一次讀一個字元
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 對於windows下,\r\n這兩個字元在一起時,表示一個換行。
// 但如果這兩個字元分開顯示時,會換兩次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否則,將會多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字元為單位讀取文件內容,一次讀多個位元組:");
// 一次讀多個字元
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 讀入多個字元到字元數組中,charread為一次讀取字元數
while ((charread = reader.read(tempchars)) != -1) {
// 同樣屏蔽掉\r不顯示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}

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

/**
* 以行為單位讀取文件,常用於讀面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行為單位讀取文件內容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次讀入一行,直到讀入null為文件結束
while ((tempString = reader.readLine()) != null) {
// 顯示行號
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}

/**
* 隨機讀取文件內容
*/
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;
// 將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次讀10個位元組,如果文件內容不足10個位元組,則讀剩下的位元組。
// 將一次讀取的位元組數賦給byteread
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 e1) {
}
}
}
}

/**
* 顯示輸入流中還剩的位元組數
*/
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 = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}

㈡ 編JAVA程序獲取指定D盤下file.txt內容要求創建一個fis位元組輸入流對象一個buf位元組組長度定義為1024整數倍

您可以使用以下代碼來實現您的需求:

該代碼會創建一個 FileInputStream 對象,該對象將從指定的文件路徑中讀取數據。然後,它會創建一個長度為 1024 的位元組數組,並使用 fis.read(buf) 方罩拿敬法將物慎文件內容讀敏乎取到該數組中。最後,它會將讀取到的內容輸出到控制台,並關閉輸入流。

Try again

㈢ Java 如何讀取目錄下的文件內容

Java讀取目錄下的文件內容,使用的是java的文件類,示例如下:

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.RandomAccessFile;
importjava.io.Reader;

publicclassReadFromFile{
/**
*以位元組為單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
*
*@paramfileName
*文件的名
*/
(StringfileName){
桐大昌Filefile=newFile(fileName);
InputStreamin=null;
try{
System.out.println("以位元組為單位讀取文件內容,一次讀一個位元組:");
//一次讀一個位元組
in=newFileInputStream(file);
inttempbyte;
while((tempbyte=in.read())!=-1){
System.out.write(tempbyte);
}
in.close();
}catch(IOExceptione){
e.printStackTrace();
return;
}
try{
System.out.println("以位元組為單位讀取文件內容,一次讀多個位元組:");
//一次讀多個位元組
byte[]tempbytes=newbyte[100];
intbyteread=0;
in=newFileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
//讀入多個位元組到位元組數組中,byteread為一次讀入的位元組數
while((byteread=in.read(tempbytes))!=-1){
System.out.write(tempbytes,0,byteread);
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(in!=null){
try{
in.close();
}catch(IOExceptione1){
}
}
}
}

/**
*以字元為單位讀取文件,常用於讀文本,數字等類型的文件
*
*@paramfileName
*文件名
*/
(StringfileName){
Filefile=newFile(fileName);
Readerreader=null;
try{
System.out.println("以字元為單位讀取文件內容,一次讀一個位元組:");
//一次讀一個字元
reader=newInputStreamReader(newFileInputStream(file));
局扒inttempchar;
while((tempchar=reader.read())!=-1){
//對於windows下, 這兩個字元在一起時,表示一個換行。
//但如果這兩個字元分開顯示時,會換兩次行。
//因此,屏蔽掉 ,或者屏蔽 。否則,將會多出很多空行。
if(((char)tempchar)!=' '){
System.out.print((char)tempchar);
}
}
reader.close();
}catch(Exceptione){
e.printStackTrace();
}
try{
System.out.println("以字元為單位讀取文件內容,一次讀多個位元組:");
仿燃//一次讀多個字元
char[]tempchars=newchar[30];
intcharread=0;
reader=newInputStreamReader(newFileInputStream(fileName));
//讀入多個字元到字元數組中,charread為一次讀取字元數
while((charread=reader.read(tempchars))!=-1){
//同樣屏蔽掉 不顯示
if((charread==tempchars.length)
&&(tempchars[tempchars.length-1]!=' ')){
System.out.print(tempchars);
}else{
for(inti=0;i<charread;i++){
if(tempchars[i]==' '){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}

}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}

/**
*以行為單位讀取文件,常用於讀面向行的格式化文件
*
*@paramfileName
*文件名
*/
(StringfileName){
Filefile=newFile(fileName);
BufferedReaderreader=null;
try{
System.out.println("以行為單位讀取文件內容,一次讀一整行:");
reader=newBufferedReader(newFileReader(file));
StringtempString=null;
intline=1;
//一次讀入一行,直到讀入null為文件結束
while((tempString=reader.readLine())!=null){
//顯示行號
System.out.println("line"+line+":"+tempString);
line++;
}
reader.close();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}

/**
*隨機讀取文件內容
*
*@paramfileName
*文件名
*/
(StringfileName){
RandomAccessFilerandomFile=null;
try{
System.out.println("隨機讀取一段文件內容:");
//打開一個隨機訪問文件流,按只讀方式
randomFile=newRandomAccessFile(fileName,"r");
//文件長度,位元組數
longfileLength=randomFile.length();
//讀文件的起始位置
intbeginIndex=(fileLength>4)?4:0;
//將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
intbyteread=0;
//一次讀10個位元組,如果文件內容不足10個位元組,則讀剩下的位元組。
//將一次讀取的位元組數賦給byteread
while((byteread=randomFile.read(bytes))!=-1){
System.out.write(bytes,0,byteread);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(randomFile!=null){
try{
randomFile.close();
}catch(IOExceptione1){
}
}
}
}

/**
*顯示輸入流中還剩的位元組數
*
*@paramin
*/
(InputStreamin){
try{
System.out.println("當前位元組輸入流中的位元組數為:"+in.available());
}catch(IOExceptione){
e.printStackTrace();
}
}

publicstaticvoidmain(String[]args){
StringfileName="C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}

㈣ java中file類的用法

public File createFile(String fileName,String dir){
File file=new File(dir+File.separator+fileName);
try{
System.out.println("file------>"+file);
file.createNewFile();
}
catch(IOException ioe){
ioe.printStackTrace();
}
return file;
}

這個能看懂吧,傳遞2個參數,一個是目錄名,一個是文件名,目錄名就是你的d:\\user,文件名就是你的x.txt。不過你的目錄名如果要跨平台的話,最好用個separate分隔符,如果就是在Windows下就用d:\\user這個,為什麼是2個\,這主要是轉義字元的原因,不明的話可以到網上在搜搜轉義字元的使用。呵呵,希望對你所幫助, 純手打,費勁啊。

㈤ java中 File類是什麼

在java中File類是可以直接操作文件的類,
它有四個構造函數:
File(String parent,String child)
File(File parent,String child)
File(URI uri)
File(String pathname)
封裝了以下主要方法:
canWrite() 返迴文件是否可以讀寫
canRead() 返迴文件是否可讀
compareTo(File pathname)檢查文件路徑間的順序
createNewFile() 當文件不存在時生成文件
delete() 從文件系統內刪除該文件
deleteOnExit() 程序順利結束時刪除文件
equals(Object obj) 檢查特定對象的路徑名是否相等
exists() 判斷文件是否存在
getAbsoluteFile() 返迴文件完整路徑的File實例
getAbsolutePath() 返迴文件完整路徑
getName() 返迴文件名稱
getParent() 返迴文件父目錄路徑
getPath() 返迴文件路徑字元串
getParentFile() 返迴文件所在文件夾的路徑
hashCode() 返迴文件哈希碼
isDirectory() 判斷該路徑指示的是否是目錄
isFile() 判斷該路徑指示的是否是文件
lastModified() 返回該文件最後更改時間標志
length() 返迴文件長度
list() 返迴文件和目錄清單
mkdir() 生成指定的目錄
renameTo(File dest) 更改文件名字
setReadOnly() 將文件設置為可讀
toString() 返迴文件狀態的字元串
toURL() 將文件的路徑字元串轉換成URL
推薦於 2017-11-25
查看全部5個回答
— 你看完啦,以下內容更有趣 —
在java中File是什麼意思?有什麼作用?
在java中File類是可以直接操作文件的類,
它有四個構造函數:
File(String parent,String child)
File(File parent,String child)
File(URI uri)
File(String pathname)

封裝了以下主要方法:
canWrite() 返迴文件是否可以讀寫
canRead() 返迴文件是否可讀
compareTo(File pathname)檢查文件路徑間的順序
createNewFile() 當文件不存在時生成文件
delete() 從文件系統內刪除該文件
deleteOnExit() 程序順利結束時刪除文件
equals(Object obj) 檢查特定對象的路徑名是否相等
exists() 判斷文件是否存在
getAbsoluteFile() 返迴文件完整路徑的File實例
getAbsolutePath() 返迴文件完整路徑
getName() 返迴文件名稱
getParent() 返迴文件父目錄路徑
getPath() 返迴文件路徑字元串
getParentFile() 返迴文件所在文件夾的路徑
hashCode() 返迴文件哈希碼
isDirectory() 判斷該路徑指示的是否是目錄
isFile() 判斷該路徑指示的是否是文件
lastModified() 返回該文件最後更改時間標志
length() 返迴文件長度
list() 返迴文件和目錄清單
mkdir() 生成指定的目錄
renameTo(File dest) 更改文件名字
setReadOnly() 將文件設置為可讀
toString() 返迴文件狀態的字元串
toURL() 將文件的路徑字元串轉換成URL

熱點內容
android編程入門經典pdf 發布:2025-02-02 04:46:19 瀏覽:54
安卓什麼軟體測試手機電池 發布:2025-02-02 04:28:52 瀏覽:992
手機上傳快 發布:2025-02-02 04:27:46 瀏覽:306
電腦配置詳解圖解都有哪些 發布:2025-02-02 04:26:27 瀏覽:715
景區應該有什麼配置 發布:2025-02-02 04:09:08 瀏覽:119
c語言與java工作 發布:2025-02-02 03:59:57 瀏覽:282
qq買什麼不要支付密碼 發布:2025-02-02 03:50:29 瀏覽:495
android讀取視頻 發布:2025-02-02 03:46:57 瀏覽:826
手機號序列碼的密碼在哪裡 發布:2025-02-02 03:29:34 瀏覽:878
安卓怎麼換回鴻蒙系統 發布:2025-02-02 03:24:35 瀏覽:513