集册 Java实例教程 从文件中获取字符串

从文件中获取字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

471
从文件获取字符串


//package com.nowjava;
/**
n o w j a   v  a . c o m - 时  代  Java 提供 
**/

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;


public class Main {



    public static String getStringFromFile(String filePath)

            throws Exception {

        FileInputStream fin = new FileInputStream(new File(filePath));
        /* from 
        N o  w  J a v a . c o m - 时  代  Java*/

        String ret = convertStreamToString(fin);

        fin.close();

        return ret;

    }


    public static String convertStreamToString(InputStream is)

            throws Exception {

        BufferedReader reader = new BufferedReader(

                new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();

        
展开阅读全文