我目前正在研究以下方法:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+"
<information>
<title>Title</title>
<vendor>Me</vendor>
<offline-allowed/>
</information>
<resources>
<jar href="myApp.jar" main="true" />
<j2se version="1.6+" href="http://java.sun.com./products/autodl/j2se"/>
</resources>
<application-desc>
name="myApp"
main-class="application.Main"
</application-desc>
</jnlp>
方法是简单地获取存储在堆栈public void addExpenditure(Stack<Double> costStack){
try(BufferedWriter bw = new BufferedWriter(new FileWriter(f.getPath(), true))) {
while (costStack.size() != 0)
bw.write(costStack.pop().toString() + " ,");
}catch(Exception e){
e.printStackTrace();
}
}
中的值并将它们写入文件中。但是,由于未知的原因,这没有发生。
相反,每次都不会将任何内容写入文件。这很奇怪,因为 this 方法完全按照应有的方式工作,并且几乎完全相同:
costStack
也不会引发异常。
以下是我目前用来完成此“工作”的所有方法:
public void addExpenditure(double amount){
Double amount1 = amount;
try(BufferedWriter bf = new BufferedWriter(new FileWriter(f.getPath(), true))){
bf.write(amount1.toString() + " , ");
}catch(IOException e){
e.printStackTrace();
}
}
不同的课程文件:
import java.util.Scanner;
import java.util.Stack;
public class BudgetMain {
/*
Pre-Tax income per month: $5875
After-Tax income per month: $4112
Savings Goal per month (61% of After-Tax income): $2508.62
Expendable income per month (After-Tax income - Savings Goal Per month): $1604
*/
public static void main(String[] args){
try {
Budget b = new Budget();
b.clearFile();
System.out.println("Available funds left to spend this month: " + b.availableFundsLeft());
Stack<Double> costStack = new Stack<>();
costStack = takeValues(costStack);
b.addExpenditure(costStack);
System.out.println("Available funds left to spend this month: " + b.availableFundsLeft());
}catch(Exception e){e.printStackTrace();}
}
public static Stack<Double> takeValues(Stack<Double> costStack){
System.out.println("Enter appropriate expenditures");
Scanner stdin = new Scanner(System.in);
while(true) {
costStack.push(stdin.nextDouble());
if (costStack.peek() == -1){
costStack.pop();
return costStack;
}
}
}
}
答案 0 :(得分:1)
此代码似乎有效,它创建了一个内容为2.0,1.0的test.out文件,
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
public class BudgetMain {
public static void main(String[] args){
Stack<Double> stack = new Stack<>();
stack.push(1.0);
stack.push(2.0);
addExpenditure1(stack, new File("test.out"));
}
public static void addExpenditure1(Stack<Double> costStack, File f){
try(BufferedWriter bw = new BufferedWriter(new FileWriter(f.getPath(), true))) {
while (costStack.size() != 0)
bw.write(costStack.pop().toString() + " ,");
}catch(Exception e){
e.printStackTrace();
}
}
}