JavaOCP/IO/IO/src/main/java/ocp/io/IO/App.java

139 lines
4.2 KiB
Java

package ocp.io.IO;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Arrays;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;;
/**
* I/O
*
*/
public class App
{
public static void main(String[] args) throws IOException
{
/**
* Creates a new file
*/
File dataFile = new File("/tmp/test.txt");
dataFile.createNewFile();
/**
* Create a stream to write to the file
*
* I will then use a kind-of Writer
* to write to it.
*
* The PrintWriter also flushes on
* any newline as specified by
* the argument provided below
*/
OutputStream out = new FileOutputStream(dataFile);
PrintWriter writer = new PrintWriter(out, true);
writer.println("ABBA");
writer.print("Gucci");
// writer.println();
/**
* Close the stream causes a flush of
* any buffered data
*/
writer.close();
/**
* A cool thing we can do is take the stream
* below and then wrap it in a BufferedInputStream
* which will read a certain amount on the first call
* to read (even more than we may have requested)
* and fill up an internal buffer.
*
* THis way if we seek back and forth and read
* we need not do several more system calls to
* request those bytes as they'd be buffered
* in for us and we are just moving back and forth
* in userspace memory.
*
* However, instead of creating it directly below
* I will actually use a similar mechanic provided
* byBufferedReader (it has to buffer because it
* can read delimited items - such as lines).
*/
// InputStream in = new BufferedInputStream(new FileInputStream(dataFile));
InputStream in = new FileInputStream(dataFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
/**
* Read two lines
*
* It has support for CR, LF, (we using this
* as the PrintWriter writes according to what
* is standard on your platform and on mine
* it is Linux/POSIX hence an LF)
* and CRLF
*/
String line1 = reader.readLine();
String line2 = reader.readLine();
System.out.println("Line 1: '"+line1+"'");
System.out.println("Line 2: '"+line2+"'");
/**
* Close it
*/
reader.close();
/**
* The below is to revise a question that was asked in the
* Java OCP exam (round 1)
*
* It was a loop that read into an array, a request of 8
* bytes each time and prints out the final array or
* something akin to that. It also loop whilst
* the read call did not yet reach the end of file.
*
* Given a file in `text.txt` containing solely
* `0123456789` (10 bytes), this should do one
* read, then another and then stop
*/
File testFile = new File("text.txt");
FileInputStream fileStream = new FileInputStream(testFile);
reader = new BufferedReader(new InputStreamReader(fileStream));
/**
* I believe they'd then print the string
* of this array of characters each loop
*
* The below would print:
*
* 01234567 (followed by) 89234567
*
* So it would print out: 0123456789234567
*/
char[] dataOut = new char[8];
while(reader.read(dataOut) != -1)
{
System.out.print(new String(dataOut));
}
// Final data will be `89234567`
System.out.println("\nFinal data: "+Arrays.toString(dataOut));
/**
* Close it all
*/
reader.close();
fileStream.close();
}
}