简体   繁体   中英

Text input and output java

I am trying to read 2 files after i read the files i want to get their contents and manipulate the contents of the two files then update a new file which is the output. The files are in the same folder as the program but the program always throws a FileNotFoundException . Below is my code:-

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

public class UpdateMaster {

public static void main(String[] args)
{
    String master = "Customer.dat";
    String trans = "Transactns.dat";
    String newMaster = "Temp.txt";

    Scanner inputStreamMaster = null;
    Scanner inputStreamTrans = null;
    PrintWriter inputStreamNewMaster = null;

    try
    {
        inputStreamMaster = new Scanner(new File(master));
        inputStreamTrans = new Scanner(new File(trans));
        inputStreamNewMaster = new PrintWriter(newMaster);

    }
    catch(FileNotFoundException e)
    {
        System.out.println("Error: you opend a file that does not exist.");
        System.exit(0);
    }
    catch(IOException e)
    {
        System.out.println("Error.");
        System.exit(0);
    }



    do
    {

        String transLine = inputStreamTrans.nextLine();
        String masterLine = inputStreamMaster.nextLine();

        String[] transLineArr = transLine.split(","); 
        String[] masterLineArr = masterLine.split(",");

        int trAccNo = Integer.parseInt(transLineArr[0]);
        int sales = Integer.parseInt(transLineArr[1]);
        int masterAccNo = Integer.parseInt(masterLineArr[0]);
        int balance = Integer.parseInt(masterLineArr[1]);
        while(masterAccNo== trAccNo){

            inputStreamNewMaster.println(trAccNo+ " , "+masterAccNo);
            masterLine = inputStreamMaster.nextLine();
            masterLineArr = masterLine.split(",");
            masterAccNo = Integer.parseInt(masterLineArr[0]);
            balance = Integer.parseInt(masterLineArr[1]);
        }
        balance = balance + sales;
        inputStreamNewMaster.println(masterAccNo+ " , "+balance);








    }while(inputStreamTrans.hasNextLine());

    inputStreamMaster.close();
    inputStreamTrans.close();
    inputStreamNewMaster.close();


    //System.out.println(" the line were written to "+ newMaster);


}
}

Like @Ankit Rustagi said in the comments, you need the full path of the files if you want to keep the current implementation.

However, there is a solution where you only need the file names: use BufferedReader / BufferedWriter . See here an example on how to use these classes (in the example it uses the full path but it works without it too).

There's nothing wrong with using relative paths like tihis. What's happening is that your program is looking for the files in the directory where you execute the program, which doesn't have to be the folder of the program. You can confirm this by logging the absolute path of the files before you try to read them. For example:

File masterFile = new File(master);
System.out.printf("Using master file '%s'%n", masterFile.getAbsolutePath());
inputStreamMaster = new Scanner(masterFile);

In general you should not hardcode file paths but allow the user to specify them in someway, for example using command line arguments, a configuration file with a well known path, or an interactive user interface.

There is a way to locate the program's class file but it's a little tricky because Java allows classes to be loaded from compressed archives that may be located in remote systems. It's better to solve this problem in some other manner.

Use absolute path

String master = "C:/Data/Customer.dat";
String trans = "C:/Data/Transactns.dat";
String newMaster = "C:/Data/Temp.txt";

The code works for me, i guess you misspelled some filename(s) or your files are in the wrong folder. I created your files on the same level as the src or the project. Also this is the folder where the files are exspected.

Try this:

String current = new java.io.File( "." ).getCanonicalPath();
System.out.println("I look for files in:"+current);

To see what directory your program expects to find its input files. If it shows the correct directory, check spelling of filenames. Otherwise, you have a clue as to what's gone wrong.

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