简体   繁体   English

使用Java中的PrintWriter打印到文件无法使用文件名

[英]Printing to file using PrintWriter in Java not working with filename

I am creating a program to analyse a text file, I am using classes to do this with a run file. 我正在创建一个程序来分析文本文件,正在使用类对运行文件进行此操作。 I am trying to get the results to print to a file. 我正在尝试将结果打印到文件中。 I have created a file called report.txt in the same folder however nothing prints in the file. 我已经在同一文件夹中创建了一个名为report.txt的文件,但是该文件中没有任何内容。 I have done this using the full file path and it works fine however I want to do this with just the file name instead providing it is in the same folder. 我已经使用完整的文件路径完成了此操作,并且工作正常,但是我想仅使用文件名来完成此操作,而要提供它在同一文件夹中。

Any help would be much appreciated on how to get the results to print to the file report.txt 任何帮助将不胜感激如何将结果打印到文件report.txt

My code is below: 我的代码如下:

Run file: 运行文件:

package cw;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.IOException;

public class TextAnalyser {
    //Declaration of all Scanners to be used in other classes.

    public static Scanner reader;
    public static Scanner Linesc;


    public static void main(String[] args) throws IOException {
        //User enters a file to be scanned.
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a filename");
        String filename = in.nextLine();
        File InputFile = new File(filename);

        //Assign all scanners to scan users file.
        Linesc = new Scanner(InputFile);


        LineCounter Lineobject = new LineCounter(); 


        Lineobject.TotalLines();

    }

}

Class file 类文件

package cw;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.IOException;
public class LineCounter {
    public static void TotalLines() throws IOException {
                      Scanner sc = TextAnalyser.Linesc;
        PrintWriter out = new PrintWriter(new FileWriter("Report.txt", true));
        int linetotal = 0;
        while (sc.hasNextLine()) {
            sc.nextLine();
            linetotal++;
        }
         out.println("The total number of lines in the file = " + linetotal);

             out.flush();
            out.close();
         System.out.println("The total number of lines in the file = " + linetotal);
    }
}

If it works with a full file path but not with only the file name, it's probably because your current directory, where the file gets written, is not what you think it is. 如果它适用于完整的文件路径,但不能仅适用于文件名,则可能是因为您当前认为的不是文件所在的目录。 See Getting the Current Working Directory in Java to determine the current directory. 请参阅获取Java中的当前工作目录以确定当前目录。

Scanner sc = TextAnalyser.Linesc;
PrintWriter out = new PrintWriter(new FileWriter(
        new File(LineCounter.class.getResource("Report.txt").toURI(), true));
int linetotal = 0;
while (sc.hasNextLine()) {
    sc.nextLine();
    linetotal++;
}

Replace the part of your code with the above, this should be able to find the report.txt file relative to the class folder. 用上面的代码替换部分代码,这应该能够找到相对于class文件夹的report.txt文件。

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

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