Skip to content

Instantly share code, notes, and snippets.

@rmg007
Created October 29, 2022 16:30
Show Gist options
  • Save rmg007/8c83e4735866ce9063f7244edc27f0fe to your computer and use it in GitHub Desktop.
Save rmg007/8c83e4735866ce9063f7244edc27f0fe to your computer and use it in GitHub Desktop.
File Reader Writer Example
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileReaderWriterExample {
//FileReader is meant for writing streams of characters
// File Writer is meant for writing streams of characters
static String filePath = "/Users/ryan/Desktop/file.txt";
public static void main(String[] args) throws IOException {
String sent = "java is awesome\n";
String sent2 = "line number 2\n";
try(FileWriter fw = new FileWriter(filePath)){
fw.write(sent);
fw.write(sent2);
}
try(FileReader fr = new FileReader(filePath)) {
int i;
while ((i = fr.read()) != -1){
System.out.println((char) i);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment