Adding files for the article BAEL-2257: Guide to OutputStream (#5386)
This commit is contained in:
parent
f046857975
commit
11dd36cb1a
@ -0,0 +1,48 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
public class OutputStreamExamples {
|
||||
|
||||
public void fileOutputStreamByteSequence(String file, String data) throws IOException {
|
||||
byte[] bytes = data.getBytes();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
out.write(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public void fileOutputStreamByteSubSequence(String file, String data) throws IOException {
|
||||
byte[] bytes = data.getBytes();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
out.write(bytes, 6, 5);
|
||||
}
|
||||
}
|
||||
|
||||
public void fileOutputStreamByteSingle(String file, String data) throws IOException {
|
||||
byte[] bytes = data.getBytes();
|
||||
try (OutputStream out = new FileOutputStream(file)) {
|
||||
out.write(bytes[6]);
|
||||
}
|
||||
}
|
||||
|
||||
public void bufferedOutputStream(String file, String... data) throws IOException {
|
||||
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
|
||||
for (String s : data) {
|
||||
out.write(s.getBytes());
|
||||
out.write(" ".getBytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void outputStreamWriter(String file, String data) throws IOException {
|
||||
try (OutputStream out = new FileOutputStream(file); Writer writer = new OutputStreamWriter(out, "UTF-8")) {
|
||||
writer.write(data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package com.baeldung.stream;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class OutputStreamExamplesTest {
|
||||
|
||||
StringBuilder filePath = new StringBuilder();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
filePath.append("src");
|
||||
filePath.append(File.separator);
|
||||
filePath.append("test");
|
||||
filePath.append(File.separator);
|
||||
filePath.append("resources");
|
||||
filePath.append(File.separator);
|
||||
filePath.append("output_file.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStream_whenWriteSingleByteCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.fileOutputStreamByteSingle(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStream_whenWriteByteSequenceCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.fileOutputStreamByteSequence(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStream_whenWriteByteSubSequenceCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.fileOutputStreamByteSubSequence(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBufferedOutputStream_whenCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.bufferedOutputStream(filePath.toString(), "Hello", "World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutputStreamWriter_whenCalled_thenOutputCreated() throws IOException {
|
||||
|
||||
final File file = new File(filePath.toString());
|
||||
OutputStreamExamples examples = new OutputStreamExamples();
|
||||
examples.outputStreamWriter(filePath.toString(), "Hello World!");
|
||||
assertTrue(file.exists());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user