简体   繁体   English

文本输入输出Java

[英]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. 我在读取要获取其内容并操纵两个文件内容的文件后尝试读取2个文件,然后更新一个新文件即输出。 The files are in the same folder as the program but the program always throws a FileNotFoundException . 这些文件与程序位于同一文件夹中,但是程序始终抛出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. 就像@Ankit Rustagi在评论中说的那样,如果要保留当前的实现,则需要文件的完整路径。

However, there is a solution where you only need the file names: use BufferedReader / BufferedWriter . 但是,有一个解决方案,您只需要文件名:使用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. 使用诸如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. 有一种方法可以定位程序的类文件,但是这有点棘手,因为Java允许从可能位于远程系统中的压缩档案中加载类。 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. 我在与src或项目相同的级别上创建了文件。 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. 否则,您将了解发生了什么问题。

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

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