使用.getName打印文件名

时间:2016-06-15 13:25:41

标签: java

所以我只是不确定如何使用这种方法。我有一个程序打印Word,字符和行的计数,但我还希望它在发出此信息之前说出文件名。我已经查看了相关信息,但我不确定如何使用我的程序实现这一点。

package practicefiles;

import java.lang.*;
import java.io.*;
import java.util.*;

public class PracticeFiles {

    public static void main(String[] args) throws Exception {
        int charCount = 0;
        int wordCount = 0;
        int lineCount = 0;
        String count;
        StringTokenizer st;
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter file: ");
        count = buf.readLine();
        buf = new BufferedReader(new FileReader(count));
        while ((count = buf.readLine()) != null) {
            lineCount++;
            st = new StringTokenizer(count, " ,;:.");
            while (st.hasMoreTokens()) {
                wordCount++;
                count = st.nextToken();
                charCount += count.length();
            }
        }
        // String fileName = count.getName();
        System.out.println("Character Count : " + charCount);
        System.out.println("Word Count : " + wordCount);
        System.out.println("Line Count : " + lineCount);
        buf.close();
    }
}

总结:询问文件,用户输入文件,打印出字符,字和行数。想在此之前打印出文件名。

3 个答案:

答案 0 :(得分:2)

您无法使用count.getName();因为计数是String而不是File

当您创建BufferedReader时,您需要FileReader,并且当您FileReader时,您正在为其提供文件的路径&{0}} #39;重新开放。

如果您想稍后引用File,那么您需要创建一个存储它的变量。

File inputFile = new File(count);

然后当你制作BufferedReader时,你可以使用 buf=new BufferedReader(new FileReader(inputFile));

如果您想获取File的名称,请使用inputFile.getName();

答案 1 :(得分:0)

一种简单的方法是从buf.readLine()行创建一个字符串,然后创建一个新文件,使用之前的字符串将其传递给FileReader。然后,您可以使用getName();

来引用该文件
public class PracticeFiles {

public static void main(String[] args) throws Exception{
int charCount=0;
int wordCount=0;
int lineCount=0;
String count;
StringTokenizer st;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter file: ");
    String fileName = buf.readLine();
    File file = new File(fileName);
    buf=new BufferedReader(new FileReader(file));
    while((count=buf.readLine())!=null)
        {
            lineCount++;
            st=new StringTokenizer(count," ,;:.");
            while(st.hasMoreTokens())
                {
                    wordCount++;
                    count=st.nextToken();
                    charCount+=count.length();
                }
        }
        System.out.println("The file name is: " + file.getName());
        System.out.println("Character Count : " + charCount);
        System.out.println("Word Count : " + wordCount);
        System.out.println("Line Count : " + lineCount);
        buf.close();
    }
}

答案 2 :(得分:-1)

尝试

package practicefiles;

import java.lang.*;
import java.io.*;
import java.util.*;

public class PracticeFiles {

    public static void main(String[] args) throws Exception {
        int charCount = 0;
        int wordCount = 0;
        int lineCount = 0;
        File file;
        String count;
        StringTokenizer st;
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter file: ");
        file = new File(buf.readLine()); // <-- this is new
        System.out.println("Use "+file.getAbsolutePath());
        buf = new BufferedReader(new FileReader(file));
        while ((count = buf.readLine()) != null) {
            lineCount++;
            st = new StringTokenizer(count, " ,;:.");
            while (st.hasMoreTokens()) {
                wordCount++;
                count = st.nextToken();
                charCount += count.length();
            }
        }
        // String fileName = count.getName();
        System.out.println("Character Count : " + charCount);
        System.out.println("Word Count : " + wordCount);
        System.out.println("Line Count : " + lineCount);
        buf.close();
    }
}