1
我不是一個java程序員,我是一名VB程序員。我將此作爲一項任務的一部分,但是,我並沒有要求有關任務的幫助。在這種情況下,我想弄清楚如何讓OutputStreamWriter
正常工作。我只想捕獲我生成的值並將它們放入文本文檔中。該文件生成,但只有一個條目存在,而不是我期待的40。我可以用VB來做到這一點,但是現在我覺得Java很奇怪。感謝您的幫助。試圖編寫一個使用OutputStream寫入文本文件的循環
感謝,
史蒂夫
下面的代碼:
public static void main(String[] args) {
long start, end;
double result,difference;
try {
//OutputStream code assistance from
// http://tutorials.jenkov.com/java-io/outputstreamwriter.html
OutputStream outputStream = new FileOutputStream("c:\\Temp\\output1.txt");
Writer out = new OutputStreamWriter(outputStream);
for(int n=1; n<=20; n++) {
//Calculate the Time for n^2.
start = System.nanoTime();
//Add code to call method to calculate n^2
result = mN2(n);
end = System.nanoTime();
difference = (end - start);
//Output results to a file
out.write("N^2 End time: " + end + " Difference: " +
difference + "\n");
out.close();
}
} catch (IOException e){
}
try {
OutputStream outputStream = new FileOutputStream("c:\\Temp\\output1.txt");
Writer out = new OutputStreamWriter(outputStream);
for(int n=1; n<=20; n++){
//Calculate the Time for 2^n.
start = System.nanoTime();
//Add code to call method to calculate 2^n
result = m2N(n);
end = System.nanoTime();
difference = (end - start);
//Output results to a file
out.write("N^2 End time: " + end + " Difference: " + difference + "\n");
out.close();
}
} catch (IOException e){
}
}
//Calculate N^2
public static double mN2(double n) {
n = n*n;
return n;
}
//Calculate 2N
public static double m2N(double n) {
n = 2*n;
return n;
}