Java:读/写文件等

时间:2014-02-25 15:49:09

标签: java bufferedreader java-io

我用2个方法创建了一个类,它应该处理文件写入或从中读取。

我想出了类似的东西:

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可以给我任何例子=)

1 个答案:

答案 0 :(得分: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”存储在项目的源文件中的假设,但是您可以轻松地更改抓住绝对路径的代码段到您正在查找的文件夹的硬编码路径。希望这有帮助!