stockstorage.java

来自「prims code」· Java 代码 · 共 38 行

JAVA
38
字号
import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.util.*;


public class StockStorage
{
    public static void writeStock(Stock stock, DataOutputStream outputStream) throws IOException
    {
        // Push the number of shares into a byte array.
        outputStream.writeInt( stock.getNumShares() );

        // Then push the stock name.
        outputStream.writeUTF( stock.getSymbol() );

        // push the stock price
        outputStream.writeInt( stock.getPrice() );
    }

    public static Stock readStock(DataInputStream inputStream) throws IOException
    {
        // read the number of shares
        int numShares = inputStream.readInt();

        // read the stock symbol
        String symbol = inputStream.readUTF();

        // read the stock price
        int price = inputStream.readInt();

        // return a new Stock object
        return new Stock( symbol, numShares, price);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?