Java按行读取文本文件

import java.io.*;

public class IoTest
{
    public static void main(String[] args)
    {
        String s;
        s=calculate();
        System.out.println(s);
    }

    static String calculate()
    {
        StringBuffer sb=new StringBuffer("");
        try{
            FileReader reader = new FileReader("words.txt");
            BufferedReader br = new BufferedReader(reader);
            String s = null;
            while((s = br.readLine()) != null) {
                sb.append(s+'\n');
            }
            br.close();
            reader.close();
       }catch(Exception e){
            e.printStackTrace();
       }
        return sb.toString();
    }
}
 

  出处:http://blog.chinaunix.net/u1/50399/showart_422957.html

你可能感兴趣的:(java,html,.net,Blog)