[BAEL-19087]-Move articles out of core-java-io part 2
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
package com.baeldung.bufferedreader;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class BufferedReaderExample {
|
||||
|
||||
public String readAllLines(BufferedReader reader) throws IOException {
|
||||
StringBuilder content = new StringBuilder();
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
content.append(line);
|
||||
content.append(System.lineSeparator());
|
||||
}
|
||||
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
public String readAllLinesWithStream(BufferedReader reader) {
|
||||
return reader
|
||||
.lines()
|
||||
.collect(Collectors.joining(System.lineSeparator()));
|
||||
}
|
||||
|
||||
public String readAllCharsOneByOne(BufferedReader reader) throws IOException {
|
||||
StringBuilder content = new StringBuilder();
|
||||
|
||||
int value;
|
||||
while ((value = reader.read()) != -1) {
|
||||
content.append((char) value);
|
||||
}
|
||||
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
public String readMultipleChars(BufferedReader reader) throws IOException {
|
||||
int length = 5;
|
||||
char[] chars = new char[length];
|
||||
int charsRead = reader.read(chars, 0, length);
|
||||
|
||||
String result;
|
||||
if (charsRead != -1) {
|
||||
result = new String(chars, 0, charsRead);
|
||||
} else {
|
||||
result = "";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public String readFile() {
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
reader = new BufferedReader(new FileReader("src/main/resources/input.txt"));
|
||||
String content = readAllLines(reader);
|
||||
return content;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
try {
|
||||
if (reader != null) {
|
||||
reader.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String readFileTryWithResources() {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/input.txt"))) {
|
||||
String content = readAllLines(reader);
|
||||
return content;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.dirmonitoring;
|
||||
|
||||
import org.apache.commons.io.monitor.FileAlterationListener;
|
||||
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
|
||||
import org.apache.commons.io.monitor.FileAlterationMonitor;
|
||||
import org.apache.commons.io.monitor.FileAlterationObserver;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class DirectoryMonitoringExample {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DirectoryMonitoringExample.class);
|
||||
|
||||
private static final int POLL_INTERVAL = 500;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
FileAlterationObserver observer = new FileAlterationObserver(System.getProperty("user.home"));
|
||||
FileAlterationMonitor monitor = new FileAlterationMonitor(POLL_INTERVAL);
|
||||
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
|
||||
@Override
|
||||
public void onFileCreate(File file) {
|
||||
LOG.debug("File: " + file.getName() + " created");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFileDelete(File file) {
|
||||
LOG.debug("File: " + file.getName() + " deleted");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFileChange(File file) {
|
||||
LOG.debug("File: " + file.getName() + " changed");
|
||||
}
|
||||
};
|
||||
observer.addListener(listener);
|
||||
monitor.addObserver(observer);
|
||||
monitor.start();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user