简体   繁体   中英

Reading/Writing a file in Java Error

I'm trying to do this assignment:

Write a program that reads a file and writes a copy of the file to another file with line numbers inserted.

So far, I have written this much of the code:

    import java.io.*;
import java.util.Scanner;

public class Question43 
{
public static void main(String[] args) throws IOException
{
    Scanner fileIn = new Scanner(new File("Assign4.txt") );


    FileWriter fileOut = new FileWriter("FileOut.txt");
    PrintWriter output = new PrintWriter(fileOut);

    String []array = new String[10];

    int indx = 0;
    while(fileIn.hasNext( ) )
    {
        array[indx] = fileIn.nextLine( );
    }

    fileIn.close();

    int num = 1;
    for(int i =0; i < array.length; i++)
    {
        output.println(num+"\t"+array[i]);
    }
}
}

When I click run, I get this error:

Exception in thread "main" java.io.FileNotFoundException: Assign4.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at Question43.main(Question43.java:8)

Can someone please advise me on what to do? Where am I supposed to put the text file named Assign4 so Eclipse IDE can read it? Also, where would the new file be created?

I appreciate your help!

In Eclipse the program is run from the top level directory ie the place where the source and bin folders are. If in doubt try

   System.out.println((new java.io.File(".")).getAbsolutePath());

Which will print out the path of the directory that the program is being run from.

You also need to remember to close your writer after using it to ensure what you've written is flushed - the writer might buffer what you've written and if your program finishes before you flush this buffer then what you've written is lost. So close your writer at the end of the program ie

import java.io.*;
import java.util.Scanner;

public class Question43 
{
  public static void main(String[] args) throws IOException
  {
    Scanner fileIn = new Scanner(new File("Assign4.txt") );


    FileWriter fileOut = new FileWriter("FileOut.txt");
    PrintWriter output = new PrintWriter(fileOut);

    String []array = new String[10];

    int indx = 0;
    while(fileIn.hasNext( ) )
    {
        array[indx] = fileIn.nextLine( );
    }

    fileIn.close();

    int num = 1;
    for(int i =0; i < array.length; i++)
    {
        output.println(num+"\t"+array[i]);
    }
    output.close();
  }
}

Another comment - you're assuming that your file has exactly 10 lines. To give you more flexibility try using a data structure that grows on demand, for example an ArrayList . Also, you don't seem to be incrementing num in your loop - so your lines will all be numbered 1.

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

public class Question43 
{
  public static void main(String[] args) throws IOException
  {
    Scanner fileIn = new Scanner(new File("Assign4.txt") );


    FileWriter fileOut = new FileWriter("FileOut.txt");
    PrintWriter output = new PrintWriter(fileOut);

    List<String> list = new ArrayList<String>();

    while(fileIn.hasNext( ) )
    {
        list.add(fileIn.nextLine( ));
    }

    fileIn.close();

    int num = 1;
    for(String line : list)
    {
        output.println(num+"\t"+line);
        num++;
    }
    output.close();
  }
}

Finally, you don't actually need a data structure at all as you can write to your output file as you read your input file ie

import java.io.*;
import java.util.Scanner;

public class Question43 
{
  public static void main(String[] args) throws IOException
  {
    Scanner fileIn = new Scanner(new File("Assign4.txt") );


    FileWriter fileOut = new FileWriter("FileOut.txt");
    PrintWriter output = new PrintWriter(fileOut);

    int num = 1;
    while(fileIn.hasNext( ) )
    {
        String line = fileIn.nextLine( );
        output.println(num+"\t"+line);
        num++;
    }

    fileIn.close();
    output.close();
  }
}

please specify the path for Assign4.txt file. Scanner cann't find the path you specified. Specify path from root directory of your project

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