Skip to content

SACHSTech/ICS4U-File-IO

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

File I/O Example

Here is an example method that uses BufferedReader and a try-finally block to read the contents of a file line-by-line, outputting to the console. The output ends when we reach the null character, indicating the end of the file.

See Main.java in in the folder src/fileIODemo.

In this example, we close the file we attempted to open, regardless of success or IOException error.

Example

package fileIODemo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        displayWords("src/fileIODemo/words.txt");
    }


    public static void displayWords(String filename) throws IOException {
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(filename));
            String word;

            // Read each line (word) from the file
            while ((word = reader.readLine()) != null) {
                System.out.println(word);  // Print each word (line) to the console
            }
        } finally {
            if (reader != null) {
                reader.close();  // Ensure the reader is closed
            }
        }
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages