日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多种方式读取文件内容

發布時間:2025/3/21 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多种方式读取文件内容 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

import java.io.*;

/**
?*
?* @author wxp
?* Created by Administrator on 2017/10/29 0029.
?*/
public class ReadFromFile {
? ? public static final String DEPATH = "D:/test/deFile/test.jsp";
? ? public static final String IMAGEPATH = "C:\\Users\\Administrator\\Desktop\\logo.png";
? ?/*
? ?* 多種方式讀取文件內容
? ?* 1、按字節讀取文件內容
? ?* 2、按字符讀取文件內容
? ?* 3、按行讀取文件內容
? ?* 4、隨機讀取文件內容
? ?*/

? ?//1、按字節讀取文件內容

? ? /**
? ? *
? ? * 以字節為單位讀取文件,常用于讀二進制文件,如圖片、聲音、影像等文件
? ? * @param fileName 文件名
? ? * @return void
? ? */
? ? 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.print(tempByte+"segment");
? ? ? ? ? ? }
? ? ? ? ? ? in.close();
? ? ? ? }catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

? ? //2、按字符讀取文件內容

? ? /**
? ? *
? ? * 以字符為單位讀取文件,常用于讀文本,數字等類型的文件
? ? * @param fileName 文件名
? ? * @return void
? ? */
? ? 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下,rn這兩個字符在一起時,表示一次換行,如兩個字符分開時,表示兩次換行
? ? ? ? ? ? ? ? if((char)tempChar!='r'){
? ? ? ? ? ? ? ? System.out.print((char)tempChar);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? reader.close();
? ? ? ? }catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }


? ? //3、按行讀取文件內容

? ? /**
? ? *
? ? * 以行為單位讀取文件,常用于讀面向行的格式化文件
? ? * @param fileName 文件名
? ? * @return void
? ? */
? ? 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;
? ? ? ? ? ? while ((tempString=reader.readLine())!=null){
? ? ? ? ? ? ? ? ? ? System.out.println("line"+line+":"+tempString);
? ? ? ? ? ? ? ? ? ? ?line++;
? ? ? ? ? ? }
? ? ? ? ? ? reader.close();
? ? ? ? }catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }

? ? //4、隨機讀取文件內容

? ? /**
? ? *
? ? * @param fileName 文件名
? ? * @return void
? ? */
? ? public ?static void readFileByRandomAccess(String fileName){
? ? ? ? RandomAccessFile randomAccessFile = null;
? ? ? ? try {
? ? ? ? ? ? System.out.println("隨機讀取一段文件內容:");
? ? ? ? ? ? //打開一個隨機訪問文件流,按只讀方式
? ? ? ? ? ? randomAccessFile = new RandomAccessFile(fileName,"r");
? ? ? ? ? ? //文件長度,字節數
? ? ? ? ? ? long fileLength = randomAccessFile.length();
? ? ? ? ? ? //將讀文件的開始位置
? ? ? ? ? ? int beginIndex = (fileLength>4)?4:0;
? ? ? ? ? ? //將讀文件的開始位置移到beginIndex位置
? ? ? ? ? ? randomAccessFile.seek(beginIndex);
? ? ? ? ? ? byte[] bytes = ?new byte[10];
? ? ? ? ? ? int ?byteRead = 0;
? ? ? ? ? ? //一次讀10個字節,如果文件內容不足10個字節,則讀剩下的字節。
? ? ? ? ? ? while ((byteRead = randomAccessFile.read(bytes))!=-1){
? ? ? ? ? ? ? ? System.out.write(bytes,0,byteRead);
? ? ? ? ? ? }
? ? ? ? }catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }finally {
? ? ? ? ? ? if (randomAccessFile!=null){
? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ?randomAccessFile.close();
? ? ? ? ? ? ? ?}catch (Exception e){
? ? ? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ? ? ?}

? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? public static void main(String[] args){
// ? ? ? ?readFileByBytes(IMAGEPATH);
// ? ? ? ?readFileByChars(DEPATH);
// ? ? ? ?readFileByLines(DEPATH);
? ? ? ? readFileByRandomAccess(DEPATH);
? ? }

}

注:僅供分享,內容參照其他學者

轉載于:https://my.oschina.net/u/3378039/blog/1560037

總結

以上是生活随笔為你收集整理的多种方式读取文件内容的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。