2014-02-25 80 views
1

我用2種方法創建了一個類,它們應該處理寫入文件或讀取文件。Java:讀取/寫入文件等

伊夫想出了這樣的事情:

package YBot; 
import java.io.*; 



public class FollowerChecker { 



public static StringBuilder sb; 


    static String readFile(String fileName) throws IOException { 
     BufferedReader br = new BufferedReader(new FileReader(fileName)); 
     try { 
      sb = new StringBuilder(); 
      String line = br.readLine(); 

      while (line != null) { 
       sb.append(line); 
       sb.append("\n"); 
       line = br.readLine(); 

      } 
      return sb.toString(); 
     } finally { 
      br.close(); 

     } 
    } 



    public static void Writer() { 

     FileWriter fw = null; 

     try { 
      fw = new FileWriter("donottouch.txt"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     StringWriter sw = new StringWriter(); 
     sw.write(TwitchStatus.totalfollows); 


     try { 
      fw.write(sw.toString()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     try { 
      fw.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

現在我的問題是:

我如何添加創建「donottouch.txt」文件中的函數,如果犯規存在,它已經或者它的空寫入「0」呢?當我的程序啓動時,它會讀取一個數字的文件,稍後,如果數字發生改變,它會重寫它。所以它最好是隻要它試圖閱讀,而它不在那裏,它就會立刻創建並重讀它。希望some1可以給我任何的例子=)

+0

你可以閱讀http://docs.oracle.com/javase/tutorial/essential/io/ –

回答

0

這是我如何處理它:

public static boolean checkIfExists(String path) { 
    if (!new File(path).exists()) { 
     return false; 
    } else { 
     return true; 
    } 
} 

public static String readFile(String file) throws IOException { 
    BufferedReader reader = new BufferedReader(new FileReader (file)); 
    String line; 
    StringBuilder sb = new StringBuilder(); 

    while((line = reader.readLine()) != null) { 
     sb.append(line); 
    } 
    reader.close(); 
    return sb.toString(); 

} 

public static void writeFile(String path) throws FileNotFoundException, 
               UnsupportedEncodingException { 
    PrintWriter writer = new PrintWriter(path, "UTF-8"); 
    writer.println("0"); 
    writer.close(); 
    return; 
} 

public static void main(String args[]) { 
    /*Gets absolute path to your project folder, assuming that is where 
    * you are storing this text file. Otherwise hard code your path 
    * accordingly. 
    */ 
    File file = new File(""); 
    String fileGet = file.getAbsolutePath(); 
    StringBuilder sb = new StringBuilder(); 
    String path = sb.append(fileGet.toString() + "/donottouch.txt").toString(); 

    String result=null; 

    if(!checkIfExists(path)) { 
     try { 
      writeFile(path); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 
     System.out.println("File created: 'donottouch.txt'"); 

    } else { 
     try { 
      result = readFile(path); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     if(result.length() == 0) { 
      try { 
       writeFile(path); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 
      System.out.println("File amended: 'donottouch.txt'"); 
     } 
     System.out.println("File exists: 'donottouch.txt'"); 
    } 
} 

很顯然,我創建了一個主類,做這一切之外的一類中,不像你,但它應該是整合非常簡單。它的前提是假設您將「donottouch.txt」存儲在項目的源文件中,但是您可以輕鬆更改將您的絕對路徑截取到您要查找的文件夾的硬編碼路徑的代碼片段。希望這可以幫助!

+0

所以我將如何調用你的代碼來寫我想要的文件的字符串?在我剛剛調用FollowerChecker.Writer()的代碼上;它確實將字符串「totalfollows」寫入文件。我只是不明白你的「路徑」。該文件位於項目源中。 – user3220962

+0

路徑是文件的絕對路徑。它只是一個結構爲「/Users/YourName/PathToYourProjectFolder/.../donottouch.txt」的字符串,所以傳遞路徑基本上是傳入文件。這樣,您不必在文件夾移動的情況下對整個路徑進行硬編碼。 checkIfExists()方法正在做的是檢查項目路徑中是否存在「donottouch.txt」文件,如果存在,則通過readFile()方法讀取它。從這裏,如果它是空的或者它不存在,則通過writeFile()方法在內部寫入「0」。 – Tgsmith61591

+0

我回答了你的問題嗎?您可能無法使用框架的**精確**結構,但可以將其中的方法合併到您的代碼中。希望這至少有助於一些:) – Tgsmith61591