容易的問題是我可以使用其他函數來代替println,因爲我想輸出一個非靜態變量到文件usig out.println();println的非靜態替換
這是我的代碼:
import java.io.*;
public class main {
String outputString ="Math.sqrt(25);" ;
static String outputPath ="src/output.txt";
/**
* @param args
*/
public static void main(String[] args) throws IOException {
File f;
f= new File (outputPath);
//file creation
if(!f.exists()){
f.createNewFile();
System.out.println("File has been created");
}else{
f.delete();
System.out.println("1. File has been deleted");
f.createNewFile();
System.out.println("2. File has been created");
}
//adding string(text) to file
try{
FileWriter outFile = new FileWriter(args[0]);
PrintWriter out = new PrintWriter(outFile);
out.println(outputString);
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
如果不是更多鈔票,也許有一個整體的其他方式去解決它。我的主要問題是我想編一個字符串來代碼的和平。但這似乎很難做:)任何幫助:)
您當然可以使用_System.out.println_打印靜態和非靜態變量。你想做什麼不工作? – jahroy
@jahroy ..鑑於他沒有在靜態方法中進行。這是什麼困擾OP。 –
提示:當編譯器輸出一條錯誤消息,但你不明白它時:複製錯誤消息,並將其複製到Google:你會發現很多解釋它的含義以及如何修復你的代碼。 –