我正在用Java编写一些代码,这些代码将能够为股票投资组合项目读取和写入文本文件。我可以读写,但当我尝试在同一个runner类中读两次文件时,它首先输出内容,然后输出两次内容,而不是两次输出相同的内容。
相关类别是(请原谅它们的长度(:
import java.util.ArrayList;
import java.io.*;
public class FileModifier {
private String path;
private int numOfLines;
private ArrayList<String[]> arr;
private BufferedReader br;
private BufferedWriter bw;
public FileModifier(String p){
path = p;
numOfLines = 0;
arr = new ArrayList<String[]>();
}
public void read(){
try{
br = new BufferedReader(new FileReader(path));
String line;
while ((line = br.readLine()) != null) {
String[] row = line.split("tt");
arr.add(row);
numOfLines++;
}
br.close();
}
catch(Exception e){
}
}
public void write(){
try{
read();
bw = new BufferedWriter(new FileWriter(path));
String content = "";
for (String[] line : arr) {
for (int i = 0; i < line.length; i++) {
content += (line[i] + "tt");
}
content += "r";
}
bw.write(content);
bw.close();
}
catch(Exception e){
}
}
public ArrayList<String[]> getContents(){
read();
return arr;
}
public int getNumberOfLines(){
read();
return numOfLines;
}
public String[] getLine(int n){
read();
return arr.get(n);
}
public void addLine(int n, String[] newLine){
read();
arr.set(n, newLine);
}
}
和
import java.util.ArrayList;
abstract public class Account {
private FileModifier file;
public Account(String historyPath){
file = new FileModifier(historyPath);
}
abstract public double getBalance();
public void addToHistory(String[] newLine){
ArrayList<String[]> contents = file.getContents();
contents.add(newLine);
file.write();
}
public String getHistory(){
String history = "";
for(String[] line: file.getContents()){
for(String part: line){
history += part + "tt";
}
history += "n";
}
return history;
}
}
和
import java.util.ArrayList;
import java.util.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
public class StockAccount extends Account{
private ArrayList<String[]> arr;
private DecimalFormat df;
private SimpleDateFormat da;
private int numOfShares;
private double portfolioValue;
private double balance;
private FileModifier portfolio;
public StockAccount(String p){
super("C:\stock_transaction_history.txt");
portfolio = new FileModifier(p);
numOfShares = portfolio.getNumberOfLines();
arr = portfolio.getContents();
portfolioValue = 10000;
balance = 10000;
df = new DecimalFormat(".##");
da = new SimpleDateFormat("HH:mm:ss");
}
public String displayPortfolio(){
String output = "";
String[] temp;
for(int i = 0; i < numOfShares; i++){
temp = arr.get(i);
for(int k = 0; k < temp.length; k++){
output += temp[k];
output += "tt";
}
if(i < numOfShares - 1)
output += "r";
}
return output;
}
public double getStockPrice(String symbol){
for(String[] line: arr){
if(line[0].equals(symbol))
return Double.parseDouble(line[2]);
}
return -1;
}
public int getAmountOwned(String symbol){
for(String[] line: arr){
if(line[0].equals(symbol))
return Integer.parseInt(line[1]);
}
return -1;
}
public double getStockValue(String symbol){
int amt = getAmountOwned(symbol);
double price = getStockPrice(symbol);
if(amt == -1 || price == -1){
return -1;
}
return amt * price;
}
public double getBalance(){
double value = 0;
for(int i = 0; i < arr.size()/2; i++){
value += getStockValue(arr.get(i)[0]);
}
balance = Double.parseDouble(df.format(portfolioValue - value));
return balance;
}
}
现在,当我尝试做时
StockAccount sa = new StockAccount("C:\Result.txt");
System.out.println(sa.getHistory());
System.out.println();
System.out.println(sa.getHistory());
我得到
Event Symbol Owned Price Value Time
Buy GOOG 10 $577.49 $5774.90 09:40:07
Buy MSFT 100 $30.00 $3000.00 13:37:00
Event Symbol Owned Price Value Time
Buy GOOG 10 $577.49 $5774.90 09:40:07
Buy MSFT 100 $30.00 $3000.00 13:37:00
Event Symbol Owned Price Value Time
Buy GOOG 10 $577.49 $5774.90 09:40:07
Buy MSFT 100 $30.00 $3000.00 13:37:00
我已经仔细阅读了我的代码,但我没有看到任何文件会重复。是不是有一些我不知道的关于文件阅读的东西?
此时应该使用调试器进入检测模式。为什么它恰好是两次的实际内容第二次time?内容存储在什么变量中?它何时附加到?你会不会两次那样做都是偶然的?故意地
仔细看看你是如何与FileModifier#arr交互的。
这与实现public void read()
的方式有关。您的getHistory()
调用file.getContents()
,后者调用read((。Read为您的ArrayList<String[]> arr
添加元素。则CCD_ 5返回CCD_。请注意,每次调用getHistory
时,arr
都将保持增长。如果你调用它一次,你就会得到预期的结果,但如果你多次调用它,你就会开始看到代码中的异常。
我不知道你在用什么工具,但这里有一个关于eclipse调试的好教程,或者如果你喜欢视频教程,那就自己动手吧。