简体   繁体   中英

PrintWriter not writing to the current directory

I wrote a program to test union-find, quick-union and weighted quick-union algorithms, and I generate pairs of integers, and write them to a .txt file that is in the same directory using PrintWriter. To find that file in the same directory I use this:

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

To generate random pairs of integers I use this class:

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());
        }

    }
}

But whenever the program finishes I don't see the contents of the file at all (it's empty, or contains whatever I physically wrote to it before).

Here's the main class' code:

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();
}

}

Which doesn't really matter, because even if I do the following:

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());
    }
}

it still doesn't write to that file, however it does write to a file if I provide full location of it, for example:

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());
    }
}

So I guess it all has to do with this:

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

How do I make PrintWriter write to the file in the same directory then? (PS the program works, but it's like PrintWriter writes to that file and then cleans up after itself!)

You are using the file as a Resource hence you will have to look it up using unionfind/data.txt not \\\\unionfind\\\\data.txt . Also, the file that is updated would be the one that is present in the build folder along with the other class files. If the classes are in a jar file, the data.txt file in the jar will be updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM