Java Utililty Methods Text File Write nio

List of utility methods to do Text File Write nio

Description

The list of methods to do Text File Write nio are organized into topic(s).

Method

voidwrite(String msg)
write
Path path = Paths.get("D:\\Program Files\\apache-activemq-5.10.0\\data\\test.txt");
try {
    Files.write(path, msg.getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
    throw new RuntimeException(msg, e);
voidwriteAllLines(File file, List list)
write All Lines
writeAllLines(file, list.toArray(new String[] {}));
voidwriteAllText(File file, String contents)
write All Text
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter out = new OutputStreamWriter(fos, ENCODING);
out.write(contents);
out.close();
voidwriteArray2File(File file, Object[] objects, String code)
write Array File
try {
    String myCode = code != null && !"".equals(code) ? code : Charset.defaultCharset().name();
    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), myCode);
    BufferedWriter writer = new BufferedWriter(out);
    for (int i = 0; i < objects.length; i++) {
        writer.write(objects[i].toString());
        if (i < objects.length - 1)
            writer.newLine();
...
voidwriteAscii(CharSequence ascii, OutputStream os)
write Ascii
os.write(ascii.toString().getBytes(StandardCharsets.US_ASCII));
voidwriteContent(File file, String content)
Writes the string content to the file.
File tempFile = new File(file.getAbsolutePath() + "." + Long.toHexString(System.currentTimeMillis()));
try {
    file.getAbsoluteFile().getParentFile().mkdirs();
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(tempFile),
            Charset.forName("UTF-8"));
    BufferedWriter writer = new BufferedWriter(os);
    writer.append(content);
    writer.close();
...
voidwriteContent(File file, String content)
Writes the string content to the file.
OutputStreamWriter os = null;
try {
    os = new OutputStreamWriter(new FileOutputStream(file), Charset.forName("UTF-8"));
    BufferedWriter writer = new BufferedWriter(os);
    writer.append(content);
    writer.flush();
} catch (Throwable t) {
    System.err.println("Failed to write content of " + file.getAbsolutePath());
...
voidwriteData(String address, BigInteger toWrite)
write Data
Path path = Paths.get(address);
byte[] data = toWrite.toByteArray();
Files.write(path, data);
voidwriteDataFromList(String address, ArrayList toWrite)
write Data From List
ArrayList<Byte[]> data = new ArrayList<>();
for (BigInteger bi : toWrite) {
    byte[] byteArr = bi.toByteArray();
    Byte[] ByteArr = new Byte[byteArr.length];
    for (int i = 0; i < byteArr.length; i++)
        ByteArr[i] = byteArr[i];
    data.add(ByteArr);
int lastElLen = 0;
for (lastElLen = 0; lastElLen < data.get(data.size() - 1).length; lastElLen++) {
    if (data.get(data.size() - 1)[lastElLen] == 0) {
        break;
byte[] combinedArr = new byte[((data.size() - 1) * data.get(0).length) + lastElLen];
int j = 0;
for (Byte[] arr : data) {
    for (Byte arrEl : arr) {
        combinedArr[j] = arrEl;
        j++;
        if (j == combinedArr.length)
            break;
BigInteger combined = new BigInteger(combinedArr);
writeData(address, combined);
voidwriteFileContent(File file, String content)
Writes the content of the string to the (UTF-8 encoded) file.
BufferedWriter writer = getBufferedUTF8Writer(file);
writer.write(content);
writer.flush();
writer.close();