gpt4 book ai didi

java - 我的 Java 代码不断向文本文件添加重复项

转载 作者:行者123 更新时间:2023-12-02 04:38:24 25 4
gpt4 key购买 nike

我正在用 Java 编写一些代码,这些代码将能够读取和写入股票投资组合项目的文本文件。我能够读取和写入,但是当我尝试在同一个运行程序类中读取文件两次时,它首先输出内容,然后输出两倍的内容,而不是输出相同的内容两次。

相关类(class)是(请原谅其长度):

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

我已经阅读了我的代码,但我没有看到文件会重复的任何地方。关于文件读取是否有一些我不知道的事情?

最佳答案

此时您应该使用调试器进入侦探模式。为什么它的第二次时间恰好是实际内容的两倍?内容存储在哪个变量中?什么时候追加的?你会不会无意中重复了两次?故意的吗?

仔细看看您是如何与 FileModifier#arr 交互的。

关于java - 我的 Java 代码不断向文本文件添加重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30473248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com