Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不知何故,这个方法删除了我的文件内容,但没有明显的原因_Java_File_Io - Fatal编程技术网

Java 不知何故,这个方法删除了我的文件内容,但没有明显的原因

Java 不知何故,这个方法删除了我的文件内容,但没有明显的原因,java,file,io,Java,File,Io,如果我没有遗漏任何您应该能够运行此代码的内容,只需要在项目根文件夹中创建一个trace.log文件 我不明白发生了什么。我只是声明了一些读写器并尝试从文件中读取。我得到一个即时空值,文件似乎是空的。为什么 import java.io.*; public class StubLogHandler { private String name = ""; private String path = ""; public StubLogHandler (String

如果我没有遗漏任何您应该能够运行此代码的内容,只需要在项目根文件夹中创建一个trace.log文件

我不明白发生了什么。我只是声明了一些读写器并尝试从文件中读取。我得到一个即时空值,文件似乎是空的。为什么

import java.io.*;


public class StubLogHandler {

    private String name = "";
    private String path = "";



    public StubLogHandler (String filePath, String fileName) {
        this.name = fileName;
        this.path = filePath;
    }



    // THIS IS THE PESCKY BUGGER
    public void testReadWrite() {
        this.fixPath();
        File file = new File (this.path+this.name);

        try (   FileReader fileReader = new FileReader(file);
                FileWriter fileWriter = new FileWriter(file);
                BufferedReader reader = new BufferedReader(fileReader);
                BufferedWriter writer = new BufferedWriter(fileWriter);) {


            System.out.println("Works, I think.");
            String line = "";

            while (line != null) {
                line = reader.readLine();
                System.out.println(line);
                // Should get a coupl'a lines, instead I get instant null
                // Before you ask, no, the file is not initially empty
            }

        } catch (FileNotFoundException fnfe) {
            System.out.println("File Not Found");
        } catch (IOException ioe) {
            System.out.println("Could not read or write to file");
        }

    }


    private void fixPath () {
        if (this.path.isEmpty())
            return;
        char lastChar = this.path.charAt(this.path.length()-1);
        if (lastChar != '\\')
            this.path += "\\";  // In case user forgets the final '\'
    }

    public String getAbsolutePath() {
        this.fixPath();
        return new File(this.path+this.name).getAbsolutePath();
    }




}


public class Start {

    public static void main (String[] args) {

        Start.testStuff();

    }

    private static void testStuff() {
        StubLogHandler log = new StubLogHandler("","trace.log");
        System.out.println(log.getPath()+log.getName());
        System.out.println(log.getAbsolutePath());
        log.testReadWrite();
    }

}
编辑

输出:

trace.log

D:\Personal\Java\workspace\Default\Practice\trace.log

我想这很有效

创建该写入程序:

    try (   FileReader fileReader = new FileReader(file);
            FileWriter fileWriter = new FileWriter(file); // here
将立即截断该文件以准备写入。e、 g

File x = new File("X");
FileWriter fw = new FileWriter(x);
将立即删除先前存在的文件X的内容创建该写入程序:

    try (   FileReader fileReader = new FileReader(file);
            FileWriter fileWriter = new FileWriter(file); // here
将立即截断该文件以准备写入。e、 g

File x = new File("X");
FileWriter fw = new FileWriter(x);

将立即删除预先存在的文件X的内容正常读取和单独写入同一文件不起作用

我相信FileReader和FileWriter已经被缓冲了。我个人不使用它们,因为它们使用默认的平台编码,即提供不可移植的数据

然后readLine返回null表示文件的结尾,因此执行以下操作:

        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
            // Should get a coupl'a lines, instead I get instant null
            // Before you ask, no, the file is not initially empty
        }
也许你想做一些事情,比如:

Path fpath = Paths.get(this.path+this.name);
List<String> lines = Files.readAllLines(fpath, StandardCharsets.UTF_8);
... process the lines
Files.write(fpath, lines, StandardCharsets.UTF_8);

正常读取和单独写入同一文件不起作用

我相信FileReader和FileWriter已经被缓冲了。我个人不使用它们,因为它们使用默认的平台编码,即提供不可移植的数据

然后readLine返回null表示文件的结尾,因此执行以下操作:

        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
            // Should get a coupl'a lines, instead I get instant null
            // Before you ask, no, the file is not initially empty
        }
也许你想做一些事情,比如:

Path fpath = Paths.get(this.path+this.name);
List<String> lines = Files.readAllLines(fpath, StandardCharsets.UTF_8);
... process the lines
Files.write(fpath, lines, StandardCharsets.UTF_8);

这是有趣的行为。我应该好好读一读,现在有没有办法阻止这个功能呢?别写了。我不明白你到底想说什么,但打开你正在阅读的文件的编写器可能会让你issues@Kalec您想将FileWriter设置为附加模式。FileWriter FileWriter=新的FileWriterfile,true;。同时读取和写入文件仍然是个坏主意。这正是我正在测试的。当然,我可以阅读整个文档,但我认为这有点太多了。试着玩东西。例如:我可以同时读写一个文件吗?这会导致问题吗?是否有奇怪的行为?例如,我不明白这里发生了什么。为什么要删除?近距离通话是否正常?因为作者是空的,所以它完全删除了所有内容?当然我可以阅读整个文档。那可能是个好主意。请注意我的修正答案。打开文件写入程序这是一个有趣的行为。我应该好好读一读,现在有没有办法阻止这个功能呢?别写了。我不明白你到底想说什么,但打开你正在阅读的文件的编写器可能会让你issues@Kalec您想将FileWriter设置为附加模式。FileWriter FileWriter=新的FileWriterfile,true;。同时读取和写入文件仍然是个坏主意。这正是我正在测试的。当然,我可以阅读整个文档,但我认为这有点太多了。试着玩东西。例如:我可以同时读写一个文件吗?这会导致问题吗?是否有奇怪的行为?例如,我不明白这里发生了什么。为什么要删除?近距离通话是否正常?因为作者是空的,所以它完全删除了所有内容?当然我可以阅读整个文档。那可能是个好主意。请注意我的修正答案。打开文件写入程序启动调试器。逐行浏览代码,看看哪一个会删除您的文件。另外,在堆栈交换上发布时,应该发布一个。要做到这一点,您需要首先找出导致问题的代码行。启动调试器。逐行浏览代码,看看哪一个会删除您的文件。另外,在堆栈交换上发布时,应该发布一个。要做到这一点,您首先需要找出是哪些代码行导致了问题。这非常有趣。我完全被java拥有的大量IO类所淹没,更不用说可怕的api了。路径/路径是不是很糟糕的api设计与文件相结合。readAllLines被认为是更好的方法?Java直到第7版都保留了提供文件和路径的功能。这很有趣。我完全被java拥有的大量IO类所淹没,更不用说可怕的api了。路径/路径是不是很糟糕的api设计与文件相结合。readAllLines被认为是更好的方法?Java直到第7版都保留了提供文件和路径/路径的好方法。