Java 讀取文件到字符串


Java的io操作比較復雜

package cn.outofmemory.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 文件操作代碼
 * 
 * @author cn.outofmemory
 * @date 2013-1-7
 */
public class FileUtils {
    /**
     * 將文本文件中的內容讀入到buffer中
     * @param buffer buffer
     * @param filePath 文件路徑
     * @throws IOException 異常
     * @author cn.outofmemory
     * @date 2013-1-7
     */
    public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
        InputStream is = new FileInputStream(filePath);
        String line; // 用來保存每行讀取的內容
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine(); // 讀取第一行
        while (line != null) { // 如果 line 為空說明讀完了
            buffer.append(line); // 將讀到的內容添加到 buffer 中
            buffer.append("\n"); // 添加換行符
            line = reader.readLine(); // 讀取下一行
        }
        reader.close();
        is.close();
    }

    /**
     * 讀取文本文件內容
     * @param filePath 文件所在路徑
     * @return 文本內容
     * @throws IOException 異常
     * @author cn.outofmemory
     * @date 2013-1-7
     */
    public static String readFile(String filePath) throws IOException {
        StringBuffer sb = new StringBuffer();
        FileUtils.readToBuffer(sb, filePath);
        return sb.toString();
    }
}

 

還有看到一段代碼,也差不多

package com.campu;
 
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
 
/**
 * @author 碼農小江
 * H20121012.java
 * 2012-10-12下午11:40:21
 */
public class H20121012 {
    /**
     * 功能:Java讀取txt文件的內容
     * 步驟:1:先獲得文件句柄
     * 2:獲得文件句柄當做是輸入一個字節碼流,需要對這個輸入流進行讀取
     * 3:讀取到輸入流后,需要讀取生成字節流
     * 4:一行一行的輸出。readline()。
     * 備注:需要考慮的是異常情況
     * @param filePath
     */
    public static void readTxtFile(String filePath){
        try {
                String encoding="GBK";
                File file=new File(filePath);
                if(file.isFile() && file.exists()){ //判斷文件是否存在
                    InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file),encoding);//考慮到編碼格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while((lineTxt = bufferedReader.readLine()) != null){
                        System.out.println(lineTxt);
                    }
                    read.close();
        }else{
            System.out.println("找不到指定的文件");
        }
        } catch (Exception e) {
            System.out.println("讀取文件內容出錯");
            e.printStackTrace();
        }
     
    }
     
    public static void main(String argv[]){
        String filePath = "L:\\Apache\\htdocs\\res\\20121012.txt";
//      "res/";
        readTxtFile(filePath);
    }
     
     
 
}

 

 

 

 

 

然后就可以根據需要對字符串進行操作了.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM