集册 Java实例教程 文件读取到字符串

文件读取到字符串

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

523
文件读到字符串
//N o w J a v a . c o m 提供


//package com.nowjava;

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 void main(String[] argv) throws Exception {

        String filePath = "nowjava.com";

        System.out.println(file_read(filePath));

    }


    public static String file_read(String filePath) {

        try {

            FileInputStream fin = new FileInputStream(new File(filePath));

            String ret = convertStreamToString(fin);

            fin.close();

            return ret;

        } catch (Exception e) {/**from 时代Java**/

            return "";

        }

    }


    public static String convertStreamToString(InputStream is)

            throws Exception {

        BufferedReader reader = new BufferedReader(

                new InputStreamReader(is));

        StringBuilder sb = 
展开阅读全文