Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStreamReader;

import java.nio.charset.StandardCharsets;

public class Main {
    public static synchronized StringBuilder readTempFile(String filename) {
        File tempFile;
        FileInputStream is = null;

        tempFile = new File(filename);

        String strLine = "";
        StringBuilder text = new StringBuilder();

        /** Reading contents of the temporary file, if already exists */
        try {
            is = new FileInputStream(tempFile);
            //            FileReader fReader = new FileReader(tempFile);
            BufferedReader bReader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));

            /** Reading the contents of the file , line by line */
            while ((strLine = bReader.readLine()) != null) {
                text.append(strLine + "\n");
            }

            bReader.close(); //close bufferedreader
            is.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return text;

    }
}