Skip to content

Commit 11dd36c

Browse files
kcacademicpivovarit
authored andcommitted
Adding files for the article BAEL-2257: Guide to OutputStream (eugenp#5386)
1 parent f046857 commit 11dd36c

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.stream;
2+
3+
import java.io.BufferedOutputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.OutputStream;
7+
import java.io.OutputStreamWriter;
8+
import java.io.Writer;
9+
10+
public class OutputStreamExamples {
11+
12+
public void fileOutputStreamByteSequence(String file, String data) throws IOException {
13+
byte[] bytes = data.getBytes();
14+
try (OutputStream out = new FileOutputStream(file)) {
15+
out.write(bytes);
16+
}
17+
}
18+
19+
public void fileOutputStreamByteSubSequence(String file, String data) throws IOException {
20+
byte[] bytes = data.getBytes();
21+
try (OutputStream out = new FileOutputStream(file)) {
22+
out.write(bytes, 6, 5);
23+
}
24+
}
25+
26+
public void fileOutputStreamByteSingle(String file, String data) throws IOException {
27+
byte[] bytes = data.getBytes();
28+
try (OutputStream out = new FileOutputStream(file)) {
29+
out.write(bytes[6]);
30+
}
31+
}
32+
33+
public void bufferedOutputStream(String file, String... data) throws IOException {
34+
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
35+
for (String s : data) {
36+
out.write(s.getBytes());
37+
out.write(" ".getBytes());
38+
}
39+
}
40+
}
41+
42+
public void outputStreamWriter(String file, String data) throws IOException {
43+
try (OutputStream out = new FileOutputStream(file); Writer writer = new OutputStreamWriter(out, "UTF-8")) {
44+
writer.write(data);
45+
}
46+
}
47+
48+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.stream;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
public class OutputStreamExamplesTest {
12+
13+
StringBuilder filePath = new StringBuilder();
14+
15+
@Before
16+
public void init() {
17+
filePath.append("src");
18+
filePath.append(File.separator);
19+
filePath.append("test");
20+
filePath.append(File.separator);
21+
filePath.append("resources");
22+
filePath.append(File.separator);
23+
filePath.append("output_file.txt");
24+
}
25+
26+
@Test
27+
public void givenOutputStream_whenWriteSingleByteCalled_thenOutputCreated() throws IOException {
28+
29+
final File file = new File(filePath.toString());
30+
OutputStreamExamples examples = new OutputStreamExamples();
31+
examples.fileOutputStreamByteSingle(filePath.toString(), "Hello World!");
32+
assertTrue(file.exists());
33+
file.delete();
34+
}
35+
36+
@Test
37+
public void givenOutputStream_whenWriteByteSequenceCalled_thenOutputCreated() throws IOException {
38+
39+
final File file = new File(filePath.toString());
40+
OutputStreamExamples examples = new OutputStreamExamples();
41+
examples.fileOutputStreamByteSequence(filePath.toString(), "Hello World!");
42+
assertTrue(file.exists());
43+
file.delete();
44+
}
45+
46+
@Test
47+
public void givenOutputStream_whenWriteByteSubSequenceCalled_thenOutputCreated() throws IOException {
48+
49+
final File file = new File(filePath.toString());
50+
OutputStreamExamples examples = new OutputStreamExamples();
51+
examples.fileOutputStreamByteSubSequence(filePath.toString(), "Hello World!");
52+
assertTrue(file.exists());
53+
file.delete();
54+
}
55+
56+
@Test
57+
public void givenBufferedOutputStream_whenCalled_thenOutputCreated() throws IOException {
58+
59+
final File file = new File(filePath.toString());
60+
OutputStreamExamples examples = new OutputStreamExamples();
61+
examples.bufferedOutputStream(filePath.toString(), "Hello", "World!");
62+
assertTrue(file.exists());
63+
file.delete();
64+
}
65+
66+
@Test
67+
public void givenOutputStreamWriter_whenCalled_thenOutputCreated() throws IOException {
68+
69+
final File file = new File(filePath.toString());
70+
OutputStreamExamples examples = new OutputStreamExamples();
71+
examples.outputStreamWriter(filePath.toString(), "Hello World!");
72+
assertTrue(file.exists());
73+
file.delete();
74+
}
75+
76+
}

0 commit comments

Comments
 (0)