Created
October 29, 2022 16:30
-
-
Save rmg007/8c83e4735866ce9063f7244edc27f0fe to your computer and use it in GitHub Desktop.
File Reader Writer Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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