An I/O Stream represents an input source or an output destination. Streams support many different kind of data, including simple bytes, primitive data types, and objects.
In java, 3 streams are created for us automatically
import java.util.Scanner
public class IOExample {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number = in.nextInt();
char letter = in.nextLine().charAt(0);
String word = in.nextLine();
System.out.println("number: " + number);
System.out.println("lettert: " + letter);
System.out.println("word: " + word);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferExample {
public static void main(String[] args) throws IOException {
// Membuat objek inputstream
InputStreamReader isr = new InputStreamReader(System.in);
// Membuat objek bufferreader
BufferedReader br = new BufferedReader(isr);
System.out.print("Input your name: ");
String nama = br.readLine();
System.out.println("Your name is " + nama);
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class ReadingFile {
public static void main(String[] args) throws FileNotFoundException, IOException {
String pathFile = "C:\\Users\\Lenovo\\Documents\\input.txt";
File file = new File(pathFile);
Scanner in = new Scanner(file);
String line;
while(in.hasNextLine()) {
line = in.nextLine();
System.out.println(line);
}
in.close();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadingFile {
public static void main(String[] args) throws FileNotFoundException, IOException {
String pathFile = "C:\\Users\\Lenovo\\Documents\\input.txt";
FileInputStream fis = new FileInputStream(pathFile);
int ch;
while((ch = fis.read()) != -1) {
System.out.print((char)ch);
}
fis.close();
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReadingFile {
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("C:\\Users\\Lenovo\\Documents\\input.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WritingFile {
public static void main(String[] args) throws IOException {
String pathFile = "C:\\Users\\Lenovo\\Documents\\output.txt";
String str = "Hello";
BufferedWriter bw = new BufferedWriter(new FileWriter(pathFile));
bw.write(str);
bw.close();
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WritingFile {
public static void main(String[] args) throws IOException {
String pathFile = "C:\\Users\\Lenovo\\Documents\\output.txt";
String str = "World";
BufferedWriter bw = new BufferedWriter(new FileWriter(pathFile, true));
bw.append(' ');
bw.append(str);
bw.close();
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class JavaApplication3 {
public static void main(String[] args) throws FileNotFoundException, IOException {
String pathFile = "C:\\Users\\Lenovo\\Documents\\output.txt";
String str = "Hello Java";
FileOutputStream fos = new FileOutputStream(pathFile);
byte[] strToBytes = str.getBytes();
fos.write(strToBytes);
}
}