集册 Java实例教程 从文件获取源代码

从文件获取源代码

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

405
从文件获取源代码


//package com.nowjava;/* from n o w  j a v a  . c o m*/

import java.io.*;


public class Main {

    public static void main(String[] argv) throws Exception {

        File file = new File("Main.java");

        System.out.println(getSourceCodeFromFile(file));

    }


    public static String getSourceCodeFromFile(File file) {

        String sourceCode = "";

        if (file == null) {

            return sourceCode;/*来自 时   代    Java - nowjava.com*/

        }

        try {

            Reader reader = new FileReader(file);

            sourceCode = getString(reader);

        } catch (IOException e) {

            throw new RuntimeException(e);

        }

        return sourceCode;

    }


    private static String getString(Reader reader) throws IOException {

        String text = "";

        BufferedReader bufferedReader = 
展开阅读全文