简体   繁体   English

错误,我无法删除临时文件或将其替换为原始文件 java

[英]Error, I can't delete or replace my temp file with my original file java

I tried to delete the temporary file or replace my temp file with my original file.我试图删除临时文件或用我的原始文件替换我的临时文件。 But I don't know how to do it.但我不知道该怎么做。 Can anyone help me with it or point to me which part should I change?谁能帮助我或指出我应该改变哪一部分?

public class Deletion {
    public static void main (String[] args) throws IOException {

        final File inputFile = new File("myFile.txt");
        final File tempFile = new File("myTempFile.txt");
        tempFile.delete();
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(inputFile));
            writer = new BufferedWriter(new FileWriter(tempFile));
    
            final String lineToRemove1 = "Apple";
            final String lineToRemove2 = "Orange";
            String currentLine;
            while((currentLine = reader.readLine()) != null) {
                // trim newline when comparing with lineToRemove
                final String trimmedLine = currentLine.trim();
                if (!currentLine.contains(lineToRemove1) 
                        && !currentLine.contains(lineToRemove2)) {
                    writer.write(currentLine);
                    writer.newLine();
                }
            }
        }
        
        finally {
            if (reader != null) { 
                reader.close();
            }
            if (writer != null) {
                writer.close();
            } 
        }  
        FileChannel src = new FileInputStream(tempFile).getChannel();
        FileChannel dest = new FileOutputStream(inputFile).getChannel();
        dest.transferFrom(src, 0, src.size());
        tempFile.delete();
    }
}

I tried to delete the temporary file or replace my temp file with my original file.我试图删除临时文件或用我的原始文件替换我的临时文件。 But I don't know how to do it.但我不知道该怎么做。 Can anyone help me with it or point to me which part should I change?谁能帮助我或指出我应该改变哪一部分?

public class Deletion {
    public static void main (String[] args) throws IOException {

        final File inputFile = new File("myFile.txt");
        final File tempFile = new File("myTempFile.txt");
        tempFile.delete();
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(inputFile));
            writer = new BufferedWriter(new FileWriter(tempFile));
    
            final String lineToRemove1 = "Apple";
            final String lineToRemove2 = "Orange";
            String currentLine;
            while((currentLine = reader.readLine()) != null) {
                // trim newline when comparing with lineToRemove
                final String trimmedLine = currentLine.trim();
                if (!currentLine.contains(lineToRemove1) 
                        && !currentLine.contains(lineToRemove2)) {
                    writer.write(currentLine);
                    writer.newLine();
                }
            }
        }
        
        finally {
            if (reader != null) { 
                reader.close();
            }
            if (writer != null) {
                writer.close();
            } 
        }  
        FileChannel src = new FileInputStream(tempFile).getChannel();
        FileChannel dest = new FileOutputStream(inputFile).getChannel();
        dest.transferFrom(src, 0, src.size());
        tempFile.delete();
    }
}

I tried to delete the temporary file or replace my temp file with my original file.我试图删除临时文件或用我的原始文件替换我的临时文件。 But I don't know how to do it.但我不知道该怎么做。 Can anyone help me with it or point to me which part should I change?谁能帮助我或指出我应该改变哪一部分?

public class Deletion {
    public static void main (String[] args) throws IOException {

        final File inputFile = new File("myFile.txt");
        final File tempFile = new File("myTempFile.txt");
        tempFile.delete();
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(inputFile));
            writer = new BufferedWriter(new FileWriter(tempFile));
    
            final String lineToRemove1 = "Apple";
            final String lineToRemove2 = "Orange";
            String currentLine;
            while((currentLine = reader.readLine()) != null) {
                // trim newline when comparing with lineToRemove
                final String trimmedLine = currentLine.trim();
                if (!currentLine.contains(lineToRemove1) 
                        && !currentLine.contains(lineToRemove2)) {
                    writer.write(currentLine);
                    writer.newLine();
                }
            }
        }
        
        finally {
            if (reader != null) { 
                reader.close();
            }
            if (writer != null) {
                writer.close();
            } 
        }  
        FileChannel src = new FileInputStream(tempFile).getChannel();
        FileChannel dest = new FileOutputStream(inputFile).getChannel();
        dest.transferFrom(src, 0, src.size());
        tempFile.delete();
    }
}

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

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