集册 Java实例教程 使用BufferedReader读取字符串

使用BufferedReader读取字符串

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

1007
使用BufferedReader读取字符串

//package com.nowjava;

import java.io.BufferedReader;

import java.io.File;
/*
来 自*
 时 代 J a v a
*/

import java.io.FileReader;

import java.io.IOException;


public class Main {

    public static void main(String[] argv) {

        String fileName = "nowjava.com";

        System.out.println(readString(fileName));

    }

    /* 
     来自 
    *n o w j a v a . c o m - 时  代  Java*/

    public static String readString(String fileName) {

        File file = new File(fileName);

        return readString(file);

    }


    public static String readString(File file) {

        StringBuilder builder = new StringBuilder();

        if (!file.exists()) {

            System.err.println("Can't Find " + file);

        }


        try {

            BufferedReader in = new BufferedReader(new FileReader(file));

            String str;

            while ((str = in.readLine()) != null) {

                
展开阅读全文