PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   29-07-2021

Sreams I/O

FileWriter is used to save data to a text file.
 
/**
 * Write text to a file
 * 
 * If the file does not exists it will be created
 */

import java.io.FileWriter;
import java.io.IOException;

class App {

    public static void main(String[] args) throws IOException {
            
        FileWriter writer = new FileWriter("Foo.txt");
        writer.write("hello foo!");
        writer.close();

    }
}
... 8 lines
 
Buffered input streams read data from a memory area known as buffer.
 
/**
 * Read text lines from a file
 * 
 * Foo.txt contains 2 lines:
 *      Hello Foo
 *      Hello Foo 2
 */

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class App {

    public static void main(String[] args) throws IOException {
            
        BufferedReader reader = new BufferedReader(
            new FileReader("./Foo.txt")
        );
        
        String line;
        while((line = reader.readLine()) != null) {
            System.out.println(line);
                // Hello Foo
                // Hello Foo 2
        }

        reader.close();
    }
}
... 18 lines
 
Read a line of chars from console with an InputStream.
 
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class App {
    public static void main(String[] args) throws Exception {

        InputStreamReader stream = new InputStreamReader(System.in);
        BufferedReader reader    = new BufferedReader(stream);
        
        a:while(true) {            
            String text = (String) reader.readLine();
            switch(text) {
                case "quit": 
                    break a;
                default:
                    System.out.println("User typed: " + text);
            }
        }
    }
}
... 11 lines
 

Questions    
Socket

        A B C D E F
🔔
1/1