Created
October 29, 2022 16:32
-
-
Save rmg007/88baa9a0493234cd8ec68d24d7b504ca to your computer and use it in GitHub Desktop.
FileInputStream FileOutputStream 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.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
public class FileInputStreamOutputStreamExample { | |
/* | |
J a v a - i s - a w e s o m e | |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 | |
*/ | |
//FileOutputStream is meant for writing streams of raw bytes such as PDFs, images, MP3s, MP4s, ... | |
// FileInputStream is meant for reading streams of raw bytes such as PDFs, images, MP3s, MP4s, ... | |
static String filePath = "/Users/ryan/Desktop/file.txt"; | |
public static void main(String[] args) throws FileNotFoundException, IOException { | |
//try( FileOutputStream fos = new FileOutputStream(filePath)) { | |
// String sent = "Java is awesome"; | |
// //fos.write(65); | |
// //fos.write(66); | |
// | |
// //fos.write(sent.getBytes()); | |
// | |
// fos.write(sent.getBytes(), 5, sent.length() - 5); | |
//} | |
try(FileInputStream fis = new FileInputStream(filePath)){ | |
//byte[] bytes = fis.readAllBytes(); | |
//String readText = new String(bytes); | |
//System.out.println(readText); | |
//byte[] bytes = new byte[fis.available()]; | |
//int read = fis.read(bytes); | |
//System.out.println(read); | |
//String readText = new String(bytes); | |
//System.out.println(readText); | |
int i; | |
while ((i = fis.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