简体   繁体   中英

Using PrintWriter for file IO

Ok so I'm about to start pulling my hair out. I thought file input was a little tricky, but oh man then there is file output. I am trying to write an array of coordinates stored in an object to a file. In C++ this was easy peasy, but for the love of God I cannot figure this out.

public static void outFile(int intersectionsIndex, Coordinate arg[], Coordinate avgPoint) {

    File file = new File("resource/result.txt");
    file.getParentFile().mkdirs();

    PrintWriter writer = new PrintWriter(file);
    int i = 0;
    while (i < intersectionsIndex) {
        writer.print(arg[i].getX());
        writer.print(" ");
        writer.println(arg[i].getY());
        i++;
    }
    writer.print("Predicted Coordinate: ");
    writer.print(avgPoint.getX());
    writer.print(" ");
    writer.println(avgPoint.getY());
    writer.close();

    return;
}

I am constantly getting the same error no matter what method of IO I use. I followed some posts on here with similar problems but to no avail. Any suggestions or other methods? I am probably missing something basic.

Edit: sorry error is

Error:(83, 30) java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown

Which is the "PrintWriter writer = newPrintWriter(file); line.

UPDATE: Problem solved. Working code below:

public static void outFile(int intersectionsIndex, Coordinate arg[], Coordinate avgPoint) throws Exception{

    File file = new File("resource/result.txt");
    file.getParentFile().mkdirs();

    PrintWriter writer = new PrintWriter(file);
    int i = 0;
    while (i < intersectionsIndex) {
        writer.print(arg[i].getX());
        writer.print(" ");
        writer.println(arg[i].getY());
        i++;
    }
    writer.print("Predicted Coordinate: ");
    writer.print(avgPoint.getX());
    writer.print(" ");
    writer.println(avgPoint.getY());
    writer.close();

    return;
}

It looks like the error that you get is actually a compile-time error, not a runtime error.

The contructor PrintWriter(File) declares a checked FileNotFoundException in its signature, therefore you either need to surround its invocation with try ... catch block, or to declare that exception in throws declaration of your method to catch it later.

See also:

Always try to avoid absolute path because the same code might not work on another system.

I suggest you to place it inside the project under resources folder.

You can try any one based on file location.

// Read from resources folder parallel to src in your project
File file1 = new File("resources/results.txt");

// Read from src/resources folder
File file2 = new File(getClass().getResource("/resources/results.txt").toURI());

You might forget to increment i in while loop. Add i++ to avoid infinite loop.

Try

while (i++ < intersectionsIndex) {...}

OR

while (i < intersectionsIndex) {
   ...
   i++;
}

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