2014-02-25 83 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