|
@@ -24,32 +24,73 @@ import java.nio.file.Path;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.Iterator;
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.experimental.UtilityClass;
|
|
|
import net.ranides.assira.collection.arrays.ArrayAllocator;
|
|
import net.ranides.assira.collection.arrays.ArrayAllocator;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * Utility methods for quick read/write text into files and streams.
|
|
|
*
|
|
*
|
|
|
* @author ranides
|
|
* @author ranides
|
|
|
*/
|
|
*/
|
|
|
|
|
+@UtilityClass
|
|
|
public final class IOStrings {
|
|
public final class IOStrings {
|
|
|
-
|
|
|
|
|
- private IOStrings() { }
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Opens provided file for reading, returns Reader using provided charset
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param path path
|
|
|
|
|
+ * @param cs cs
|
|
|
|
|
+ * @return Reader
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static Reader reader(Path path, Charset cs) throws IOException {
|
|
public static Reader reader(Path path, Charset cs) throws IOException {
|
|
|
return reader(path.toFile(), cs);
|
|
return reader(path.toFile(), cs);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Opens provided file for reading, returns Reader using provided charset
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file file
|
|
|
|
|
+ * @param cs cs
|
|
|
|
|
+ * @return Reader
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static Reader reader(File file, Charset cs) throws IOException {
|
|
public static Reader reader(File file, Charset cs) throws IOException {
|
|
|
return new InputStreamReader(new FileInputStream(file), cs);
|
|
return new InputStreamReader(new FileInputStream(file), cs);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Opens provided file for write, returns Writer using provided charset
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param path path
|
|
|
|
|
+ * @param cs cs
|
|
|
|
|
+ * @return Writer
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static Writer writer(Path path, Charset cs) throws IOException {
|
|
public static Writer writer(Path path, Charset cs) throws IOException {
|
|
|
return writer(path.toFile(), cs);
|
|
return writer(path.toFile(), cs);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Opens provided file for write, returns Writer using provided charset
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file file
|
|
|
|
|
+ * @param cs cs
|
|
|
|
|
+ * @return Writer
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static Writer writer(File file, Charset cs) throws IOException {
|
|
public static Writer writer(File file, Charset cs) throws IOException {
|
|
|
return new OutputStreamWriter(new FileOutputStream(file), cs);
|
|
return new OutputStreamWriter(new FileOutputStream(file), cs);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes down content to writer and closes it.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param writer writer
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void write(Writer writer, String content) throws IOException {
|
|
public static void write(Writer writer, String content) throws IOException {
|
|
|
try {
|
|
try {
|
|
|
writer.write(content);
|
|
writer.write(content);
|
|
@@ -57,7 +98,15 @@ public final class IOStrings {
|
|
|
writer.close();
|
|
writer.close();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes down content to stream and closes it.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param ostream ostream
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void write(OutputStream ostream, String content, Charset charset) throws IOException {
|
|
public static void write(OutputStream ostream, String content, Charset charset) throws IOException {
|
|
|
try {
|
|
try {
|
|
|
write(new OutputStreamWriter(ostream, charset), content);
|
|
write(new OutputStreamWriter(ostream, charset), content);
|
|
@@ -65,15 +114,38 @@ public final class IOStrings {
|
|
|
ostream.close();
|
|
ostream.close();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes down content to file.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param path path
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void write(Path path, String content, Charset charset) throws IOException {
|
|
public static void write(Path path, String content, Charset charset) throws IOException {
|
|
|
write(path.toFile(), content, charset);
|
|
write(path.toFile(), content, charset);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- public static void write(File file, String content, Charset charset) throws IOException {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes down content to file.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file file
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void write(File file, String content, Charset charset) throws IOException {
|
|
|
write(new FileOutputStream(file), content, charset);
|
|
write(new FileOutputStream(file), content, charset);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reads all content from reader and closes it
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param reader reader
|
|
|
|
|
+ * @return String
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static String read(Reader reader) throws IOException {
|
|
public static String read(Reader reader) throws IOException {
|
|
|
try {
|
|
try {
|
|
|
char[] buffer = new char[32];
|
|
char[] buffer = new char[32];
|
|
@@ -88,6 +160,14 @@ public final class IOStrings {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reads all content from stream and closes it
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param istream istream
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @return String
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static String read(InputStream istream, Charset charset) throws IOException {
|
|
public static String read(InputStream istream, Charset charset) throws IOException {
|
|
|
try {
|
|
try {
|
|
|
return IOStrings.read(new InputStreamReader(istream, charset));
|
|
return IOStrings.read(new InputStreamReader(istream, charset));
|
|
@@ -96,14 +176,38 @@ public final class IOStrings {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reads all content from file
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param path path
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @return String
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static String read(Path path, Charset charset) throws IOException {
|
|
public static String read(Path path, Charset charset) throws IOException {
|
|
|
return read(path.toFile(), charset);
|
|
return read(path.toFile(), charset);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reads all content from file
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file file
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @return String
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static String read(File file, Charset charset) throws IOException {
|
|
public static String read(File file, Charset charset) throws IOException {
|
|
|
return read( new FileInputStream(file), charset );
|
|
return read( new FileInputStream(file), charset );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reads all lines from reader and closes it.
|
|
|
|
|
+ * Every newline format is supported.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param ireader ireader
|
|
|
|
|
+ * @return lines
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static List<String> readLines(Reader ireader) throws IOException {
|
|
public static List<String> readLines(Reader ireader) throws IOException {
|
|
|
ArrayList<String> result = new ArrayList<>();
|
|
ArrayList<String> result = new ArrayList<>();
|
|
|
try (BufferedReader reader = new BufferedReader(ireader)) {
|
|
try (BufferedReader reader = new BufferedReader(ireader)) {
|
|
@@ -115,19 +219,54 @@ public final class IOStrings {
|
|
|
result.trimToSize();
|
|
result.trimToSize();
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reads all lines from stream and closes it.
|
|
|
|
|
+ * Every newline format is supported.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param istream istream
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @return lines
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static List<String> readLines(InputStream istream, Charset charset) throws IOException {
|
|
public static List<String> readLines(InputStream istream, Charset charset) throws IOException {
|
|
|
return readLines(new BufferedReader(new InputStreamReader(istream, charset)));
|
|
return readLines(new BufferedReader(new InputStreamReader(istream, charset)));
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reads all lines from file.
|
|
|
|
|
+ * Every newline format is supported.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param path path
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @return lines
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static List<String> readLines(Path path, Charset charset) throws IOException {
|
|
public static List<String> readLines(Path path, Charset charset) throws IOException {
|
|
|
return Files.readAllLines(path, charset);
|
|
return Files.readAllLines(path, charset);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Reads all lines from file.
|
|
|
|
|
+ * Every newline format is supported.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file file
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @return lines
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static List<String> readLines(File file, Charset charset) throws IOException {
|
|
public static List<String> readLines(File file, Charset charset) throws IOException {
|
|
|
return readLines(new FileInputStream(file), charset);
|
|
return readLines(new FileInputStream(file), charset);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes all lines into writer and closes it.
|
|
|
|
|
+ * Uses system dependent line separator.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param iwriter iwriter
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void writeLines(Writer iwriter, Iterator<String> content) throws IOException {
|
|
public static void writeLines(Writer iwriter, Iterator<String> content) throws IOException {
|
|
|
try(BufferedWriter writer = new BufferedWriter(iwriter)) {
|
|
try(BufferedWriter writer = new BufferedWriter(iwriter)) {
|
|
|
while(content.hasNext()) {
|
|
while(content.hasNext()) {
|
|
@@ -136,31 +275,93 @@ public final class IOStrings {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes all lines into stream and closes it.
|
|
|
|
|
+ * Uses system dependent line separator.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param ostream ostream
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void writeLines(OutputStream ostream, Charset charset, Iterator<String> content) throws IOException {
|
|
public static void writeLines(OutputStream ostream, Charset charset, Iterator<String> content) throws IOException {
|
|
|
writeLines(new OutputStreamWriter(ostream, charset), content);
|
|
writeLines(new OutputStreamWriter(ostream, charset), content);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes all lines into file.
|
|
|
|
|
+ * Uses system dependent line separator.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param path path
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void writeLines(Path path, Charset charset, Iterator<String> content) throws IOException {
|
|
public static void writeLines(Path path, Charset charset, Iterator<String> content) throws IOException {
|
|
|
writeLines(path.toFile(), charset, content);
|
|
writeLines(path.toFile(), charset, content);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes all lines into file.
|
|
|
|
|
+ * Uses system dependent line separator.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file file
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void writeLines(File file, Charset charset, Iterator<String> content) throws IOException {
|
|
public static void writeLines(File file, Charset charset, Iterator<String> content) throws IOException {
|
|
|
writeLines(new FileOutputStream(file), charset, content);
|
|
writeLines(new FileOutputStream(file), charset, content);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes all lines into writer and closes it.
|
|
|
|
|
+ * Uses system dependent line separator.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param iwriter iwriter
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void writeLines(Writer iwriter, Iterable<String> content) throws IOException {
|
|
public static void writeLines(Writer iwriter, Iterable<String> content) throws IOException {
|
|
|
writeLines(iwriter, content.iterator());
|
|
writeLines(iwriter, content.iterator());
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes all lines into stream and closes it.
|
|
|
|
|
+ * Uses system dependent line separator.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param ostream ostream
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void writeLines(OutputStream ostream, Charset charset, Iterable<String> content) throws IOException {
|
|
public static void writeLines(OutputStream ostream, Charset charset, Iterable<String> content) throws IOException {
|
|
|
writeLines(ostream, charset, content.iterator());
|
|
writeLines(ostream, charset, content.iterator());
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes all lines into file.
|
|
|
|
|
+ * Uses system dependent line separator.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param path path
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void writeLines(Path path, Charset charset, Iterable<String> content) throws IOException {
|
|
public static void writeLines(Path path, Charset charset, Iterable<String> content) throws IOException {
|
|
|
writeLines(path.toFile(), charset, content);
|
|
writeLines(path.toFile(), charset, content);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes all lines into file.
|
|
|
|
|
+ * Uses system dependent line separator.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file file
|
|
|
|
|
+ * @param charset charset
|
|
|
|
|
+ * @param content content
|
|
|
|
|
+ * @throws IOException on error
|
|
|
|
|
+ */
|
|
|
public static void writeLines(File file, Charset charset, Iterable<String> content) throws IOException {
|
|
public static void writeLines(File file, Charset charset, Iterable<String> content) throws IOException {
|
|
|
writeLines(file, charset, content.iterator());
|
|
writeLines(file, charset, content.iterator());
|
|
|
}
|
|
}
|