Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a code in java that load a CSV data file and transform it into a BINARY fi

ID: 3877521 • Letter: W

Question

Write a code in java that load a CSV data file and transform it into a BINARY file. You MUST name your binary file “data.bin”. Subsequent queries will then operate on the binary file. You could use the Java Data Streams and Object Streams classes, but you are free to design the format of the binary file (compression libraries are allowed!).

Have a method that transform the CSV file with filename into a binary file. The filename of the binary file should be stored in your program.

Have another method prints the rows of the table where the value in column number columnNum is equal to the given value. Column numbers start from one.

Have another method that prints the rows of the table where the value in column number columnNum is greater than the given value.

Here is an example of what in the cvs file:

1|3691|O|194029.55|1996-01-02|5-LOW|Clerk#000000951|0|nstructions sleep furiously among |
2|7801|O|60951.63|1996-12-01|1-URGENT|Clerk#000000880|0| foxes. pending accounts at the pending, silent asymptot|
3|12332|F|247296.05|1993-10-14|5-LOW|Clerk#000000955|0|sly final accounts boost. carefully regular ideas cajole carefully. depos|
4|13678|O|53829.87|1995-10-11|5-LOW|Clerk#000000124|0|sits. slyly regular warthogs cajole. regular, regular theodolites acro|
5|4450|F|139660.54|1994-07-30|5-LOW|Clerk#000000925|0|quickly. bold deposits sleep slyly. packages use slyly|
6|5563|F|65843.52|1992-02-21|4-NOT SPECIFIED|Clerk#000000058|0|ggle. special, final requests are against the furiously specia|
7|3914|O|231037.28|1996-01-10|2-HIGH|Clerk#000000470|0|ly special requests |

Explanation / Answer

Getting data from a csv file

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
* Simple Java program to read CSV file in Java. In this program we will read
* list of books stored in CSV file as comma separated values.
*
* @author WINDOWS 8
*
*/
public class CSVReaderInJava {

    public static void main(String... args) {
        List<Book> books = readBooksFromCSV("books.txt");

        // let's print all the person read from CSV file
        for (Book b : books) {
            System.out.println(b);
        }
    }

    private static List<Book> readBooksFromCSV(String fileName) {
        List<Book> books = new ArrayList<>();
        Path pathToFile = Paths.get(fileName);

        // create an instance of BufferedReader
        // using try with resource, Java 7 feature to close resources
        try (BufferedReader br = Files.newBufferedReader(pathToFile,
                StandardCharsets.US_ASCII)) {

            // read the first line from the text file
            String line = br.readLine();

            // loop until all lines are read
            while (line != null) {

                // use string.split to load a string array with the values from
                // each line of
                // the file, using a comma as the delimiter
                String[] attributes = line.split(",");

                Book book = createBook(attributes);

                // adding book into ArrayList
                books.add(book);

                // read next line before looping
                // if end of file reached, line would be null
                line = br.readLine();
            }

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return books;
    }

    private static Book createBook(String[] metadata) {
        String name = metadata[0];
        int price = Integer.parseInt(metadata[1]);
        String author = metadata[2];

        // create and return book of this metadata
        return new Book(name, price, author);
    }

}

class Book {
    private String name;
    private int price;
    private String author;

    public Book(String name, int price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book [name=" + name + ", price=" + price + ", author=" + author
                + "]";
    }

}

...

If you're referring to accessing the ACTUAL BINARY form then read in the file and convert every byte to a binary representation...


Here's some code to convert a byte into a string with the bits:
String getBits(byte b)
{
    String result = "";
    for(int i = 0; i < 8; i++)
        result += (b & (1 << i)) == 0 ? "0" : "1";
    return result;
}

If you're referring to accessing the bytes in the file then simply use the following code (you can use this for the first case as well):
File file = new File("filename.bin");
byte[] fileData = new byte[file.length()];
FileInputStream in = new FileInputStream(file);
in.read(fileData):
in.close();
// now fileData contains the bytes of the file

To use these two pieces of code you can now loop over every byte and create a String object (8X larger than the original file size!!) with the bits:
String content = "";
for(byte b : fileData)
    content += getBits(b);
// content now contains your bits.

        try {
            StringBuilder sb = new StringBuilder();
            File file = new File("C:/log.txt");
            DataInputStream input = new DataInputStream( new FileInputStream( file ) );
            try {
                while( true ) {
                    sb.append( Integer.toBinaryString( input.readByte() ) );
                }
            } catch( EOFException eof ) {
            } catch( IOException e ) {
                e.printStackTrace();
            }
            System.out.println(sb.toString());
        } catch( FileNotFoundException e2 ) {
            e2.printStackTrace();
        }

...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote