2014-01-30 52 views
-4

我想做一個程序,它執行時間表,它將一個名爲log.txt的文件輸出爲一個日誌文件,但是當我運行它時,它所做的只是運行,並且執行時間表並生成文件,但什麼都不寫入文件。有人可以告訴我,我的代碼有什麼問題嗎?如果你這樣做,謝謝。使用Writer後,爲什麼我的文件中沒有輸出?

重點:

  • [=的.jar文件可運行]
  • [.ZIP =源代碼]

下載鏈接: - http://wardogsk93-ftp.bugs3.com/Downloads/Java/Counter/

Java文檔: - http://wardogsk93-ftp.bugs3.com/Downloads/Java/Counter/Java%20Doc/

Main.java :-

public class Main { 

    /************************************************/ 
    /*************STUFF YOU CAN CHANGE***************/ 
    /************************************************/ 

    /** change this to start at a different number must be a number {@link Integer}**/ 
    public static int minCount = 1; 

    /** change to change the number of where the programme will end must be a number {@link Integer}**/ 
    public static int maxCount = 10; 

    /** change this to how many times you want to sleep for in seconds (1 = 1 second, 2 = 2 second, 10 = 10 second) before moving to next sum must be a number {@link Integer} **/ 
    public static int sleepAmountMultiplyer = 1; 

    /** true = outputs to the command prompt/false = outputs to eclipse console {@link boolean}**/ 
    public static boolean outputTOCMD = true; 

    /************************************************/ 
    /******DONT CHANGE ANYTHING BELOW THIS LINE******/ 
    /************************************************/ 

    /** allows to output to a command prompt **/ 
    private static Console cmd; 
    private static Output file; 

    private static int endNumber = maxCount + 1; 
    private static int sleepAmount = 1000 * sleepAmountMultiplyer; 

    /** 
    * main method 
    * call this to start 
    **/  
    public static void start() { 
     file = new Output(); 

     if (outputTOCMD) { 

      cmd = new Console(); 

      count(); 

      cmd.exit(); 
     } else { 
      count(); 

      System.exit(1);   
     } 
    } 

    public static Main getInstance() { 
     return Main.getInstance(); 
    } 

    /**code to start running**/ 
    private static void count() { 
     try { 
      for (int i = minCount; i < maxCount + 1; i++) { 
       int j = i * i; 

       Thread.sleep(sleepAmount); 

       if (i == endNumber) { 
        return; 
       } 

       if (outputTOCMD) { 
        cmd.out(i + " X " + i + " = " + j); 

        file.write(String.valueOf(i) + " X " + String.valueOf(i) + " = " + String.valueOf(j)); 
       } else { 
        System.out.println(i + " X " + i + " = " + j); 

        file.write(String.valueOf(i) + " X " + String.valueOf(i) + " = " + String.valueOf(j)); 
       } 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

Output.class: -

import java.io.*; 

public class Output { 
    public Output() {} 

    private Console cmd; 
    private File logFile; 
    private String input; 
    private BufferedReader reader; 
    private BufferedWriter writer; 

    public Output(Console cmd, File logFile, BufferedReader reader, BufferedWriter writer) { 
     this.cmd = cmd; 
     this.logFile = logFile; 
     this.reader = reader; 
     this.writer = writer; 
    } 

    /** writes to a log file using {@link FileWriter} **/ 
    public void write(String message) { 
     try { 
      logFile = new File("log.txt"); 
      writer = new BufferedWriter(new FileWriter(logFile)); 

      if (!logFile.exists()) { 
       writer.write(message); 
       writer.close(); 
      } else { 
       read(); 
       if (logFile.isFile()) { 
        logFile.delete(); 

        writer.write(message); 
       } 
      } 
     } catch (IOException e) { 
      if (Main.outputTOCMD) { 
       cmd.out(e.getMessage()); 
      } else { 
       e.printStackTrace(); 
      } 
     } 
    } 

    /** writes to a log file using {@link FileReader} **/ 
    public void read() { 
     try {   
      logFile = new File("log.txt"); 
      reader = new BufferedReader(new FileReader(logFile)); 

      if (logFile.exists()) { 
       setInput(reader.readLine()); 
      } 
     } catch (IOException e) { 
      if (Main.outputTOCMD) { 
       cmd.out(e.getMessage()); 
      } else { 
       e.printStackTrace(); 
      } 
     } 
    } 

    /** 
    * @return the input 
    */ 
    private String getInput() { 
     return input; 
    } 

    /** 
    * @param input the input to set 
    */ 
    private String setInput(String input) { 
     this.input = input; 
     return input; 
    } 
} 

Console.class: -

import java.awt.Color; 
import java.awt.Image; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 

/** 
* Creates the command prompt window in class {@link Console} 
*/ 
public class Console { 
    private JFrame frame; 
    private JTextArea console; 
    private Image icon; 

    public Console() { 
     try { 
      frame = new JFrame(); 
      frame.setBackground(Color.BLACK); 
      frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
      frame.setLocationRelativeTo(null); 
      frame.setName("Commad Prompt"); 
      frame.setSize(678, 340); 
      frame.setTitle(frame.getName()); 
      frame.setVisible(true); 

      icon = new ImageIcon(new URL("http://upload.wikimedia.org/wikipedia/en/e/ef/Command_prompt_icon_(windows).png")).getImage(); 
      frame.setIconImage(icon); 

      console = new JTextArea(); 
      console.setAutoscrolls(true); 
      console.setBackground(Color.BLACK); 
      console.setEditable(false); 
      console.setForeground(Color.WHITE); 
      console.setSelectionColor(Color.WHITE); 
      console.setSelectedTextColor(Color.BLACK); 
      console.setVisible(true); 

      frame.add(console); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * @param {@link String} text does the same as {@link System}.out.println(); 
    */ 
    public void out(String text) { 
     console.append(text + "\n"); 
    } 

    /** 
    * @exception {@link Exception} to catch any errors and prints them to the window 
    * does the same has {@link System}.exit(1); 
    */ 
    public void exit() { 
     try { 
      Thread.sleep(1000 * Main.sleepAmountMultiplyer); 

      console.disable(); 
      frame.dispose(); 
      System.exit(1); 
     } catch (Exception e) { 
      this.out(e.getMessage()); 
     } 
    } 

    /** 
    * @return Allows you to acces all the stuff in <br>{@link Console}</br> 
    **/ 
    public Console getInstance() { 
     return this; 
    } 
} 


Launch File:- 


public class Test { 
    public static void main(String[] args) { 
     Main.minCount = 1; 
     Main.maxCount = 10; 
     Main.sleepAmountMultiplyer = 1; 
     Main.outputTOCMD = true; 

     Main.start(); 
    } 
} 
+1

調試它怎麼樣? –

+0

或者爲它編寫測試? –

+0

我試過調試它,但它所做的只是運行並生成文件但不記錄它 – WARDOGSK93

回答

2

當完成寫入文件必須始終使用close()方法。否則,它不會拯救它(you also want to close to avoid resource leak errors...read here)。因此,在此方法中:

public void write(String message) { 
    try { 
     logFile = new File("log.txt"); 
     writer = new BufferedWriter(new FileWriter(logFile)); 

     if (!logFile.exists()) { 
      writer.write(message); 
      writer.close(); 
     } else { 
      read(); 
      if (logFile.isFile()) { 
       logFile.delete(); 

       writer.write(message); 
      } 
     } 
     //close the buffer writer in order to save 
     writer.close(); 
    } catch (IOException e) { 
     if (Main.outputTOCMD) { 
      cmd.out(e.getMessage()); 
     } else { 
      e.printStackTrace(); 
     } 
    } 
} 

或者,您可以在finally塊中關閉。閱讀完畢後,您還必須關閉BufferReader。如果您計劃對多個Thread讀取/寫入同一文件,則在使用Thread時需要非常小心。

注意:這將每次覆蓋文件。但是,如果你想追加數據,改變這一行:

writer = new BufferedWriter(new FileWriter(logFile)); 

要:

writer = new BufferedWriter(new FileWriter(logFile, true)); 

FileWriter第二個參數,確認是否要覆蓋該文件或追加到文件。看看這example

+0

感謝Mohammad S.生病現在嘗試它 – WARDOGSK93

+0

它現在的工作,但我怎麼會讓它讀取文件並保留其內容並添加新的東西刪除舊的行在'log.txt'必須像讀者一樣。關(); – WARDOGSK93

+0

這是一個完整的其他問題,但我更新了我的答案 –

相關問題