收到错误:<identifier> expected </identifier>

时间:2014-04-17 19:53:35

标签: identifier

我试图阅读包含小诗文本的文本简单文件,然后将每行发送到输出文件,前面是行号。

我还没有想出如何添加行号,但是当我尝试将每行发送到输出文件时,我一直收到标识符预期错误。这是我的代码:

import java .io.File;
import java.ioFIleNotFoundException; 
import java.io.PrintWriter;
import java.util.Scanner;

public class ReadFile
{
    public static void main(String [] args)
    {
        //Construct Scanner Objects for input files
        Scanner in1 = new Scanner(new File("JackBeNimble.txt"));

        //Construct PrintWriter for the output file
        PrintWriter out = new PrintWriter("JBN_LineByLine.txt");

        //Read lines from the file
        while(in1.hasNextLine())
        {
            String line1 = in1.nextLine();

            out.println(line1); 
        }
    }
    in1.close();
    out.close();
 }

2 个答案:

答案 0 :(得分:0)

java.io.FileNotFoundException之前,你有一个错误的FileNotFoundException(应该是in1.close())和你的结束};是错位的;它应该在out.close()之后;请注意,您也没有处理任何例外情况。

答案 1 :(得分:0)

我发现了一些问题,

// Added the throws FileNotFoundException
public static void main(String [] args) throws FileNotFoundException
{
    //Construct Scanner Objects for input files
    Scanner in1 = new Scanner(new File("JackBeNimble.txt"));

    //Construct PrintWriter for the output file
    PrintWriter out = new PrintWriter("JBN_LineByLine.txt");

    //Read lines from the file
    while(in1.hasNextLine())
    {
        String line1 = in1.nextLine();

        out.println(line1); 
    }
   // Close in the main body.
   in1.close();
   out.close();
}