Skip to content

bcostaaa01/input-output-file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

📂 input-output-file

This Java program writes and reads input from a file.

⚙️ Codebase

  1. In order to use the tools we need to input and output data to a file, we need to import the packages from java.io
import java.io.BufferedReader; // read data from file
import java.io.BufferedWriter; // write data to file
import java.io.FileReader; // read file
import java.io.FileWriter; // write file
import java.io.IOException; // throw an exception
  1. Write the main class
public class writedata {}
  1. Create an array (for example) to store the data we want to input to the file
String[] sentences = { "Hey!", "My name is Bruno.", "I am a software developer.", "I enjoy programming with Java." };
  1. Create try and catch exceptions to write the data to file and read it afterwards
try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
            for (String sentence : sentences) { // loop through the sentences array 
                writer.write("\n" + sentence); // output the elements of the array
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new FileReader("output.txt"));
            String line;
            while ((line = reader.readLine()) != null) { // loop while line is not equal to null, when it is, stop looping
                System.out.println(line); // logs all the content from the file
            }
            reader.close(); // ends the reading of the file
        } catch (IOException e) {
            e.printStackTrace();
        }
  1. This should create a file called output.txt and then read it, logging all the lines written in it in the console/terminal.

Screenshot 2022-10-28 at 11 49 39 1

🎉🎉🎉

📚 Useful resources

About

This Java program writes and reads input from a file.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages