0

I have written a class that is capable of reading a .dat file and print the contents to screen so I can read them. So far, this prints everything necessary for me to review.

public class WriteDefinitions {

    private static String   SHOP_NAME_CONTAINS = "Example";
    private static Integer  SHOP_ITEM_ID = 0;
    private static Integer  SHOP_QTY_AMT = 0;
    //private static int[][]    NEW_ITEMS = new int[][] { {itemId, itemQty}, {itemId, itemQty} };
    private static int[][]  NEW_ITEMS = new int[][] { {3, 10}, {4, 10} };

    public static void main(String[] args) {
        if(SHOP_NAME_CONTAINS != "") pullShopInformation();
    }

    private static void pullShopInformation() {
        int lineNumber = 0;
        try {
            byte abyte2[] = FileOperations.readFile("./data/content/Shops.dat");
            Stream stream2 = new Stream(abyte2);
            lineNumber = stream2.readUnsignedWord();
            System.out.println("Total shops: " + lineNumber);
            System.out.println("");
            for (int i = 0; i < lineNumber; i++) {
                String name = stream2.readString();
                if(name.contains(SHOP_NAME_CONTAINS)) {
                    int type = stream2.readUnsignedByte();
                    int listOfShopItems = stream2.readUnsignedByte();
                    for (int j = 0; j < listOfShopItems; j++) {
                        int itemId = stream2.readUnsignedWord();
                        int itemQty = stream2.readUnsignedWord();
                        System.out.println("Shop #: " + i + "   |   Item #: " + itemId + "  |   Qty: " + itemQty);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Here's a print of a shop named Example

Shop #: 110 |   Item #: 8   |   Qty: 10
Shop #: 110 |   Item #: 6   |   Qty: 0
Shop #: 110 |   Item #: 12  |   Qty: 10
Shop #: 110 |   Item #: 10  |   Qty: 10
Shop #: 110 |   Item #: 4   |   Qty: 10
Shop #: 110 |   Item #: 5   |   Qty: 10

How would I make changes inside of the .dat file?

In the above print out for shop # 110 item 6 has qty 0. How could I update the qty inside of the .dat to 10? Also how would I append new items (NEW_ITEMS[][]) to a specific shop, or even further create a new shop altogether?

I realize I need to use writeFile() but am struggling to understand how to use it. FileOperations.writeFile(".data/content/Shops.dat", newItemQty?);

I am using DataInputStream to read the file. Here's the FileOperations class.

public class FileOperations {


    public FileOperations() {
    }

    public static final byte[] readFile(String s) {
        try {
            File file = new File(s);
            int i = (int)file.length();
            byte abyte0[] = new byte[i];
            DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(s)));
            dataInputStream.readFully(abyte0, 0, i);
            dataInputStream.close();
            totalRead++;
            return abyte0;
        } catch(Exception exception) {
            System.out.println((new StringBuilder()).append("Read Error: ").append(s).toString());
        }
        return null;
    }

    public static final void writeFile(String s, byte abyte0[]) {
        try {
            (new File((new File(s)).getParent())).mkdirs();
            FileOutputStream fileOutputStream = new FileOutputStream(s);
            fileOutputStream.write(abyte0, 0, abyte0.length);
            fileOutputStream.close();
            totalWrite++;
            completeWrite++;
        } catch(Throwable throwable) {
            System.out.println((new StringBuilder()).append("Write Error: ").append(s).toString());
        }
    }

    public static boolean fileExists(String file) {
    File f = new File(file);
        if(f.exists()) {
            return true;
        } else {
            return false;
        }
    }


    public static int totalRead = 0;
    public static int totalWrite = 0;
    public static int completeWrite = 0;
}
Jordan
  • 35
  • 1
  • 5
  • 2
    So, what is your question? – NomadMaker Feb 02 '20 at 01:35
  • I edited the main post with additional information. I do not know how to begin using the WriteFile() method to update the .dat file with either modifications/new records. – Jordan Feb 02 '20 at 01:47
  • 1
    You may also want to follow the traditional java standards and start method names with a lowercase letter. It will make the code more readable. – NomadMaker Feb 02 '20 at 01:49
  • Overwriting can be tricky. But here's a similar queston: [write x'th row](https://stackoverflow.com/questions/6166386/write-xth-row-in-file-in-java) – Scratte Feb 02 '20 at 02:23
  • Does this answer your question? [How to add a new line of text to an existing file in Java?](https://stackoverflow.com/questions/4614227/how-to-add-a-new-line-of-text-to-an-existing-file-in-java) – Scratte Feb 02 '20 at 02:27

0 Answers0