1

在这里,我提出了我在学习 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();
        }
    }
}

现在我面临的问题是,每当我尝试打开

ABC1.txt

来自工作区文件夹(在 C:)中的文件,它显示 12.00 MB 的大小,每当我刷新文件夹时,大小也会每次都增加。我想提的另一件事是,我看不到文件

ABC.txt

在我正在使用的 IDE (Eclipse) 中......我想要解决方案

1)为什么文件 ABC1.txt 的大小大于 12.00 MB +

2)为什么我无法在我的 IDE 中获取 ABC.txt 文件?

4

2 回答 2

0

我知道了!!!将类范围删除为公共并提及:

class UserFILE_IO{}

您需要做的就是将代码更改为:

try{
    fout = new FileOutputStream("ABC.txt");
    fin = new FileInputStream("ABC.txt");
}  

最后将循环更改为:

try {
    char c;
    while ((c = (char) fin.read()) == 1) {
        fout1.write(c);
        System.out.print(c);
    }

那么你就会把一切都做好。

于 2015-09-16T17:44:37.467 回答
0

您还可以在该包内包含程序源代码的文件夹中找到它。在这里,由于您没有提到要存储的文件的正确路径,因此它必须是默认位置的情况。尝试将路径写得更精确。

这里的情况可能是这样的,abc.txt 相同的文件也可以被文件 ABC.txt 的内容覆盖,所以,如果存在任何名为 abc.txt 的文件,那么 ypu 也应该查看它。

于 2015-09-18T18:04:25.620 回答