- Java
- Basics
- Classes
- Objects
- Arrays
- Variables
- Loops
- Numbers
- Strings
- Exceptions
- Regexp
- OOP
- Inheritance
- Polymorphism
- Static Keyword
- Abstract Keyword
- Interfaces
- Constructors
- Packages
- Nested Classes
- Final Keyword
- Swing
- Frame
- Panel
- Listener
- Combo Box
- Label
- Image
- Menu
- Table
- Layout
- Drawing
- Timer
- Designer
- Collections
- Lists
- Comparable
- Sets
- Maps
- Generics
- Properties
- Streams
- Json
- Compiler
- Sublime Text
- Apache Ant
- I/O
- Streams IO ♣
- Socket
- Watching Files
- Logger
- Clipboard
- Encrypt
- Junit
- About Junit
- Test Case
- Suite Test
- Annotations
- Exceptions
- JavaFX
- Openjfx
- Scene Builder
- First App
- Jar Archive
- On Action
- Change Listener
- Threads
- Create Thread
- Sleep
- Lock
- Scheduler
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();
}
}
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();
}
}
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);
}
}
}
}
Last update: 61 days ago