Illuminatiiiiii
9/2/2018 - 5:26 PM

Reading Files

For Episode 54 of the Java Tutorial: https://youtu.be/2Ya1Db9P2cE

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sed tortor ac sapien rutrum ultrices. Phasellus tempor vitae ligula sit amet molestie. Vestibulum id velit vel urna elementum venenatis. Duis ac finibus risus. Praesent nec eros ut ligula vulputate dapibus vel sit amet orci. Nunc eleifend est quis suscipit sollicitudin. Etiam pharetra velit vitae enim pharetra, ut ultrices felis luctus. Mauris eget cursus enim, ac gravida arcu.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) {

        String fileName;
        System.out.println("Type the name of the file you want to read.");
        InputStreamReader ir = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(ir);
        try{
            int input; //where we will store the file input
            fileName = br.readLine(); //asks the user for the name of the file
            System.out.println("File name set: " + fileName);
            FileInputStream fi = new FileInputStream(fileName); //Creating a new file input byte stream
            do{
                input = fi.read(); //Reads a single ASCII byte number from the file
                if(input == -1){ //Says end of file when read generates -1, which is the number generated when there is no more input
                    System.out.println("End of File");
                }else{
                    System.out.println((char) input); //Outputs the input from the file
                }
            }while(input != -1); //Runs as long as input doesnt equal -1

            //Program done, closing file
            fi.close();
        }catch (IOException e){
            System.out.println(e);
        }
    }
}