我用Java编写了一些能够读取和编写股票投资组合项目文本文件的代码。我能够读写,但是当我尝试在同一个流程类中读取文件两次时,它首先输出内容然后输出两倍的内容,而不是两次输出相同的内容。
相关课程(并请原谅他们的长度):
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("\t\t");
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] + "\t\t");
}
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 + "\t\t";
}
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 += "\t\t";
}
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
我已经阅读了我的代码,但我没有看到任何文件会被复制的地方。是否有关于文件阅读的内容,我不知道?
答案 0 :(得分:1)
这时您应该使用调试器进入侦探模式。为什么第二时间的实际内容是两次?内容存储在什么变量中?什么时候附加?你偶然两次这样做吗?故意吗?
仔细看看你如何与FileModifier#arr进行互动。
答案 1 :(得分:1)
这与您实施public void read()
的方式有关。您的getHistory()
调用file.getContents()
调用read()。 Read会向您的ArrayList<String[]> arr
添加元素。 getContents()
然后返回arr
。请注意,每次拨打arr
时getHistory
都会继续增长。如果你调用一次,就会得到预期的结果,但是如果你多次调用它,你会开始在代码中看到异常。
我不知道您使用的是什么工具,但是here is a good tutorial on debugging with eclipse或者如果您喜欢视频教程,knock yourself out.