多种方式读取文件内容
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
總結
以上是生活随笔為你收集整理的多种方式读取文件内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows远程Linux/Ubunt
- 下一篇: C语言第三次作业