Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

public class Main {
    /**
     * Reads content of the file into string buffer
     * @param tempFile instance of java.io.File that points to an actual file on the file system.
     * @return instance of java.lang.StringBuffer
     */
    public static StringBuffer readFromFile(File tempFile) {
        StringBuffer stringBuffer = new StringBuffer();
        FileInputStream is = null;

        try {
            is = new FileInputStream(tempFile);

            byte[] buffer = new byte[1024];
            int count = 0;

            while ((count = is.read(buffer)) != -1) {
                stringBuffer.append(new String(buffer, 0, count));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return stringBuffer;
    }
}