OOP实验4-文件

实验四 文件

实验四 文件
本实验4学时

  1. 实验目的:
    文件的基本操作. 了解异常处理.

  2. 实验环境与条件:
    JDK, NetBeans

  3. 实验内容:
    文本文件的读写. 使用附录的FileTool.java中的文件读写函数, 完成自己设定的功能目标.
    程序具体的功能/作用, 自己设定.

内容参考示例:
把一个Account对象, 保存到文件中; 从文件中读取数据, 形成Account对象.
文件account.txt格式如下:

账号1,余额1

附录:一个文件读写的示例
FileTool.java文件内容如下:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileTool {
    public static String read(String fileName) {
        StringBuilder content = new StringBuilder();
        try (FileInputStream fileInput = new FileInputStream(fileName);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInput);) {
            while (inputStreamReader.ready()) {
                content.append((char) inputStreamReader.read());
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return content.toString();
    }
    public static boolean write(String fileName, String content) {
        try (FileOutputStream fileOutput = new FileOutputStream(fileName);) {
            fileOutput.write(content.getBytes());
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }
}

Account.Java文件内容如下:

public class Account {
    private String accountID;
    private int balance;
    public void deposit(int amount) {
        this.balance += amount;
    }
    public void withdraw(int amount) {
        this.balance -= amount;
    }
    public Account() {
    }
    public Account(String accountID) {
        this.accountID = accountID;
    }
    public Account(String accountID, int balance) {
        this.accountID = accountID;
        this.balance = balance;
    }
    public int getBalance() {
        return balance;
    }
    public void setBalance(int balance) {
        this.balance = balance;
    }
    public String getAccountID() {
        return accountID;
    }
    public void setAccountID(String accountID) {
        this.accountID = accountID;
    }
}

AccountDao.java文件内容如下:

public class AccountDao {
    private String accountFileName;
    public AccountDao() {
    }
    public AccountDao(String accountFileName) {
        this.accountFileName = accountFileName;
    }
    public Account getAccount() {
        String content = FileTool.read(accountFileName);
        String[] items = content.split(",");
        Account account = new Account(items[0], Integer.parseInt(items[1]));
        return account;
    }
    public boolean saveAccounts(Account account) {
        String content = "";
        content += account.getAccountID() + "," + account.getBalance();
        return FileTool.write(accountFileName, content);
    }
}

主类MainClass.Java文件内容如下:

public class MainClass {
    public static void main(String[] args) {
        String accountFileName = "c:/javalab/account.txt";
        AccountDao accountDao = new AccountDao(accountFileName);
        Account account = accountDao.getAccount();
        System.out.println(account.getAccountID() + ":" + account.getAccountID());
        account.deposit(100);
        accountDao.saveAccounts(account);
        account = accountDao.getAccount();
        System.out.println(account.getAccountID() + ":" + account.getAccountID());
    }
}

你可能感兴趣的:(OOP)