简体   繁体   English

使用PrintWriter进行文件IO

[英]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. 在C ++中,这很容易,但是对于上帝的爱,我无法弄清楚。

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. 无论使用哪种IO方法,我都会不断遇到相同的错误。 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. 这是“ PrintWriter writer = newPrintWriter(file);”行。

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. 构造函数PrintWriter(File)在其签名中声明一个选中的FileNotFoundException ,因此,您需要在try ... catch块周围对其进行调用,或者在throws方法声明的过程中声明该异常以稍后捕获它。

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. 我建议您将其放在resources文件夹下的项目中。

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. 您可能会忘记在while循环中增加i Add i++ to avoid infinite loop. 添加i++以避免无限循环。

Try 尝试

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

OR 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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