Skip to content
Advertisement

Several Errors being Logged for File I/O code

Here is my code from learning the File i/o…

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

public class UserFILE_IO {
    static FileInputStream fin;
    static FileOutputStream fout, fout1;

    public static void main(String[] args) {

        try {
            fin = new FileInputStream("ABC.txt");
            fout = new FileOutputStream("ABC.txt");
        } catch (FileNotFoundException e) {
            System.out.println("FILE NOT FOUND!!!");
        }

        String s;

        Scanner sc = new Scanner(System.in);
        s = sc.nextLine();

        try {
            fout.write(s.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {

            fout.close();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        sc.close();
        try {
            fout1 = new FileOutputStream("ABC1.txt");

        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        try {
            char c;
            while ((c = (char) fin.read()) != -1) {
                fout1.write(c);
                System.out.print(c);

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            fin.close();
            fout1.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Whenever I try to open the ABC1.txt file from the workspace folder (In C:), it shows the size of 12.00 MB and whenever I refresh the folder the size also gets increased each time . And another thing I want to mention is, I m not able to see the File ABC.txt in the IDE (Eclipse) which I m using.

  1. Why that file ABC1.txt is that larger of size 12.00 MB+

  2. And why I m not able to get the ABC.txt file in my IDE?

Advertisement

Answer

You can also find it in the folder consisting the source code of your program inside that package. Here as you have not mentioned the correct path to the file that where it is going to be stored then it must be the case of the default location . Try to write the path more precise.

Here the case may be like , abc.txt same file can also be overridden with contents of the file ABC.txt so , if there exist any file named abc.txt then ypu should also look into it.

Advertisement