Home>

I don't know how to do the following.
① Take out the second row of the array
② Calculate the total value of bytes
③Binary file read/write
④CSV file conversion

Particularly ① and ②.

Specifically, first, the specified format file is read, and the first line is taken out as a header and the second line is separated as a comma.
Next, the total number of bytes is returned as a list based on the second line extracted with commas.
Finally, based on the total number of bytes, read the binary file and convert the read binary file into a CSV file.

The source code is
ReadFormat method is the format call method,
totalByte method is the total byte value calculation method,
Binary method is a binary file read/write method,
CreateCsv method becomes CSV conversion method.

Because ① and ② are not possible, the method after the totalByte cannot be completed.

Thanks for your reply.
Thank you.

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
public class Search2 {
    final static String Rfile1 = "loc";
    final static String Rfile2 = "acc";
    final static String Rfile3 = "mag";

    public static void main (String [] args) throws IOException {
        // TODO Automatically generated method stub
        String formatNumber = args [1];
        String conversionFile = args [2];

        if (conversionFile.equals ("1")) {
            conversionFile = Rfile1;
        } else if (conversionFile.equals ("2")) {
            conversionFile = Rfile2;
        } else if (conversionFile.equals ("3")) {
            conversionFile = Rfile3;
        } else {
            System.out.println ("File is not found");
        }
        File fileName = new File ("Format" + "\\" + formatNumber + "\\" + conversionFile + ".fmt");
        // File filePath = new File ("../../../ Format" + "\\" + formatNumber + "\\" + conversionFile + ".fmt");
        // String filePath = fileName + "\\" + formatNumber + "\\" + conversionFile + ".fmt";

        String filePath = fileName.getCanonicalPath ();
        // file read
        ReadFormat (filePath, formatNumber, conversionFile);
        String text1 [] = new String [1];// totalByte list (1) as an argument, I return the total number of bytes
        // totalByte ();
        // Create CSV file
        // Binary ();
        return;
    }


    / **
     *
     * @param formatNumber
     * @return
     * @throws IOException
     * /

    private static void ReadFormat (String filePath, String formatNumber, String conversionFile) {

        try {
            int i = 0;
            String str = null;
            String [] text1 = new String [2];

            BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (filePath), "Shift-JIS"));
            while ((str = br.readLine ())! = null) {
                byte [] b = str.getBytes ();
                str = new String (b, "UTF-8");
                text1 [i] = str;
                i ++;
            }
            br.close ();

        } catch (Exception e) {
            System.out.println ("Conversion failed");
        }
        return;
    }
    / **
     *
     * @param createFileName
     * @param list
     * @return
     * /

    public static int totalByte () {


        int sum = 0;
        for (int a = 0;a<bytes.length;a ++) {
            sum + = bytes [a];
        }
        System.out.println (sum);
        return sum;
        }public static String Binary (List<String []>list1, String charset, String str, Integer len) {

        try {
        FileInputStream fis = new FileInputStream ("C: \\ Users \\ nakamura \\ Desktop \\ pleiades \\ workspace \\ Sample5 \\ 1801 \\ 20180602235854_f8ba5380f328eb51.mag");
        BufferedInputStream bis = new BufferedInputStream (fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream ();
        / * int cnt = 0;
        String sb = "";
        for (int c = 0;c<str.length ();c ++) {
            String tmpStr = str.substring (c, c + 1);
            byte [] b = tmpStr.getBytes (charset);
            if (cnt + b.length>len) {
                return sb.toString ();
            } else {
                //sb.append(tmpStr);
                cnt + = b.length;
            }
        } * /
        byte [] b = new byte [8];
        int readByte = 0;
        while ((readByte = bis.read (b))! = -1) {
            baos.write (b, 0, readByte);
            readByte = bis.read (b);
        }
        byte [] result = baos.toByteArray ();
        FileOutputStream fos = new FileOutputStream ("C: \\ Users \\ nakamura \\ Desktop \\ pleiades \\ workspace \\ Sample5 \\ 1801 \\ 20180602235854_f8ba5380f328eb51.mag");
        fos.write (result.length);
        fos.close ();
        bis.close ();
        baos.close ();
        } catch (IOException e) {
            e.printStackTrace ();
        }

        return str;
    }

    / * public static boolean CreateCsv () {
        try {
        PrintWriter pw = new PrintWriter (new BufferedWriter (new OutputStreamWriter (new FileOutputStream (), "UTF-8")));
        for (String [] tmpStringArray: list) {
            pw.println (String.join (",", tmpStringArray));
        }
        pw.close ();
    } catch (Exception e) {
        return false;
    }
    } * /
}

`` `