Skip to content

Latest commit

 

History

History
73 lines (54 loc) · 1.19 KB

IO.md

File metadata and controls

73 lines (54 loc) · 1.19 KB

Java IO

BufferedReader

inline drawing

A BufferedReader is used to read data from a source in Java

Syntax:

  • Path variable to the .txt Document
public void readFile(String path){
    try{
    BufferedReader reader = new BufferedReader(new FileReader(path))
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

Getting data from the Reader:

try{
String output = reader.readLine();
int age =Integer.parseInt( reader.readLine());
double weight = Double.parseDouble(reader.readLine());
}
catch(IOException e){
    e.printStackTrace();
}

BufferedWriter

A BufferedWriter is used to "write" data to a specific destination (eg. a .txt file)

Syntax:

  • Path variable to the .txt Document
public void writeToFile(String path){
    try{
    BufferedWriter writer = new BufferedWriter(new FileWriter(path))
    }
    catch(IOException e){
        e.printStackTrace();
    }
}

Writing Data with the Writer:

try{
writer.write(age);
writer.write(name);
writer.write(weight);
}
catch(IOException e){
    e.printStackTrace();
}