PrintWriter没有写入当前目录

时间:2014-04-22 08:59:26

标签: java printwriter

我编写了一个程序来测试union-find,quick-union和加权快速联合算法,然后生成整数对,并使用PrintWriter将它们写入同一目录下的.txt文件。要在同一目录中找到该文件,我使用:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("\\unionfind\\data.txt");
File file = new File(url.toURI());

要生成随机的整数对,我使用这个类:

public class RandomIntPairGenerator {

public RandomIntPairGenerator(File file, int N){
    try(PrintWriter pw = new PrintWriter(file)){

        Random rnd = new Random();
        pw.println(N);

        for(int i = 0; i < N; i++){
            int p = rnd.nextInt(N);
            int q = rnd.nextInt(N);

            pw.println(p + " " + q);
        }
       }
       catch(Exception e){
            System.out.println(e.getCause());
        }

    }
}

但是每当程序结束时,我根本看不到文件的内容(它是空的,或者包含我以前写过的任何内容)。

这是主要的代码:

package unionfind;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Scanner;

public class UnionFind {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    oldMain();
}

static void oldMain() {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL url = classLoader.getResource("\\unionfind\\data.txt");
    try{
        File file = new File(url.toURI());

        final int N = 25000;

        /*StopWatch stopWatchUF = new StopWatch();
        unionFind(file);
        System.out.println("Union find has taken " + stopWatchUF.getElapsed());

        StopWatch stopWatchQU = new StopWatch();
        quickUnion(file);
        System.out.println("Quick union has taken " + stopWatchQU.getElapsed());

        StopWatch stopWatchWQU = new StopWatch();
        weightedQuickUnion(file);
        System.out.println("Weighted quick union has taken " + stopWatchWQU.getElapsed());*/

        for(int i = 5000; i <= N; i += 5000){
            RandomIntPairGenerator rnd = new RandomIntPairGenerator(file, i);

            System.out.println("\n\nGenerating for " + i + "\n\n");

            StopWatch stopWatchUF = new StopWatch();
            unionFind(file);
            System.out.println("Union find has taken " + stopWatchUF.getElapsed());

            StopWatch stopWatchQU = new StopWatch();
            quickUnion(file);
            System.out.println("Quick union find has taken " + stopWatchQU.getElapsed());

            StopWatch stopWatchWQU = new StopWatch();
            weightedQuickUnion(file);
            System.out.println("Weighted quick union has taken " + stopWatchWQU.getElapsed());
        }

    }
    catch(Exception e){
        System.out.println(e.getCause());
    }
}

static void unionFind(File file) throws FileNotFoundException {
    Scanner sc = new Scanner(file);

    System.out.println("Union find");

    int pairsNumUF = sc.nextInt();
    UF uf = new UF(pairsNumUF);

    for(int i = 0; i < pairsNumUF; i++){
        int p = sc.nextInt();
        int q = sc.nextInt();

        if(uf.connected(p, q)){
            //System.out.println(p + " and " + q + " are already connected.");
            continue;
        }

        uf.union(p, q);
        //System.out.println("connecting " + p + " and " + q + ".");
    }

    sc.close();
}

static void quickUnion(File file) throws FileNotFoundException {
    Scanner sc1 = new Scanner(file);
    System.out.println("\nQuick union");

    int pairsNumQU = sc1.nextInt();
    QU qu = new QU(pairsNumQU);

    for(int i = 0; i < pairsNumQU; i++){
        int p = sc1.nextInt();
        int q = sc1.nextInt();

        if(qu.connected(p, q)){
            //System.out.println(p + " and " + q + " are already connected.");
            continue;
        }

        qu.union(p, q);
        //System.out.println("connecting " + p + " and " + q + ".");
    }

    sc1.close();
}

static void weightedQuickUnion(File file) throws FileNotFoundException {
    Scanner sc2 = new Scanner(file);

    System.out.println("\nWeighted quick union");

    int pairsNumWQU = sc2.nextInt();
    WQU wqu = new WQU(pairsNumWQU);

    for(int i = 0; i < pairsNumWQU; i++){
        int p = sc2.nextInt();
        int q = sc2.nextInt();

        if(wqu.connected(p, q)){
            //System.out.println(p + " and " + q + " are already connected.");
            continue;
        }

        wqu.union(p, q);
        //System.out.println("connecting " + p + " and " + q + ".");
    }

    sc2.close();
}

}

哪个并不重要,因为即使我执行以下操作:

public static void main(String[] args) {

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL url = classLoader.getResource("\\unionfind\\data.txt");

    try{
        File file = new File(url.toURI());
        PrintWriter pw = new PrintWriter(file);

        pw.println("test");

        pw.close();
    }
    catch(Exception e){
        System.out.println(e.getCause());
    }
}

它仍然没有写入该文件,但是如果我提供了它的完整位置,它会写入文件,例如:

public static void main(String[] args) {

    //ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    //URL url = classLoader.getResource("\\unionfind\\data.txt");

    try{
        File file = new File("E:\\Desktop\\test.txt");
        PrintWriter pw = new PrintWriter(file);

        pw.println("test");

        pw.close();
    }
    catch(Exception e){
        System.out.println(e.getCause());
    }
}

所以我猜这一切都与此有关:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

如何让PrintWriter写入同一目录中的文件呢? (P.S.该程序有效,但它就像PrintWriter写入该文件然后自行清理!)

1 个答案:

答案 0 :(得分:0)

您将该文件用作Resource,因此您必须使用unionfind/data.txt而不是\\unionfind\\data.txt进行查找。此外,更新的文件将是与其他类文件一起存在于构建文件夹中的文件。如果类在jar文件中,则jar中的data.txt文件将被更新。