Java Text File Write nio writeDataFromList(String address, ArrayList toWrite)

Here you can find the source of writeDataFromList(String address, ArrayList toWrite)

Description

write Data From List

License

Open Source License

Declaration

public static void writeDataFromList(String address, ArrayList<BigInteger> toWrite) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.math.BigInteger;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public class Main {
    public static void writeDataFromList(String address, ArrayList<BigInteger> toWrite) throws IOException {
        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);//  ww w .  j av a 2 s. c o  m
        }
        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);
    }

    public static void writeData(String address, BigInteger toWrite) throws IOException {
        Path path = Paths.get(address);
        byte[] data = toWrite.toByteArray();
        Files.write(path, data);
    }
}

Related

  1. writeArray2File(File file, Object[] objects, String code)
  2. writeAscii(CharSequence ascii, OutputStream os)
  3. writeContent(File file, String content)
  4. writeContent(File file, String content)
  5. writeData(String address, BigInteger toWrite)
  6. writeFileContent(File file, String content)
  7. writeFileContents(File file, String content)
  8. writeFileTxt(String fileName, String[] totalFile)
  9. writeHeader(DataOutput data, String header)