I/O with Java

I/O Streams

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

  • System.out  : standard output stream
  • System.in     : standard input stream
  • System.err   : standard error stream

Getting input from Scanner

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);
    }
}

Getting input from BufferedReader

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);
    }   
}

Reading File

Reading File use Scanner

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();         
    }    
}

Reading File use FileInputStream

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();
    }    
}

Reading File use BufferedReader

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();
  }  
}

Writing File

Writing File use BufferedWriter (1)

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(); 
    }
    
}

Writing File use BufferedWriter (2)

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(); 
    }
    
}

Writing File use FileOutputStream

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);
    }    
}

I/O in Java

By Nur Ratna Sari

I/O in Java

  • 69