2014-01-23 31 views
0

我已經學習了關於Java的基礎課,但我的知識很少。自從我越來越熟悉Java以來​​,我在過去的一兩個月裏創建了一個基於文本的RPG。我想知道是否有任何方法可以讓程序創建一個「保存」文件存儲在某個文件夾中,並提示用戶是否希望打開保存的字符。我還沒有學習Java的任何面向對象的部分。我能做些什麼來實現這一點?如何創建可以再次讀取的文本文件?

+0

查看File類,InputStreams,OutputStreams,FileReader和FileWriter。還有其他一些有用的讀者和作者,這將使讀寫文件變得更容易。 – tfandango

+0

開始閱讀[Java Tutorials](http://docs.oracle.com/javase/tutorial/reallybigindex.html),您將在其中找到如何做到這一切以及更多,包括一些「面向對象的部分」。 –

回答

1

我想知道是否有什麼辦法,我可以有程序創建存儲在特定的文件夾 「保存」的文件,並提示如果 他們想開一個保存字符的用戶。

寫入和讀取文本文件是一種可靠的初學者方式來保存遊戲狀態。

有很多方法可以write to files listed here,但這裏有一個例子:

PrintWriter out = new PrintWriter("filename.txt");//create the writer object 
out.println(text);//write 
out.close() //when you're done writing, this will save 

現在,這裏讀取文件found from here一個簡單的方法:

BufferedReader br = new BufferedReader(new FileReader("filename.txt"));//objects to read with 
try { 
    StringBuilder sb = new StringBuilder();//use to build a giant string from the file 
    String line = br.readLine(); 

    //loop file line by line 
    while (line != null) { 
     sb.append(line); 
     sb.append(System.lineSeparator()); 
     line = br.readLine(); 
    } 
    String everything = sb.toString(); 
} finally { 
    br.close();//always close! 
} 

有很多在線教程只是一個谷歌遠。 | = ^]

注意Always remember to close readers/writers/scanners/etc. Else wise you'll get resource errors which you can read more about here.

+0

@Relytnevir Java中的一些方法會拋出異常。你必須告訴代碼讓它被拋出或被捕獲。有多種方式來處理這個錯誤。我建議在我的'try'和'finally'塊之間加上'catch'語句。這裏有很多需要閱讀的內容:http://docs.oracle.com/javase/tutorial/essential/exceptions/ –

+0

謝謝你的幫助。在這個遊戲工作幫助了我很多的話題。我只是改變它使用父類和子類來清理代碼一點點。我嘗試使用這個代碼,但它給了我這個錯誤:未報告的異常FileNotFoundException;必須被捕獲或宣佈被拋出。你能向我解釋發生了什麼事嗎? @MohammadS。 – Relytnevir

-1

這是我建,以保存文本類,

import java.io.*; 
import javax.swing.JFileChooser; 
import javax.swing.JOptionPane; 

public class Savetext { 

    /** 
    * Saves text to a <i>.txt</i> file with the specified name, 
    * 
    * @param name the file name without the extension 
    * 
    * @param txt the text to be written in the file 
    * 
    * @param message the message to be displayed when done saving, if <tt>null</tt> no 
    * message will be displayed 
    */ 
    public static void Save(String name, String txt, String message) { 
     name += ".txt"; 
     gtxt(name, txt, message); 
    } 

    private static void gtxt(String name, String txt, String mess) { 
     JFileChooser fc = new JFileChooser(); 
     fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
     File file = null; 
     fc.showSaveDialog(null); 
     file = fc.getSelectedFile(); 
     String path = file.getPath(); 
     path += "\\"; 
     path += name; 

     File f1 = new File(path); 
     try { 
      PrintWriter out = new PrintWriter(new BufferedWriter(
        new FileWriter(f1))); 
      wrtxt(out, txt, mess); 
     } catch (IOException e) { 

      JOptionPane.showMessageDialog(null, e.getMessage().toString()); 
     } 

    } 

    private static void wrtxt(PrintWriter out, String txt, String mess) { 
     out.println(txt); 
     out.flush(); 
     out.close(); 
     if (mess != null) 
      JOptionPane.showMessageDialog(null, mess); 
    } 

} 

應該叫靜態保存()方法,未做實例該類,

+1

給OP大塊的代碼沒有解釋是不是一個初學者的好方法... –

+0

正確,但這就是我現在有, –

+1

@MohammadS .:他不只是張貼代碼沒有解釋,但更糟的是,他張貼* **糟糕的***代碼,包括以與創建它的方式不同的方式關閉PrintWriter,從而阻止在循環中重用它或在finally代碼塊中關閉它的能力,並將GUI代碼組合在相同塊作爲文件I/O代碼,應該避免的事情。不僅如此,這個問題與GUI或Swing編碼沒有任何關係。 –

相關問題