在JAVA中重命名和删除文本文件 [英] Rename and deleting a text file in JAVA

查看:86
本文介绍了在JAVA中重命名和删除文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[重新发布 在java中重命名和删除文本文件 ,可用答案被忽略 - SA]



我有两个文本文件,即 - item.txt(文件1)和temp.txt(文件2)。我的目标是在文件1中搜索一个名称,如果找到,则用不同的名称替换它,并将更新的行写入文件2.另外,我有一个方法检查我在文件1中搜索的字符串的行不包含该字符串的行将被添加到文件2.



所以,这里是我被困的地方。一切正常,除了我要删除文件1的部分,并按文件1重命名文件2(即item.txt)。有人可以帮我纠正吗?我仍然是Java的初学者,所以我的代码可能不是人们所期望的最好的代码,但这是我到目前为止所尝试的。谢谢问题是当我编译代码时,更新的数据被写入file2,而应该被删除的file1不会被删除。那么,可能是什么问题呢?



[Re-posted Renaming and deleting a text file in java, the available answer has been ignored — SA]

I have two text files namely - item.txt (file 1) and temp.txt (file 2). My goal is to search for a name in the file 1 and if found then replace it with a different name and write the updated line to file 2. Also, I have a method that checks for the lines for the string I searched in file 1. The lines that do not contain that string will be added to file 2.

So, here is where I'm stuck. Everything works fine except the part where I want to delete file 1 and rename file 2 by file 1 (i.e item.txt). Can someone please help me with any correction? I am still a beginner in Java, so my code might not be the best looking code as one might expect but this is what I tried so far. Thanks The problem is when i compile the code the updated data is written to file2 and file1 which was supposed to get deleted doesn't delete. So, what could be the problem?

package project4;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class kitkat {

    PrintWriter out,in;
    Scanner in;
    Scanner temp;
    File file1 = new File("item.txt");
    File file2 = new File("temp.txt");

    public void write() throws FileNotFoundException {
        out = new PrintWriter(file1);

        out.println("User1"+ "\t"+"639755"+"\t"+"400");
        out.println("User2"+ "\t"+"639725"+"\t"+"800");
        out.close();        
    }

    public void nfile() throws IOException {
        n = new PrintWriter(new FileWriter(file2,true));
    }

    Scanner input = new Scanner(System.in);
    String replacement = "User3";
    String search;
    String total;

    public void search() {
        System.out.println("Enter your search name");
        search = input.nextLine();
        total = search; 
    }

    public void lolipop() throws IOException {
        in = new Scanner(file1);

        search();
        while(in.hasNext()) {
            String a,b,c;
            a = in.next();
            b = in.next();
            c = in.next();

            if(a.contains(search)) {
                System.out.println("Your match is found"+search);
                a = replacement;
                System.out.println(a+b+c);
                n.file();
                n.println(a+"\t"+b+"\t"+c);

                n.close();
            }
        }
    }

    public void jellybeans() throws IOException {
        temp = new Scanner(file1);
        while(temp.hasNext()) {
            String p,q,r;
            p = temp.next();
            q = temp.next();
            r = temp.next();
            if(!(p.contains(total))) {
                System.out.println(p+q+r);
                n.file();
                n.println(p+"\t"+q+"\t"+r);
                n.close();
                renamefile();
            }
        }
    }

   public void renamefile() {
       file1.delete();
       file2.renameTo(file1);
   }    
}


package project4;

import java.io.FileNotFoundException;
import java.io.IOException;

public class tuna {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        kitkat kt = new kitkat();
        kt.lolipop();
        kt.jellybeans();
    }
}

推荐答案

我已经回答了你上一个问题:在java中重命名和删除文本文件



我准确地解释了你的bug在哪里;很明显。



你做了什么?我没有费心去检查你是否修改了代码。对我来说,这足以让你看到我在我的回答中指出了完全相同的错误。也就是说,你完全忽略了我的答案,并没有试图修复你的错误。我们假设您不理解我的答案。但你没有评论它,没有问任何问题。相反,你只是忽略了整个事情,并决定重新发布同样的问题 - 特别是 - 同样的错误。



这是纯粹的滥用,因为它是。此外,这也适得其反。如果您重新发布并忽略答案,谁会想帮助您?好吧,现在检查自己还不算太晚,但是你需要做一些努力。



-SA
I already answered your previous question: Renaming and deleting a text file in java.

I precisely explained where your bug is; it is very apparent.

And what have you done? I did not bother to check if you modified your code or not. For me, it's enough to see that you reproduced exact same bug I pointed out in my answer. That said, you completely ignored my answer and did not try to fix you bug. Let's assume that you did not understand my answer. But you did not comment on it, did not ask any questions. Instead, you just ignored the whole thing and decided to re-post the same question with — notably — the same exact bug.

And this is the pure abuse as it is. Also, this is counterproductive. Who will want to help you if you are re-posting and ignoring the answers? All right, it's not too late to check yourself, but then you will need to do some effort.

—SA


这篇关于在JAVA中重命名和删除文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆